libpqxx  5.0
strconv.hxx
1 /*-------------------------------------------------------------------------
2  *
3  * FILE
4  * pqxx/stringconv.hxx
5  *
6  * DESCRIPTION
7  * String conversion definitions for libpqxx
8  * DO NOT INCLUDE THIS FILE DIRECTLY; include pqxx/stringconv instead.
9  *
10  * Copyright (c) 2008-2015, Jeroen T. Vermeulen <jtv@xs4all.nl>
11  *
12  * See COPYING for copyright license. If you did not receive a file called
13  * COPYING with this source code, please notify the distributor of this mistake,
14  * or contact the author.
15  *
16  *-------------------------------------------------------------------------
17  */
18 #ifndef PQXX_H_STRINGCONV
19 #define PQXX_H_STRINGCONV
20 
21 #include "pqxx/compiler-public.hxx"
22 
23 #include <sstream>
24 #include <stdexcept>
25 
26 
27 namespace pqxx
28 {
29 
41 
43 
46 template<typename T> struct string_traits {};
47 
48 namespace internal
49 {
51 PQXX_NORETURN PQXX_LIBEXPORT void throw_null_conversion(
52  const std::string &type);
53 } // namespace pqxx::internal
54 
55 #define PQXX_DECLARE_STRING_TRAITS_SPECIALIZATION(T) \
56 template<> struct PQXX_LIBEXPORT string_traits<T> \
57 { \
58  typedef T subject_type; \
59  static const char *name() { return #T; } \
60  static bool has_null() { return false; } \
61  static bool is_null(T) { return false; } \
62  static T null() \
63  { internal::throw_null_conversion(name()); return subject_type(); } \
64  static void from_string(const char Str[], T &Obj); \
65  static std::string to_string(T Obj); \
66 };
67 
69 
78 
82 
83 #undef PQXX_DECLARE_STRING_TRAITS_SPECIALIZATION
84 
86 template<> struct PQXX_LIBEXPORT string_traits<const char *>
87 {
88  static const char *name() { return "const char *"; }
89  static bool has_null() { return true; }
90  static bool is_null(const char *t) { return !t; }
91  static const char *null() { return NULL; }
92  static void from_string(const char Str[], const char *&Obj) { Obj = Str; }
93  static std::string to_string(const char *Obj) { return Obj; }
94 };
95 
97 template<> struct PQXX_LIBEXPORT string_traits<char *>
98 {
99  static const char *name() { return "char *"; }
100  static bool has_null() { return true; }
101  static bool is_null(const char *t) { return !t; }
102  static const char *null() { return NULL; }
103 
104  // Don't allow this conversion since it breaks const-safety.
105  // static void from_string(const char Str[], char *&Obj);
106 
107  static std::string to_string(char *Obj) { return Obj; }
108 };
109 
111 template<size_t N> struct PQXX_LIBEXPORT string_traits<char[N]>
112 {
113  static const char *name() { return "char[]"; }
114  static bool has_null() { return true; }
115  static bool is_null(const char t[]) { return !t; }
116  static const char *null() { return NULL; }
117  static std::string to_string(const char Obj[]) { return Obj; }
118 };
119 
121 
124 template<size_t N> struct PQXX_LIBEXPORT string_traits<const char[N]>
125 {
126  static const char *name() { return "char[]"; }
127  static bool has_null() { return true; }
128  static bool is_null(const char t[]) { return !t; }
129  static const char *null() { return NULL; }
130  static std::string to_string(const char Obj[]) { return Obj; }
131 };
132 
133 
134 template<> struct PQXX_LIBEXPORT string_traits<std::string>
135 {
136  static const char *name() { return "string"; }
137  static bool has_null() { return false; }
138  static bool is_null(const std::string &) { return false; }
139  static std::string null()
140  { internal::throw_null_conversion(name()); return std::string(); }
141  static void from_string(const char Str[], std::string &Obj) { Obj=Str; }
142  static std::string to_string(const std::string &Obj) { return Obj; }
143 };
144 
145 template<> struct PQXX_LIBEXPORT string_traits<const std::string>
146 {
147  static const char *name() { return "const string"; }
148  static bool has_null() { return false; }
149  static bool is_null(const std::string &) { return false; }
150  static const std::string null()
151  { internal::throw_null_conversion(name()); return std::string(); }
152  static const std::string to_string(const std::string &Obj) { return Obj; }
153 };
154 
155 template<> struct PQXX_LIBEXPORT string_traits<std::stringstream>
156 {
157  static const char *name() { return "stringstream"; }
158  static bool has_null() { return false; }
159  static bool is_null(const std::stringstream &) { return false; }
160  static std::stringstream null()
161  {
163  // No, dear compiler, we don't need a return here.
164  throw 0;
165  }
166  static void from_string(const char Str[], std::stringstream &Obj)
167  { Obj.clear(); Obj << Str; }
168  static std::string to_string(const std::stringstream &Obj)
169  { return Obj.str(); }
170 };
171 
172 
173 // TODO: Implement date conversions
174 
176 
188 template<typename T>
189  inline void from_string(const char Str[], T &Obj)
190 {
191  if (!Str)
192  throw std::runtime_error("Attempt to read NULL string");
194 }
195 
196 
198 
204 template<typename T> inline void from_string(const char Str[], T &Obj, size_t)
205 {
206  return from_string(Str, Obj);
207 }
208 
209 template<>
210  inline void from_string<std::string>(const char Str[],
211  std::string &Obj,
212  size_t len) //[t0]
213 {
214  if (!Str)
215  throw std::runtime_error("Attempt to read NULL string");
216  Obj.assign(Str, len);
217 }
218 
219 template<typename T>
220  inline void from_string(const std::string &Str, T &Obj) //[t45]
221  { from_string(Str.c_str(), Obj); }
222 
223 template<typename T>
224  inline void from_string(const std::stringstream &Str, T &Obj) //[t0]
225  { from_string(Str.str(), Obj); }
226 
227 template<> inline void
228 from_string(const std::string &Str, std::string &Obj) //[t46]
229  { Obj = Str; }
230 
231 
232 namespace internal
233 {
235 inline int digit_to_number(char c) PQXX_NOEXCEPT { return c-'0'; }
236 inline char number_to_digit(int i) PQXX_NOEXCEPT
237  { return static_cast<char>(i+'0'); }
238 } // namespace pqxx::internal
239 
240 
242 
246 template<typename T> inline std::string to_string(const T &Obj)
247  { return string_traits<T>::to_string(Obj); }
248 
250 
251 } // namespace pqxx
252 
253 #endif
254 
static const char * name()
Definition: strconv.hxx:113
static const char * null()
Definition: strconv.hxx:116
static std::string to_string(char *Obj)
Definition: strconv.hxx:107
static const char * name()
Definition: strconv.hxx:126
static const char * name()
Definition: strconv.hxx:99
int digit_to_number(char c) PQXX_NOEXCEPT
Compute numeric value of given textual digit (assuming that it is a digit)
Definition: strconv.hxx:235
static bool has_null()
Definition: strconv.hxx:100
static std::string to_string(const std::stringstream &Obj)
Definition: strconv.hxx:168
static std::string to_string(const char *Obj)
Definition: strconv.hxx:93
static bool is_null(const std::string &)
Definition: strconv.hxx:149
static bool is_null(const std::stringstream &)
Definition: strconv.hxx:159
#define PQXX_DECLARE_STRING_TRAITS_SPECIALIZATION(T)
Definition: strconv.hxx:55
static const char * name()
Definition: strconv.hxx:157
static const char * null()
Definition: strconv.hxx:129
static bool is_null(const char t[])
Definition: strconv.hxx:128
static std::string null()
Definition: strconv.hxx:139
static const char * null()
Definition: strconv.hxx:102
static const char * name()
Definition: strconv.hxx:136
static std::string to_string(const char Obj[])
Definition: strconv.hxx:117
static const std::string to_string(const std::string &Obj)
Definition: strconv.hxx:152
static bool is_null(const std::string &)
Definition: strconv.hxx:138
static void from_string(const char Str[], const char *&Obj)
Definition: strconv.hxx:92
static const std::string null()
Definition: strconv.hxx:150
PQXX_NORETURN void throw_null_conversion(const std::string &type)
Throw exception for attempt to convert null to given type.
Definition: strconv.cxx:302
static const char * null()
Definition: strconv.hxx:91
static bool has_null()
Definition: strconv.hxx:148
static bool is_null(const char t[])
Definition: strconv.hxx:115
static const char * name()
Definition: strconv.hxx:88
static std::stringstream null()
Definition: strconv.hxx:160
static const char * name()
Definition: strconv.hxx:147
Traits class for use in string conversions.
Definition: strconv.hxx:46
static bool has_null()
Definition: strconv.hxx:127
std::string to_string(const T &Obj)
Convert built-in type to a readable string that PostgreSQL will understand.
Definition: strconv.hxx:246
static std::string to_string(const std::string &Obj)
Definition: strconv.hxx:142
char number_to_digit(int i) PQXX_NOEXCEPT
Definition: strconv.hxx:236
static std::string to_string(const char Obj[])
Definition: strconv.hxx:130
static void from_string(const char Str[], std::string &Obj)
Definition: strconv.hxx:141
static void from_string(const char Str[], std::stringstream &Obj)
Definition: strconv.hxx:166
static bool has_null()
Definition: strconv.hxx:114
static bool is_null(const char *t)
Definition: strconv.hxx:90
static bool has_null()
Definition: strconv.hxx:158
void from_string(const field &F, T &Obj)
Convert a field&#39;s string contents to another type.
Definition: result.hxx:459
static bool is_null(const char *t)
Definition: strconv.hxx:101
void from_string(const char Str[], T &Obj)
Attempt to convert postgres-generated string to given built-in type.
Definition: strconv.hxx:189
static bool has_null()
Definition: strconv.hxx:89
static bool has_null()
Definition: strconv.hxx:137
std::string to_string(const field &Obj)
Convert a field to a string.
Definition: result.hxx:464