KSSUtil  v14.2.0
C++ general utility library
convert.hpp
Go to the documentation of this file.
1 //
2 // convert.hpp
3 // kssutil
4 //
5 // Created by Steven W. Klassen on 2018-06-15.
6 // Copyright © 2018 Klassen Software Solutions. All rights reserved.
7 // Licensing follows the MIT License.
8 //
9 
10 #ifndef kssutil_convert_hpp
11 #define kssutil_convert_hpp
12 
13 #include <cerrno>
14 #include <chrono>
15 #include <iostream>
16 #include <sstream>
17 #include <string>
18 #include <system_error>
19 #include <typeinfo>
20 
21 #include <kss/contract/all.h>
22 
23 #include "rtti.hpp"
24 
30 namespace kss { namespace util { namespace strings {
31 
37  template <class T>
38  T convert(const std::string& s, const T& = T()) {
39  kss::contract::parameters({
40  KSS_EXPR(!s.empty())
41  });
42 
43  std::istringstream strm(s);
44  T t;
45  strm >> t;
46  if (strm.bad() || strm.fail()) {
47  throw std::system_error(EIO, std::system_category(),
48  "Could not convert '" + s + "'"
49  + " to the type " + kss::util::rtti::name<T>());
50  }
51  return t;
52  }
53 
54  template<>
55  inline std::string convert(const std::string& s, const std::string&) {
56  return s;
57  }
58 
59  // These specializations use implementations from <cstdlib> as they should be
60  // more efficient than using the stream conversions.
61  template<> float convert(const std::string& s, const float&);
62  template<> double convert(const std::string& s, const double&);
63  template<> long double convert(const std::string& s, const long double&);
64  template<> int convert(const std::string& s, const int&);
65  template<> long convert(const std::string& s, const long&);
66  template<> long long convert(const std::string& s, const long long&);
67  template<> unsigned convert(const std::string& s, const unsigned&);
68  template<> unsigned long convert(const std::string& s, const unsigned long&);
69  template<> unsigned long long convert(const std::string& s,
70  const unsigned long long&);
71 
72  // These specializations are used to convert text representations of durations
73  // (e.g. "10s") into their corresponding durations (e.g. std::chrono::seconds).
74  // Note that while these will convert the duration types, for example,
75  // seconds oneMinute = convert("1m");
76  // is perfectly valid and will be equivalent to 60s, they will throw an
77  // std::overflow_error if the resulting value cannot be represented by the desired
78  // type.
79  template<> std::chrono::hours convert(const std::string& s, const std::chrono::hours&);
80  template<> std::chrono::minutes convert(const std::string& s, const std::chrono::minutes&);
81  template<> std::chrono::seconds convert(const std::string& s, const std::chrono::seconds&);
82  template<> std::chrono::milliseconds convert(const std::string& s, const std::chrono::milliseconds&);
83  template<> std::chrono::microseconds convert(const std::string& s, const std::chrono::microseconds&);
84  template<> std::chrono::nanoseconds convert(const std::string& s, const std::chrono::nanoseconds&);
85 
86  // These specializations are used to convert text representations of time, in
87  // ISO8601 format, into time_point values. This covers the "standard" time_point
88  // definitions. If you have your own you will need to provide your own overrides.
89  template<>
90  std::chrono::time_point<std::chrono::system_clock, std::chrono::hours>
91  convert(const std::string& s, const std::chrono::time_point<std::chrono::system_clock, std::chrono::hours>&);
92 
93  template<>
94  std::chrono::time_point<std::chrono::system_clock, std::chrono::minutes>
95  convert(const std::string& s, const std::chrono::time_point<std::chrono::system_clock, std::chrono::minutes>&);
96 
97  template<>
98  std::chrono::time_point<std::chrono::system_clock, std::chrono::seconds>
99  convert(const std::string& s, const std::chrono::time_point<std::chrono::system_clock, std::chrono::seconds>&);
100 
101  template<>
102  std::chrono::time_point<std::chrono::system_clock, std::chrono::milliseconds>
103  convert(const std::string& s, const std::chrono::time_point<std::chrono::system_clock, std::chrono::milliseconds>&);
104 
105  template<>
106  std::chrono::time_point<std::chrono::system_clock, std::chrono::microseconds>
107  convert(const std::string& s, const std::chrono::time_point<std::chrono::system_clock, std::chrono::microseconds>&);
108 
109  template<>
110  std::chrono::time_point<std::chrono::system_clock, std::chrono::nanoseconds>
111  convert(const std::string& s, const std::chrono::time_point<std::chrono::system_clock, std::chrono::nanoseconds>&);
112 
113 
114  template<>
115  std::chrono::time_point<std::chrono::steady_clock, std::chrono::hours>
116  convert(const std::string& s, const std::chrono::time_point<std::chrono::steady_clock, std::chrono::hours>&);
117 
118  template<>
119  std::chrono::time_point<std::chrono::steady_clock, std::chrono::minutes>
120  convert(const std::string& s, const std::chrono::time_point<std::chrono::steady_clock, std::chrono::minutes>&);
121 
122  template<>
123  std::chrono::time_point<std::chrono::steady_clock, std::chrono::seconds>
124  convert(const std::string& s, const std::chrono::time_point<std::chrono::steady_clock, std::chrono::seconds>&);
125 
126  template<>
127  std::chrono::time_point<std::chrono::steady_clock, std::chrono::milliseconds>
128  convert(const std::string& s, const std::chrono::time_point<std::chrono::steady_clock, std::chrono::milliseconds>&);
129 
130  template<>
131  std::chrono::time_point<std::chrono::steady_clock, std::chrono::microseconds>
132  convert(const std::string& s, const std::chrono::time_point<std::chrono::steady_clock, std::chrono::microseconds>&);
133 
134  template<>
135  std::chrono::time_point<std::chrono::steady_clock, std::chrono::nanoseconds>
136  convert(const std::string& s, const std::chrono::time_point<std::chrono::steady_clock, std::chrono::nanoseconds>&);
137 }}}
138 
139 #endif
kss::util::strings::convert
T convert(const std::string &s, const T &=T())
Definition: convert.hpp:38
rtti.hpp
Run Time Type Information.
kss
All Klassen Software Solutions libraries begin with this namespace.
Definition: add_rel_ops.hpp:19