13 #ifndef SST_CORE_FROM_STRING_H
14 #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 ) {
28 return std::stoi(input,0,0);
30 else if ( std::is_same<long,T>::value ) {
31 return std::stol(input,0,0);
33 else if ( std::is_same<long long, T>::value ) {
34 return std::stoll(input,0,0);
37 return static_cast<T
>(std::stol(input,0,0));
41 if ( std::is_same<bool, T>::value ) {
43 std::string transform(input);
44 for (
auto & c: transform) c = std::tolower(c);
45 if ( transform ==
"true" || transform ==
"t" || transform ==
"yes" || transform ==
"y" || transform ==
"on" || transform ==
"1" ) {
47 }
else if ( transform ==
"false" || transform ==
"f" || transform ==
"no" || transform ==
"n" || transform ==
"off" || transform ==
"0" ) {
51 throw new std::invalid_argument(
"from_string: no valid conversion");
54 else if ( std::is_same<unsigned long, T>::value ) {
55 return std::stoul(input,0,0);
57 else if ( std::is_same<unsigned long long, T>::value ) {
58 return std::stoull(input,0,0);
61 return static_cast<T
>(std::stoul(input,0,0));
67 typename std::enable_if<std::is_floating_point<T>::value, T >::type
68 from_string(
const std::string& input)
70 if ( std::is_same<float, T>::value ) {
73 else if ( std::is_same<double, T>::value ) {
76 else if ( std::is_same<long double, T>::value ) {
82 typename std::enable_if<std::is_class<T>::value, T >::type
83 from_string(
const std::string& input) {
84 static_assert(std::is_constructible<T,std::string>::value,
"ERROR: from_string can only be used with integral and floating point types and with classes that have a constructor that takes a single string as input.\n");
91 #endif // SST_CORE_FROM_STRING_H