libpqxx  7.2.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 always_null;
93 
95  static bool is_null(TYPE const &value);
96 
98 
103  [[nodiscard]] static TYPE null();
104 };
105 
106 
108 template<typename TYPE> struct no_null
109 {
110  static constexpr bool has_null = false;
111  static constexpr bool always_null = false;
112  [[nodiscard]] static constexpr bool is_null(TYPE const &) noexcept
113  {
114  return false;
115  }
116 };
117 
118 
120 
126 template<typename TYPE> struct string_traits
127 {
129 
146  [[nodiscard]] static inline zview
147  to_buf(char *begin, char *end, TYPE const &value);
148 
150 
157  static inline char *into_buf(char *begin, char *end, TYPE const &value);
158 
160 
163  [[nodiscard]] static inline TYPE from_string(std::string_view text);
164 
166 
170  [[nodiscard]] static inline std::size_t
171  size_buffer(TYPE const &value) noexcept;
172 };
173 
174 
176 template<typename ENUM>
177 struct nullness<ENUM, std::enable_if_t<std::is_enum_v<ENUM>>> : no_null<ENUM>
178 {};
179 } // namespace pqxx
180 
181 
182 namespace pqxx::internal
183 {
185 
194 template<typename ENUM> struct enum_traits
195 {
196  using impl_type = std::underlying_type_t<ENUM>;
198 
199  [[nodiscard]] static constexpr zview
200  to_buf(char *begin, char *end, ENUM const &value)
201  {
202  return impl_traits::to_buf(begin, end, static_cast<impl_type>(value));
203  }
204 
205  static constexpr char *into_buf(char *begin, char *end, ENUM const &value)
206  {
207  return impl_traits::into_buf(begin, end, static_cast<impl_type>(value));
208  }
209 
210  [[nodiscard]] static ENUM from_string(std::string_view text)
211  {
212  return static_cast<ENUM>(impl_traits::from_string(text));
213  }
214 
215  [[nodiscard]] static std::size_t size_buffer(ENUM const &value) noexcept
216  {
217  return impl_traits::size_buffer(static_cast<impl_type>(value));
218  }
219 };
220 } // namespace pqxx::internal
221 
222 
224 
235 #define PQXX_DECLARE_ENUM_CONVERSION(ENUM) \
236  template<> struct string_traits<ENUM> : pqxx::internal::enum_traits<ENUM> \
237  {}; \
238  template<> inline std::string const type_name<ENUM> { #ENUM }
239 
240 
241 namespace pqxx
242 {
244 
256 template<typename TYPE>
257 [[nodiscard]] inline TYPE from_string(std::string_view text)
258 {
260 }
261 
262 
264 
270 template<>
271 [[nodiscard]] inline std::string_view from_string(std::string_view text)
272 {
273  return text;
274 }
275 
276 
278 
285 template<typename T> inline void from_string(std::string_view text, T &value)
286 {
287  value = from_string<T>(text);
288 }
289 
290 
292 
297 template<typename TYPE> inline std::string to_string(TYPE const &value);
298 
299 
301 
304 template<typename TYPE>
305 inline void into_string(TYPE const &value, std::string &out);
306 
307 
309 template<typename TYPE>
310 [[nodiscard]] inline bool is_null(TYPE const &value) noexcept
311 {
312  return nullness<TYPE>::is_null(value);
313 }
314 
315 
317 
320 template<typename TYPE>
321 [[nodiscard]] inline std::size_t size_buffer(TYPE const &value) noexcept
322 {
323  return string_traits<TYPE>::size_buffer(value);
324 }
325 
326 
328 
334 template<typename TYPE> inline constexpr bool is_sql_array{false};
335 
336 
338 
350 template<typename TYPE> inline constexpr bool is_unquoted_safe{false};
351 
352 
354 template<typename T> inline constexpr char array_separator{','};
356 } // namespace pqxx
357 
358 
359 #include "pqxx/internal/conversions.hxx"
360 #endif
static TYPE from_string(std::string_view text)
Parse a string representation of a TYPE value.
Definition: strconv.cxx:718
void from_string(std::string_view text, T &value)
Attempt to convert postgres-generated string to given built-in object.
Definition: strconv.hxx:285
Traits class for use in string conversions.
Definition: strconv.hxx:126
Private namespace for libpqxx&#39;s internal use; do not access.
Definition: composite.hxx:70
Helper class for defining enum conversions.
Definition: strconv.hxx:194
static constexpr char * into_buf(char *begin, char *end, ENUM const &value)
Definition: strconv.hxx:205
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 value to type T.
Definition: field.hxx:377
constexpr bool is_sql_array
Does this type translate to an SQL array?
Definition: strconv.hxx:334
static bool is_null(TYPE const &value)
Is value a null?
constexpr bool is_unquoted_safe
Can we use this type in arrays and composite types without quoting them?
Definition: strconv.hxx:350
std::string to_string(field const &value)
Convert a field to a string.
Definition: result.cxx:498
constexpr char array_separator
Element separator between SQL array elements of this type.
Definition: strconv.hxx:354
static bool always_null
Is this type always null?
Definition: strconv.hxx:92
std::underlying_type_t< ENUM > impl_type
Definition: strconv.hxx:196
static std::size_t size_buffer(TYPE const &value) noexcept
Estimate how much buffer space is needed to represent value.
static std::size_t size_buffer(ENUM const &value) noexcept
Definition: strconv.hxx:215
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:200
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:112
bool is_null(TYPE const &value) noexcept
Is value null?
Definition: strconv.hxx:310
static ENUM from_string(std::string_view text)
Definition: strconv.hxx:210
STL namespace.
Marker-type wrapper: zero-terminated std::string_view.
Definition: zview.hxx:36
static bool has_null
Does this type have a null value?
Definition: strconv.hxx:89
std::size_t size_buffer(TYPE const &value) noexcept
Estimate how much buffer space is needed to represent value as a string.
Definition: strconv.hxx:321
Nullness traits describing a type which does not have a null value.
Definition: strconv.hxx:108
std::string demangle_type_name(char const [])
Attempt to demangle std::type_info::name() to something human-readable.
Definition: strconv.cxx:223
Traits describing a type&#39;s "null value," if any.
Definition: strconv.hxx:86