libpqxx
strconv.hxx
1 
11 #ifndef PQXX_H_STRINGCONV
12 #define PQXX_H_STRINGCONV
13 
14 #include "pqxx/compiler-public.hxx"
15 
16 #include <limits>
17 #include <sstream>
18 #include <stdexcept>
19 
20 
21 namespace pqxx
22 {
23 
46 
48 
51 template<typename T, typename = void> struct string_traits;
52 
53 namespace internal
54 {
56 [[noreturn]] PQXX_LIBEXPORT void throw_null_conversion(
57  const std::string &type);
58 } // namespace pqxx::internal
59 
60 
68 #define PQXX_DECLARE_STRING_TRAITS_SPECIALIZATION(T) \
69 template<> struct PQXX_LIBEXPORT string_traits<T> \
70 { \
71  static constexpr const char *name() noexcept { return #T; } \
72  static constexpr bool has_null() noexcept { return false; } \
73  static bool is_null(T) { return false; } \
74  [[noreturn]] static T null() \
75  { internal::throw_null_conversion(name()); } \
76  static void from_string(const char Str[], T &Obj); \
77  static std::string to_string(T Obj); \
78 };
79 
81 
90 
94 
95 #undef PQXX_DECLARE_STRING_TRAITS_SPECIALIZATION
96 
97 
99 
108 template<typename ENUM>
110 {
111  using underlying_type = typename std::underlying_type<ENUM>::type;
113 
114  static constexpr bool has_null() noexcept { return false; }
115  [[noreturn]] static ENUM null()
116  { internal::throw_null_conversion("enum type"); }
117 
118  static void from_string(const char Str[], ENUM &Obj)
119  {
120  underlying_type tmp;
122  Obj = ENUM(tmp);
123  }
124 
125  static std::string to_string(ENUM Obj)
127 };
128 
129 
131 
142 #define PQXX_DECLARE_ENUM_CONVERSION(ENUM) \
143 template<> \
144 struct string_traits<ENUM> : pqxx::enum_traits<ENUM> \
145 { \
146  static constexpr const char *name() noexcept { return #ENUM; } \
147  [[noreturn]] static ENUM null() \
148  { internal::throw_null_conversion(name()); } \
149 }
150 
151 
153 template<> struct PQXX_LIBEXPORT string_traits<const char *>
154 {
155  static constexpr const char *name() noexcept { return "const char *"; }
156  static constexpr bool has_null() noexcept { return true; }
157  static bool is_null(const char *t) { return t == nullptr; }
158  static const char *null() { return nullptr; }
159  static void from_string(const char Str[], const char *&Obj) { Obj = Str; }
160  static std::string to_string(const char *Obj) { return Obj; }
161 };
162 
164 template<> struct PQXX_LIBEXPORT string_traits<char *>
165 {
166  static constexpr const char *name() noexcept { return "char *"; }
167  static constexpr bool has_null() noexcept { return true; }
168  static bool is_null(const char *t) { return t == nullptr; }
169  static const char *null() { return nullptr; }
170 
171  // Don't allow this conversion since it breaks const-safety.
172  // static void from_string(const char Str[], char *&Obj);
173 
174  static std::string to_string(char *Obj) { return Obj; }
175 };
176 
178 template<size_t N> struct PQXX_LIBEXPORT string_traits<char[N]>
179 {
180  static constexpr const char *name() noexcept { return "char[]"; }
181  static constexpr bool has_null() noexcept { return true; }
182  static bool is_null(const char t[]) { return t == nullptr; }
183  static const char *null() { return nullptr; }
184  static std::string to_string(const char Obj[]) { return Obj; }
185 };
186 
187 template<> struct PQXX_LIBEXPORT string_traits<std::string>
188 {
189  static constexpr const char *name() noexcept { return "string"; }
190  static constexpr bool has_null() noexcept { return false; }
191  static bool is_null(const std::string &) { return false; }
192  [[noreturn]] static std::string null()
194  static void from_string(const char Str[], std::string &Obj) { Obj=Str; }
195  static std::string to_string(const std::string &Obj) { return Obj; }
196 };
197 
198 template<> struct PQXX_LIBEXPORT string_traits<const std::string>
199 {
200  static constexpr const char *name() noexcept { return "const string"; }
201  static constexpr bool has_null() noexcept { return false; }
202  static bool is_null(const std::string &) { return false; }
203  [[noreturn]] static const std::string null()
205  static const std::string to_string(const std::string &Obj) { return Obj; }
206 };
207 
208 template<> struct PQXX_LIBEXPORT string_traits<std::stringstream>
209 {
210  static constexpr const char *name() noexcept { return "stringstream"; }
211  static constexpr bool has_null() noexcept { return false; }
212  static bool is_null(const std::stringstream &) { return false; }
213  [[noreturn]] static std::stringstream null()
215  static void from_string(const char Str[], std::stringstream &Obj)
216  { Obj.clear(); Obj << Str; }
217  static std::string to_string(const std::stringstream &Obj)
218  { return Obj.str(); }
219 };
220 
221 
222 // TODO: Implement date conversions.
223 
225 
237 template<typename T>
238  inline void from_string(const char Str[], T &Obj)
239 {
240  if (Str == nullptr) throw std::runtime_error{"Attempt to read null string."};
242 }
243 
244 
246 
252 template<typename T> inline void from_string(const char Str[], T &Obj, size_t)
253 {
254  return from_string(Str, Obj);
255 }
256 
257 template<>
258  inline void from_string<std::string>( //[t00]
259  const char Str[],
260  std::string &Obj,
261  size_t len)
262 {
263  if (Str == nullptr) throw std::runtime_error{"Attempt to read null string."};
264  Obj.assign(Str, len);
265 }
266 
267 template<typename T>
268  inline void from_string(const std::string &Str, T &Obj) //[t45]
269  { from_string(Str.c_str(), Obj); }
270 
271 template<typename T>
272  inline void from_string(const std::stringstream &Str, T &Obj) //[t00]
273  { from_string(Str.str(), Obj); }
274 
275 template<> inline void
276 from_string(const std::string &Str, std::string &Obj) //[t46]
277  { Obj = Str; }
278 
279 
280 namespace internal
281 {
283 constexpr int digit_to_number(char c) noexcept { return c-'0'; }
284 constexpr char number_to_digit(int i) noexcept
285  { return static_cast<char>(i+'0'); }
286 } // namespace pqxx::internal
287 
288 
290 
294 template<typename T> inline std::string to_string(const T &Obj)
295  { return string_traits<T>::to_string(Obj); }
296 
298 
299 } // namespace pqxx
300 
301 #endif
void from_string(const field &F, T &Obj)
Convert a field&#39;s string contents to another type.
Definition: field.hxx:365
static const std::string to_string(const std::string &Obj)
Definition: strconv.hxx:205
void throw_null_conversion(const std::string &type)
Throw exception for attempt to convert null to given type.
Definition: strconv.cxx:58
static bool is_null(const char *t)
Definition: strconv.hxx:157
static void from_string(const char Str[], std::string &Obj)
Definition: strconv.hxx:194
static std::string to_string(const std::string &Obj)
Definition: strconv.hxx:195
constexpr int digit_to_number(char c) noexcept
Compute numeric value of given textual digit (assuming that it is a digit)
Definition: strconv.hxx:283
static const char * null()
Definition: strconv.hxx:183
static const std::string null()
Definition: strconv.hxx:203
static bool is_null(const std::string &)
Definition: strconv.hxx:202
static std::string to_string(char *Obj)
Definition: strconv.hxx:174
static constexpr const char * name() noexcept
Definition: strconv.hxx:166
STL namespace.
static std::string to_string(const std::stringstream &Obj)
Definition: strconv.hxx:217
void from_string(const std::string &Str, std::string &Obj)
Definition: strconv.hxx:276
static bool is_null(const std::stringstream &)
Definition: strconv.hxx:212
static bool is_null(const char t[])
Definition: strconv.hxx:182
static constexpr const char * name() noexcept
Definition: strconv.hxx:210
static std::string to_string(const char *Obj)
Definition: strconv.hxx:160
std::string to_string(const field &Obj)
Convert a field to a string.
Definition: result.cxx:448
static std::string null()
Definition: strconv.hxx:192
static constexpr bool has_null() noexcept
Definition: strconv.hxx:156
static constexpr bool has_null() noexcept
Definition: strconv.hxx:167
Helper class for defining enum conversions.
Definition: strconv.hxx:109
constexpr char number_to_digit(int i) noexcept
Definition: strconv.hxx:284
static void from_string(const char Str[], const char *&Obj)
Definition: strconv.hxx:159
static ENUM null()
Definition: strconv.hxx:115
static constexpr bool has_null() noexcept
Definition: strconv.hxx:181
static std::string to_string(const char Obj[])
Definition: strconv.hxx:184
Traits class for use in string conversions.
Definition: strconv.hxx:51
static constexpr bool has_null() noexcept
Definition: strconv.hxx:190
typename std::underlying_type< ENUM >::type underlying_type
Definition: strconv.hxx:111
std::string to_string(const T &Obj)
Convert built-in type to a readable string that PostgreSQL will understand.
Definition: strconv.hxx:294
static constexpr bool has_null() noexcept
Definition: strconv.hxx:114
static constexpr bool has_null() noexcept
Definition: strconv.hxx:211
static const char * null()
Definition: strconv.hxx:169
static constexpr const char * name() noexcept
Definition: strconv.hxx:180
static constexpr bool has_null() noexcept
Definition: strconv.hxx:201
static bool is_null(const char *t)
Definition: strconv.hxx:168
static constexpr const char * name() noexcept
Definition: strconv.hxx:155
static constexpr const char * name() noexcept
Definition: strconv.hxx:189
static void from_string(const char Str[], ENUM &Obj)
Definition: strconv.hxx:118
static constexpr const char * name() noexcept
Definition: strconv.hxx:200
#define PQXX_DECLARE_STRING_TRAITS_SPECIALIZATION(T)
Definition: strconv.hxx:68
The home of all libpqxx classes, functions, templates, etc.
Definition: array.hxx:25
static std::stringstream null()
Definition: strconv.hxx:213
static const char * null()
Definition: strconv.hxx:158
static bool is_null(const std::string &)
Definition: strconv.hxx:191
static std::string to_string(ENUM Obj)
Definition: strconv.hxx:125
static void from_string(const char Str[], std::stringstream &Obj)
Definition: strconv.hxx:215