ctrl-utils
string.h
1 
10 #ifndef CTRL_UTILS_STRING_H_
11 #define CTRL_UTILS_STRING_H_
12 
13 #include <sstream> // std::string
14 #include <string> // std::stringstream
15 
16 namespace ctrl_utils {
17 
21 template <typename T>
22 inline std::string ToString(const T& value) {
23  std::stringstream ss;
24  ss << value;
25  return ss.str();
26 }
27 
31 template <>
32 inline std::string ToString(const std::string& value) {
33  return value;
34 }
35 
39 template <typename T>
40 inline void ToString(std::string& str, const T& value) {
41  str = ToString(value);
42 }
43 
47 template <typename T>
48 inline void FromString(const std::string& str, T& value) {
49  std::stringstream ss(str);
50  ss >> value;
51 }
52 
56 template <typename T>
57 inline T FromString(const std::string& str) {
58  T value;
59  FromString(str, value);
60  return value;
61 }
62 
66 template <>
67 inline void FromString(const std::string& str, std::string& value) {
68  value = str;
69 }
70 
71 #if __cplusplus >= 201703L
72 } // namespace
73 
74 #include <string_view>
75 
76 namespace ctrl_utils {
77 
78 template <typename T>
79 inline T FromString(const std::string_view& str) {
80  T value;
81  FromString(std::string{str}, value);
82  return value;
83 }
84 
85 template <>
86 inline void FromString(const std::string& str, std::string_view& value) {
87  value = str;
88 }
89 
90 #endif // __cplusplus >= 201703L
91 
92 inline std::ostream& bold(std::ostream& os) { return os << "\e[1m"; }
93 inline std::ostream& underline(std::ostream& os) { return os << "\e[4m"; }
94 inline std::ostream& bold_underline(std::ostream& os) { return os << "\e[1;4m"; }
95 inline std::ostream& normal(std::ostream& os) { return os << "\e[0m"; }
96 
97 } // namespace ctrl_utils
98 
99 #endif // CTRL_UTILS_STRING_H_
Definition: ctrl_utils.cc:18
nlohmann::json FromString(const std::string &str)
Definition: json.h:27