12 #ifndef SST_CORE_FROM_STRING_H
13 #define SST_CORE_FROM_STRING_H
17 #include <type_traits>
23 typename std::enable_if<std::is_integral<T>::value, T>::type
24 from_string(
const std::string& input)
26 if ( std::is_signed<T>::value ) {
27 if ( std::is_same<int, T>::value ) {
return std::stoi(input,
nullptr, 0); }
28 else if ( std::is_same<long, T>::value ) {
29 return std::stol(input,
nullptr, 0);
31 else if ( std::is_same<long long, T>::value ) {
32 return std::stoll(input,
nullptr, 0);
35 return static_cast<T
>(std::stol(input,
nullptr, 0));
39 if ( std::is_same<bool, T>::value ) {
41 std::string transform(input);
42 for (
auto& c : transform )
44 if ( transform ==
"true" || transform ==
"t" || transform ==
"yes" || transform ==
"y" ||
45 transform ==
"on" || transform ==
"1" ) {
49 transform ==
"false" || transform ==
"f" || transform ==
"no" || transform ==
"n" ||
50 transform ==
"off" || transform ==
"0" ) {
54 throw new std::invalid_argument(
"from_string: no valid conversion");
57 else if ( std::is_same<unsigned long, T>::value ) {
58 return std::stoul(input,
nullptr, 0);
60 else if ( std::is_same<unsigned long long, T>::value ) {
61 return std::stoull(input,
nullptr, 0);
64 return static_cast<T
>(std::stoul(input,
nullptr, 0));
70 typename std::enable_if<std::is_floating_point<T>::value, T>::type
71 from_string(
const std::string& input)
73 if ( std::is_same<float, T>::value ) {
return stof(input); }
74 else if ( std::is_same<double, T>::value ) {
77 else if ( std::is_same<long double, T>::value ) {
86 typename std::enable_if<std::is_class<T>::value, T>::type
87 from_string(
const std::string& input)
90 std::is_constructible<T, std::string>::value,
91 "ERROR: from_string can only be used with integral and floating point types and with classes that have a "
92 "constructor that takes a single string as input.\n");
99 #endif // SST_CORE_FROM_STRING_H