libpqxx  7.0.0
strconv.hxx
1 /* String conversion definitions.
2  *
3  * DO NOT INCLUDE THIS FILE DIRECTLY; include pqxx/stringconv instead.
4  *
5  * Copyright (c) 2000-2020, Jeroen T. Vermeulen.
6  *
7  * See COPYING for copyright license. If you did not receive a file called
8  * COPYING with this source code, please notify the distributor of this
9  * mistake, or contact the author.
10  */
11 #ifndef PQXX_H_STRINGCONV
12 #define PQXX_H_STRINGCONV
13 
14 #include "pqxx/compiler-public.hxx"
15 
16 #include <algorithm>
17 #include <cstring>
18 #include <limits>
19 #include <sstream>
20 #include <stdexcept>
21 #include <typeinfo>
22 
23 #if __has_include(<charconv>)
24 # include <charconv>
25 #endif
26 
27 #include "pqxx/except.hxx"
28 #include "pqxx/util.hxx"
29 #include "pqxx/zview.hxx"
30 
31 
32 namespace pqxx::internal
33 {
35 PQXX_LIBEXPORT std::string demangle_type_name(char const[]);
36 } // namespace pqxx::internal
37 
38 
39 namespace pqxx
40 {
65 
67 
75 template<typename TYPE>
76 std::string const type_name{internal::demangle_type_name(typeid(TYPE).name())};
77 
78 
80 
86 template<typename TYPE, typename ENABLE = void> struct nullness
87 {
89  static bool has_null;
90 
92  static bool is_null(TYPE const &value);
93 
95 
100  [[nodiscard]] static TYPE null();
101 };
102 
103 
105 template<typename TYPE> struct no_null
106 {
107  static constexpr bool has_null = false;
108  [[nodiscard]] static constexpr bool is_null(TYPE const &) noexcept
109  {
110  return false;
111  }
112 };
113 
114 
116 
119 template<typename TYPE> struct string_traits
120 {
122 
139  [[nodiscard]] static inline zview
140  to_buf(char *begin, char *end, TYPE const &value);
141 
143 
150  static inline char *into_buf(char *begin, char *end, TYPE const &value);
151 
152  [[nodiscard]] static inline TYPE from_string(std::string_view text);
153 
155 
159  [[nodiscard]] static inline size_t size_buffer(TYPE const &value);
160 };
161 
162 
164 template<typename ENUM>
165 struct nullness<ENUM, std::enable_if_t<std::is_enum_v<ENUM>>> : no_null<ENUM>
166 {};
167 } // namespace pqxx
168 
169 
170 namespace pqxx::internal
171 {
173 
182 template<typename ENUM> struct enum_traits
183 {
184  using impl_type = std::underlying_type_t<ENUM>;
186 
187  [[nodiscard]] static constexpr zview
188  to_buf(char *begin, char *end, ENUM const &value)
189  {
190  return impl_traits::to_buf(begin, end, static_cast<impl_type>(value));
191  }
192 
193  static constexpr char *into_buf(char *begin, char *end, ENUM const &value)
194  {
195  return impl_traits::into_buf(begin, end, static_cast<impl_type>(value));
196  }
197 
198  [[nodiscard]] static ENUM from_string(std::string_view text)
199  {
200  return static_cast<ENUM>(impl_traits::from_string(text));
201  }
202 
203  [[nodiscard]] static size_t size_buffer(ENUM const &value)
204  {
205  return impl_traits::size_buffer(static_cast<impl_type>(value));
206  }
207 };
208 } // namespace pqxx::internal
209 
210 
212 
223 #define PQXX_DECLARE_ENUM_CONVERSION(ENUM) \
224  template<> struct string_traits<ENUM> : pqxx::internal::enum_traits<ENUM> \
225  {}; \
226  template<> std::string const type_name<ENUM> { #ENUM }
227 
228 
229 namespace pqxx
230 {
232 
244 template<typename T>[[nodiscard]] inline T from_string(std::string_view text)
245 {
246  return string_traits<T>::from_string(text);
247 }
248 
249 
251 
258 template<typename T> inline void from_string(std::string_view text, T &value)
259 {
260  value = from_string<T>(text);
261 }
262 
263 
265 
270 template<typename TYPE> inline std::string to_string(TYPE const &value);
271 
272 
274 
277 template<typename TYPE>
278 inline void into_string(TYPE const &value, std::string &out);
279 
280 
282 template<typename TYPE>[[nodiscard]] inline bool is_null(TYPE const &value)
283 {
284  if constexpr (nullness<TYPE>::has_null)
285  {
286  return nullness<TYPE>::is_null(value);
287  }
288  else
289  {
290  ignore_unused(value);
291  return false;
292  }
293 }
294 
295 
297 
303 template<typename T> inline constexpr bool is_sql_array{false};
304 
305 
307 template<typename T> inline constexpr char array_separator{','};
309 } // namespace pqxx
310 
311 
312 #include "pqxx/internal/conversions.hxx"
313 #endif
static TYPE from_string(std::string_view text)
Definition: strconv.cxx:678
Helper class for defining enum conversions.
Definition: strconv.hxx:182
void from_string(std::string_view text, T &value)
Attempt to convert postgres-generated string to given built-in object.
Definition: strconv.hxx:258
Nullness traits describing a type which does not have a null value.
Definition: strconv.hxx:105
Private namespace for libpqxx&#39;s internal use; do not access.
Definition: connection.hxx:59
bool is_null(TYPE const &value)
Is value null?
Definition: strconv.hxx:282
static constexpr char * into_buf(char *begin, char *end, ENUM const &value)
Definition: strconv.hxx:193
void into_string(TYPE const &value, std::string &out)
Convert a value to a readable string that PostgreSQL will understand.
T from_string(field const &value)
Convert a field&#39;s string contents to another type.
Definition: field.hxx:355
constexpr bool is_sql_array
Does this type translate to an SQL array?
Definition: strconv.hxx:303
static bool is_null(TYPE const &value)
Is value a null?
std::string to_string(field const &value)
Convert a field to a string.
Definition: result.cxx:478
constexpr char array_separator
Element separator between SQL array elements of this type.
Definition: strconv.hxx:307
std::underlying_type_t< ENUM > impl_type
Definition: strconv.hxx:184
static size_t size_buffer(ENUM const &value)
Definition: strconv.hxx:203
The home of all libpqxx classes, functions, templates, etc.
Definition: array.hxx:25
static constexpr zview to_buf(char *begin, char *end, ENUM const &value)
Definition: strconv.hxx:188
std::string const type_name
A human-readable name for a type, used in error messages and such.
Definition: strconv.hxx:76
static constexpr bool is_null(TYPE const &) noexcept
Definition: strconv.hxx:108
static ENUM from_string(std::string_view text)
Definition: strconv.hxx:198
STL namespace.
Traits describing a type&#39;s "null value," if any.
Definition: strconv.hxx:86
static bool has_null
Does this type have a null value?
Definition: strconv.hxx:89
void ignore_unused(T &&)
Suppress compiler warning about an unused item.
Definition: util.hxx:43
Marker-type wrapper: zero-terminated std::string_view.
Definition: zview.hxx:32
std::string demangle_type_name(char const [])
Attempt to demangle std::type_info::name() to something human-readable.
Definition: strconv.cxx:223
Traits class for use in string conversions.
Definition: strconv.hxx:119