libpqxx
strconv.hxx
1 
11 #ifndef PQXX_H_STRINGCONV
12 #define PQXX_H_STRINGCONV
13 
14 #include "pqxx/compiler-public.hxx"
15 
16 #include <sstream>
17 #include <stdexcept>
18 
19 
20 namespace pqxx
21 {
22 
34 
36 
39 template<typename T> struct string_traits {};
40 
41 namespace internal
42 {
44 [[noreturn]] PQXX_LIBEXPORT void throw_null_conversion(
45  const std::string &type);
46 } // namespace pqxx::internal
47 
48 
56 #define PQXX_DECLARE_STRING_TRAITS_SPECIALIZATION(T) \
57 template<> struct PQXX_LIBEXPORT string_traits<T> \
58 { \
59  static constexpr const char *name() noexcept { return #T; } \
60  static constexpr bool has_null() noexcept { return false; } \
61  static bool is_null(T) { return false; } \
62  [[noreturn]] static T null() \
63  { internal::throw_null_conversion(name()); } \
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 constexpr const char *name() noexcept { return "const char *"; }
89  static constexpr bool has_null() noexcept { return true; }
90  static bool is_null(const char *t) { return !t; }
91  static const char *null() { return nullptr; }
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 constexpr const char *name() noexcept { return "char *"; }
100  static constexpr bool has_null() noexcept { return true; }
101  static bool is_null(const char *t) { return !t; }
102  static const char *null() { return nullptr; }
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 constexpr const char *name() noexcept { return "char[]"; }
114  static constexpr bool has_null() noexcept { return true; }
115  static bool is_null(const char t[]) { return !t; }
116  static const char *null() { return nullptr; }
117  static std::string to_string(const char Obj[]) { return Obj; }
118 };
119 
120 template<> struct PQXX_LIBEXPORT string_traits<std::string>
121 {
122  static constexpr const char *name() noexcept { return "string"; }
123  static constexpr bool has_null() noexcept { return false; }
124  static bool is_null(const std::string &) { return false; }
125  static std::string null()
126  { internal::throw_null_conversion(name()); return std::string(); }
127  static void from_string(const char Str[], std::string &Obj) { Obj=Str; }
128  static std::string to_string(const std::string &Obj) { return Obj; }
129 };
130 
131 template<> struct PQXX_LIBEXPORT string_traits<const std::string>
132 {
133  static constexpr const char *name() noexcept { return "const string"; }
134  static constexpr bool has_null() noexcept { return false; }
135  static bool is_null(const std::string &) { return false; }
136  static const std::string null()
137  { internal::throw_null_conversion(name()); return std::string(); }
138  static const std::string to_string(const std::string &Obj) { return Obj; }
139 };
140 
141 template<> struct PQXX_LIBEXPORT string_traits<std::stringstream>
142 {
143  static constexpr const char *name() noexcept { return "stringstream"; }
144  static constexpr bool has_null() noexcept { return false; }
145  static bool is_null(const std::stringstream &) { return false; }
146  static std::stringstream null()
147  {
149  // No, dear compiler, we don't need a return here.
150  throw 0;
151  }
152  static void from_string(const char Str[], std::stringstream &Obj)
153  { Obj.clear(); Obj << Str; }
154  static std::string to_string(const std::stringstream &Obj)
155  { return Obj.str(); }
156 };
157 
158 
159 // TODO: Implement date conversions
160 
162 
174 template<typename T>
175  inline void from_string(const char Str[], T &Obj)
176 {
177  if (!Str) throw std::runtime_error("Attempt to read null string");
179 }
180 
181 
183 
189 template<typename T> inline void from_string(const char Str[], T &Obj, size_t)
190 {
191  return from_string(Str, Obj);
192 }
193 
194 template<>
195  inline void from_string<std::string>( //[t00]
196  const char Str[],
197  std::string &Obj,
198  size_t len)
199 {
200  if (!Str) throw std::runtime_error("Attempt to read null string");
201  Obj.assign(Str, len);
202 }
203 
204 template<typename T>
205  inline void from_string(const std::string &Str, T &Obj) //[t45]
206  { from_string(Str.c_str(), Obj); }
207 
208 template<typename T>
209  inline void from_string(const std::stringstream &Str, T &Obj) //[t00]
210  { from_string(Str.str(), Obj); }
211 
212 template<> inline void
213 from_string(const std::string &Str, std::string &Obj) //[t46]
214  { Obj = Str; }
215 
216 
217 namespace internal
218 {
220 inline int digit_to_number(char c) noexcept { return c-'0'; }
221 inline char number_to_digit(int i) noexcept
222  { return static_cast<char>(i+'0'); }
223 } // namespace pqxx::internal
224 
225 
227 
231 template<typename T> inline std::string to_string(const T &Obj)
232  { return string_traits<T>::to_string(Obj); }
233 
235 
236 } // namespace pqxx
237 
238 #endif
static constexpr const char * name() noexcept
Definition: strconv.hxx:143
static std::string to_string(char *Obj)
Definition: strconv.hxx:107
static constexpr const char * name() noexcept
Definition: strconv.hxx:113
static bool is_null(const std::stringstream &)
Definition: strconv.hxx:145
static std::string to_string(const char Obj[])
Definition: strconv.hxx:117
std::string to_string(const T &Obj)
Convert built-in type to a readable string that PostgreSQL will understand.
Definition: strconv.hxx:231
static const char * null()
Definition: strconv.hxx:102
The home of all libpqxx classes, functions, templates, etc.
Definition: array.hxx:22
char number_to_digit(int i) noexcept
Definition: strconv.hxx:221
void from_string(const std::string &Str, std::string &Obj)
Definition: strconv.hxx:213
static void from_string(const char Str[], const char *&Obj)
Definition: strconv.hxx:92
static constexpr const char * name() noexcept
Definition: strconv.hxx:88
static bool is_null(const char t[])
Definition: strconv.hxx:115
static std::string null()
Definition: strconv.hxx:125
static constexpr bool has_null() noexcept
Definition: strconv.hxx:89
int digit_to_number(char c) noexcept
Compute numeric value of given textual digit (assuming that it is a digit)
Definition: strconv.hxx:220
void throw_null_conversion(const std::string &type)
Throw exception for attempt to convert null to given type.
Definition: strconv.cxx:266
static const std::string null()
Definition: strconv.hxx:136
static void from_string(const char Str[], std::stringstream &Obj)
Definition: strconv.hxx:152
STL namespace.
static constexpr const char * name() noexcept
Definition: strconv.hxx:122
static std::stringstream null()
Definition: strconv.hxx:146
void from_string(const field &F, T &Obj)
Convert a field&#39;s string contents to another type.
Definition: field.hxx:347
static const std::string to_string(const std::string &Obj)
Definition: strconv.hxx:138
static bool is_null(const char *t)
Definition: strconv.hxx:90
static constexpr const char * name() noexcept
Definition: strconv.hxx:99
static bool is_null(const std::string &)
Definition: strconv.hxx:124
static const char * null()
Definition: strconv.hxx:116
static void from_string(const char Str[], std::string &Obj)
Definition: strconv.hxx:127
static std::string to_string(const char *Obj)
Definition: strconv.hxx:93
std::string to_string(const field &Obj)
Convert a field to a string.
Definition: result.cxx:434
static const char * null()
Definition: strconv.hxx:91
static constexpr bool has_null() noexcept
Definition: strconv.hxx:134
static constexpr bool has_null() noexcept
Definition: strconv.hxx:100
static bool is_null(const std::string &)
Definition: strconv.hxx:135
static constexpr bool has_null() noexcept
Definition: strconv.hxx:123
static constexpr const char * name() noexcept
Definition: strconv.hxx:133
static constexpr bool has_null() noexcept
Definition: strconv.hxx:144
static std::string to_string(const std::string &Obj)
Definition: strconv.hxx:128
static constexpr bool has_null() noexcept
Definition: strconv.hxx:114
#define PQXX_DECLARE_STRING_TRAITS_SPECIALIZATION(T)
Definition: strconv.hxx:56
Traits class for use in string conversions.
Definition: strconv.hxx:39
static std::string to_string(const std::stringstream &Obj)
Definition: strconv.hxx:154
static bool is_null(const char *t)
Definition: strconv.hxx:101