All Classes Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
lexical_cast.hpp
1 // The MIT License (MIT)
2 
3 // Copyright (c) 2012-2014 Rapptz
4 
5 // Permission is hereby granted, free of charge, to any person obtaining a copy of
6 // this software and associated documentation files (the "Software"), to deal in
7 // the Software without restriction, including without limitation the rights to
8 // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9 // the Software, and to permit persons to whom the Software is furnished to do so,
10 // subject to the following conditions:
11 
12 // The above copyright notice and this permission notice shall be included in all
13 // copies or substantial portions of the Software.
14 
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17 // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18 // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19 // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20 // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 
22 #ifndef GEARS_STRING_LEXICAL_CAST_HPP
23 #define GEARS_STRING_LEXICAL_CAST_HPP
24 
25 #include <string>
26 #include <sstream>
27 #include <stdexcept>
28 
29 namespace gears {
30 namespace string {
31 namespace detail {
32 template<typename Target>
33 struct lexical_caster {
34  template<typename CharT>
35  static inline Target cast(const std::basic_string<CharT>& str) {
36  std::basic_istringstream<CharT> ss(str);
37  Target result;
38  if((ss >> result).fail() || !(ss >> std::ws).eof()) {
39  throw std::invalid_argument("lexical_cast failed");
40  }
41  return result;
42  }
43 };
44 
45 template<typename CharT>
46 struct lexical_caster<std::basic_string<CharT>> {
47  static inline std::basic_string<CharT> cast(const std::basic_string<CharT>& str) {
48  return str;
49  }
50 };
51 
52 template<>
53 struct lexical_caster<int> {
54  template<typename CharT>
55  static inline int cast(const std::basic_string<CharT>& str) {
56  return std::stoi(str);
57  }
58 };
59 
60 template<>
61 struct lexical_caster<long> {
62  template<typename CharT>
63  static inline long cast(const std::basic_string<CharT>& str) {
64  return std::stol(str);
65  }
66 };
67 
68 
69 template<>
70 struct lexical_caster<long long> {
71  template<typename CharT>
72  static inline long long cast(const std::basic_string<CharT>& str) {
73  return std::stoll(str);
74  }
75 };
76 
77 template<>
78 struct lexical_caster<float> {
79  template<typename CharT>
80  static inline float cast(const std::basic_string<CharT>& str) {
81  return std::stof(str);
82  }
83 };
84 
85 template<>
86 struct lexical_caster<double> {
87  template<typename CharT>
88  static inline double cast(const std::basic_string<CharT>& str) {
89  return std::stod(str);
90  }
91 };
92 
93 template<>
94 struct lexical_caster<long double> {
95  template<typename CharT>
96  static inline long double cast(const std::basic_string<CharT>& str) {
97  return std::stold(str);
98  }
99 };
100 
101 template<>
102 struct lexical_caster<unsigned long> {
103  template<typename CharT>
104  static inline unsigned long cast(const std::basic_string<CharT>& str) {
105  return std::stoul(str);
106  }
107 };
108 
109 template<>
110 struct lexical_caster<unsigned long long> {
111  template<typename CharT>
112  static inline unsigned long long cast(const std::basic_string<CharT>& str) {
113  return std::stoull(str);
114  }
115 };
116 
117 template<typename T>
118 struct remove_const {
119  using type = T;
120 };
121 
122 template<typename T>
123 struct remove_const<const T> {
124  using type = T;
125 };
126 
127 template<typename T>
128 struct remove_volatile {
129  using type = T;
130 };
131 
132 template<typename T>
133 struct remove_volatile<volatile T> {
134  using type = T;
135 };
136 
137 template<typename T>
138 struct remove_cv {
139  using type = typename std::remove_volatile<typename std::remove_const<T>::type>::type;
140 };
141 } // detail
142 
159 template<typename Target, typename CharT>
160 inline Target lexical_cast(const std::basic_string<CharT>& str) {
161  return detail::lexical_caster<typename detail::remove_cv<Target>::type>::cast(str);
162 }
163 } // string
164 } // gears
165 
166 #endif // GEARS_STRING_LEXICAL_CAST_HPP