libpqxx  7.1.2
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 
123 template<typename TYPE> struct string_traits
124 {
126 
143  [[nodiscard]] static inline zview
144  to_buf(char *begin, char *end, TYPE const &value);
145 
147 
154  static inline char *into_buf(char *begin, char *end, TYPE const &value);
155 
157 
160  [[nodiscard]] static inline TYPE from_string(std::string_view text);
161 
163 
167  [[nodiscard]] static inline std::size_t
168  size_buffer(TYPE const &value) noexcept;
169 };
170 
171 
173 template<typename ENUM>
174 struct nullness<ENUM, std::enable_if_t<std::is_enum_v<ENUM>>> : no_null<ENUM>
175 {};
176 } // namespace pqxx
177 
178 
179 namespace pqxx::internal
180 {
182 
191 template<typename ENUM> struct enum_traits
192 {
193  using impl_type = std::underlying_type_t<ENUM>;
195 
196  [[nodiscard]] static constexpr zview
197  to_buf(char *begin, char *end, ENUM const &value)
198  {
199  return impl_traits::to_buf(begin, end, static_cast<impl_type>(value));
200  }
201 
202  static constexpr char *into_buf(char *begin, char *end, ENUM const &value)
203  {
204  return impl_traits::into_buf(begin, end, static_cast<impl_type>(value));
205  }
206 
207  [[nodiscard]] static ENUM from_string(std::string_view text)
208  {
209  return static_cast<ENUM>(impl_traits::from_string(text));
210  }
211 
212  [[nodiscard]] static std::size_t size_buffer(ENUM const &value) noexcept
213  {
214  return impl_traits::size_buffer(static_cast<impl_type>(value));
215  }
216 };
217 } // namespace pqxx::internal
218 
219 
221 
232 #define PQXX_DECLARE_ENUM_CONVERSION(ENUM) \
233  template<> struct string_traits<ENUM> : pqxx::internal::enum_traits<ENUM> \
234  {}; \
235  template<> inline std::string const type_name<ENUM> { #ENUM }
236 
237 
238 namespace pqxx
239 {
241 
253 template<typename TYPE>
254 [[nodiscard]] inline TYPE from_string(std::string_view text)
255 {
257 }
258 
259 
261 
267 template<>
268 [[nodiscard]] inline std::string_view from_string(std::string_view text)
269 {
270  return text;
271 }
272 
273 
275 
282 template<typename T> inline void from_string(std::string_view text, T &value)
283 {
284  value = from_string<T>(text);
285 }
286 
287 
289 
294 template<typename TYPE> inline std::string to_string(TYPE const &value);
295 
296 
298 
301 template<typename TYPE>
302 inline void into_string(TYPE const &value, std::string &out);
303 
304 
306 template<typename TYPE>
307 [[nodiscard]] inline bool is_null(TYPE const &value) noexcept
308 {
309  return nullness<TYPE>::is_null(value);
310 }
311 
312 
314 
320 template<typename T> inline constexpr bool is_sql_array{false};
321 
322 
324 template<typename T> inline constexpr char array_separator{','};
326 } // namespace pqxx
327 
328 
329 #include "pqxx/internal/conversions.hxx"
330 #endif
pqxx::internal::demangle_type_name
std::string demangle_type_name(char const[])
Attempt to demangle std::type_info::name() to something human-readable.
Definition: strconv.cxx:223
pqxx::nullness
Traits describing a type's "null value," if any.
Definition: strconv.hxx:87
pqxx::type_name
std::string const type_name
A human-readable name for a type, used in error messages and such.
Definition: strconv.hxx:76
pqxx::conversion_error
Value conversion failed, e.g. when converting "Hello" to int.
Definition: except.hxx:180
pqxx::internal::to_string_float
std::string to_string_float(T value)
Floating-point implementations for pqxx::to_string().
Definition: strconv.cxx:607
pqxx::internal::enum_traits::into_buf
static constexpr char * into_buf(char *begin, char *end, ENUM const &value)
Definition: strconv.hxx:202
pqxx::nullness::is_null
static bool is_null(TYPE const &value)
Is value a null?
pqxx
The home of all libpqxx classes, functions, templates, etc.
Definition: array.hxx:26
pqxx::string_traits::from_string
static TYPE from_string(std::string_view text)
Parse a string representation of a TYPE value.
Definition: strconv.cxx:685
pqxx::is_null
bool is_null(TYPE const &value) noexcept
Is value null?
Definition: strconv.hxx:307
pqxx::to_string
std::string to_string(field const &value)
Convert a field to a string.
Definition: result.cxx:498
pqxx::internal::enc_group
encoding_group enc_group(int libpq_enc_id)
Definition: encodings.cxx:585
pqxx::internal::enum_traits::size_buffer
static std::size_t size_buffer(ENUM const &value) noexcept
Definition: strconv.hxx:212
pqxx::internal::state_buffer_overrun
std::string state_buffer_overrun(int have_bytes, int need_bytes)
Definition: strconv.cxx:247
pqxx::internal::throw_null_conversion
void throw_null_conversion(std::string const &type)
Definition: strconv.cxx:241
pqxx::nullness::has_null
static bool has_null
Does this type have a null value?
Definition: strconv.hxx:89
pqxx::internal
Private namespace for libpqxx's internal use; do not access.
Definition: connection.hxx:61
pqxx::no_null::always_null
static constexpr bool always_null
Definition: strconv.hxx:111
pqxx::cursor_base::difference_type
result_difference_type difference_type
Definition: cursor.hxx:44
pqxx::zview
Marker-type wrapper: zero-terminated std::string_view.
Definition: zview.hxx:33
pqxx::result
Result set containing data returned by a query or command.
Definition: result.hxx:71
pqxx::string_traits
Traits class for use in string conversions.
Definition: strconv.hxx:124
pqxx::into_string
void into_string(TYPE const &value, std::string &out)
Convert a value to a readable string that PostgreSQL will understand.
pqxx::internal::enum_traits::from_string
static ENUM from_string(std::string_view text)
Definition: strconv.hxx:207
pqxx::no_null::is_null
static constexpr bool is_null(TYPE const &) noexcept
Definition: strconv.hxx:112
pqxx::internal::enum_traits::impl_type
std::underlying_type_t< ENUM > impl_type
Definition: strconv.hxx:193
pqxx::array_separator
constexpr char array_separator
Element separator between SQL array elements of this type.
Definition: strconv.hxx:324
pqxx::no_null::has_null
static constexpr bool has_null
Definition: strconv.hxx:110
pqxx::nullness::always_null
static bool always_null
Is this type always null?
Definition: strconv.hxx:92
pqxx::internal::to_dumb_stringstream
std::string to_dumb_stringstream(dumb_stringstream< F > &s, F value)
Definition: strconv.cxx:597
pqxx::from_string
T from_string(field const &value)
Convert a field's value to type T.
Definition: field.hxx:360
pqxx::internal::enum_traits::to_buf
static constexpr zview to_buf(char *begin, char *end, ENUM const &value)
Definition: strconv.hxx:197
pqxx::string_traits::to_buf
static zview to_buf(char *begin, char *end, TYPE const &value)
Return a string_view representing value, plus terminating zero.
pqxx::internal::enum_traits
Helper class for defining enum conversions.
Definition: strconv.hxx:192
pqxx::is_sql_array
constexpr bool is_sql_array
Does this type translate to an SQL array?
Definition: strconv.hxx:320
pqxx::string_traits::size_buffer
static std::size_t size_buffer(TYPE const &value) noexcept
Estimate how much buffer space is needed to represent value.
pqxx::no_null
Nullness traits describing a type which does not have a null value.
Definition: strconv.hxx:109
pqxx::conversion_overrun
Could not convert value to string: not enough buffer space.
Definition: except.hxx:187
pqxx::string_traits::into_buf
static char * into_buf(char *begin, char *end, TYPE const &value)
Write value's string representation into buffer at begin.