-
Notifications
You must be signed in to change notification settings - Fork 2
/
Convert.h
38 lines (33 loc) · 1.11 KB
/
Convert.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#ifndef CONVERT
namespace Convert
{
enum class String_start { Alpha, Alphanumeric };
template<typename T>
inline void check_item(const T& t) {}
template<>
inline void check_item(const std::string& s)
{
// Check whether string is empty.
if (s.empty())
throw std::runtime_error("Illegal string");
// Return string if all characters are alphanumeric.
if (find_if(s.begin(), s.end(), [](const char c) { return !std::isalnum(c); }) == s.end())
return;
else
throw std::runtime_error("Illegal string: " + s);
}
template<typename T>
inline T get_item_from_stream(std::istringstream& ss)
{
// Read the item from the stringstream, operator >> trims automatically.
T item;
if (!(ss >> item))
throw std::runtime_error("Item does not match type");
// Check whether stringstream is empty, if not type is incorrect.
std::string dummy;
if (ss >> dummy)
throw std::runtime_error("Item does not match type");
return item;
}
}
#endif