libpqxx  7.7.3
util.hxx
1 /* Various utility definitions for libpqxx.
2  *
3  * DO NOT INCLUDE THIS FILE DIRECTLY; include pqxx/util instead.
4  *
5  * Copyright (c) 2000-2022, 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_UTIL
12 #define PQXX_H_UTIL
13 
14 #if !defined(PQXX_HEADER_PRE)
15 # error "Include libpqxx headers as <pqxx/header>, not <pqxx/header.hxx>."
16 #endif
17 
18 #include <cctype>
19 #include <cstdio>
20 #include <functional>
21 #include <iterator>
22 #include <limits>
23 #include <memory>
24 #include <stdexcept>
25 #include <string>
26 #include <string_view>
27 #include <type_traits>
28 #include <typeinfo>
29 #include <utility>
30 #include <vector>
31 
32 #if __has_include(<version>)
33 # include <version>
34 #endif
35 
36 #include "pqxx/except.hxx"
37 #include "pqxx/internal/encodings.hxx"
38 #include "pqxx/types.hxx"
39 #include "pqxx/version.hxx"
40 
41 
43 namespace pqxx
44 {}
45 
46 #include <pqxx/internal/libpq-forward.hxx>
47 
48 
50 namespace pqxx::internal
51 {
52 
53 // C++20: Retire wrapper.
55 template<typename LEFT, typename RIGHT>
56 inline constexpr bool cmp_less(LEFT lhs, RIGHT rhs) noexcept
57 {
58 #if defined(PQXX_HAVE_CMP)
59  return std::cmp_less(lhs, rhs);
60 #else
61  // We need a variable just because lgtm.com gives off a false positive
62  // warning when we compare the values directly. It considers that a
63  // "self-comparison."
64  constexpr bool left_signed{std::is_signed_v<LEFT>};
65  if constexpr (left_signed == std::is_signed_v<RIGHT>)
66  return lhs < rhs;
67  else if constexpr (std::is_signed_v<LEFT>)
68  return (lhs <= 0) ? true : (std::make_unsigned_t<LEFT>(lhs) < rhs);
69  else
70  return (rhs <= 0) ? false : (lhs < std::make_unsigned_t<RIGHT>(rhs));
71 #endif
72 }
73 
74 
75 // C++20: Retire wrapper.
77 template<typename LEFT, typename RIGHT>
78 inline constexpr bool cmp_greater(LEFT lhs, RIGHT rhs) noexcept
79 {
80 #if defined(PQXX_HAVE_CMP)
81  return std::cmp_greater(lhs, rhs);
82 #else
83  return cmp_less(rhs, lhs);
84 #endif
85 }
86 
87 
88 // C++20: Retire wrapper.
90 template<typename LEFT, typename RIGHT>
91 inline constexpr bool cmp_less_equal(LEFT lhs, RIGHT rhs) noexcept
92 {
93 #if defined(PQXX_HAVE_CMP)
94  return std::cmp_less_equal(lhs, rhs);
95 #else
96  return not cmp_less(rhs, lhs);
97 #endif
98 }
99 
100 
101 // C++20: Retire wrapper.
103 template<typename LEFT, typename RIGHT>
104 inline constexpr bool cmp_greater_equal(LEFT lhs, RIGHT rhs) noexcept
105 {
106 #if defined(PQXX_HAVE_CMP)
107  return std::cmp_greater_equal(lhs, rhs);
108 #else
109  return not cmp_less(lhs, rhs);
110 #endif
111 }
112 
113 
115 
118 [[nodiscard]] inline std::string cat2(std::string_view x, std::string_view y)
119 {
120  std::string buf;
121  auto const xs{std::size(x)}, ys{std::size(y)};
122  buf.resize(xs + ys);
123  x.copy(std::data(buf), xs);
124  y.copy(std::data(buf) + xs, ys);
125  return buf;
126 }
127 } // namespace pqxx::internal
128 
129 
130 namespace pqxx
131 {
132 using namespace std::literals;
133 
135 template<typename... T> inline constexpr void ignore_unused(T &&...) noexcept
136 {}
137 
138 
140 
143 template<typename TO, typename FROM>
144 inline TO check_cast(FROM value, std::string_view description)
145 {
146  static_assert(std::is_arithmetic_v<FROM>);
147  static_assert(std::is_arithmetic_v<TO>);
148  static_assert(std::is_integral_v<FROM> == std::is_integral_v<TO>);
149 
150  // The rest of this code won't quite work for bool, but bool is trivially
151  // convertible to other arithmetic types as far as I can see.
152  if constexpr (std::is_same_v<FROM, bool>)
153  return static_cast<TO>(value);
154 
155  // Depending on our "if constexpr" conditions, this parameter may not be
156  // needed. Some compilers will warn.
157  ignore_unused(description);
158 
159  using from_limits = std::numeric_limits<decltype(value)>;
160  using to_limits = std::numeric_limits<TO>;
161  if constexpr (std::is_signed_v<FROM>)
162  {
163  if constexpr (std::is_signed_v<TO>)
164  {
165  if (value < to_limits::lowest())
166  throw range_error{internal::cat2("Cast underflow: "sv, description)};
167  }
168  else
169  {
170  // FROM is signed, but TO is not. Treat this as a special case, because
171  // there may not be a good broader type in which the compiler can even
172  // perform our check.
173  if (value < 0)
175  "Casting negative value to unsigned type: "sv, description)};
176  }
177  }
178  else
179  {
180  // No need to check: the value is unsigned so can't fall below the range
181  // of the TO type.
182  }
183 
184  if constexpr (std::is_integral_v<FROM>)
185  {
186  using unsigned_from = std::make_unsigned_t<FROM>;
187  using unsigned_to = std::make_unsigned_t<TO>;
188  constexpr auto from_max{static_cast<unsigned_from>((from_limits::max)())};
189  constexpr auto to_max{static_cast<unsigned_to>((to_limits::max)())};
190  if constexpr (from_max > to_max)
191  {
192  if (internal::cmp_greater(value, to_max))
193  throw range_error{internal::cat2("Cast overflow: "sv, description)};
194  }
195  }
196  else if constexpr ((from_limits::max)() > (to_limits::max)())
197  {
198  if (value > (to_limits::max)())
199  throw range_error{internal::cat2("Cast overflow: ", description)};
200  }
201 
202  return static_cast<TO>(value);
203 }
204 
205 
227 inline PQXX_PRIVATE void check_version() noexcept
228 {
229  // There is no particular reason to do this here in @ref connection, except
230  // to ensure that every meaningful libpqxx client will execute it. The call
231  // must be in the execution path somewhere or the compiler won't try to link
232  // it. We can't use it to initialise a global or class-static variable,
233  // because a smart compiler might resolve it at compile time.
234  //
235  // On the other hand, we don't want to make a useless function call too
236  // often for performance reasons. A local static variable is initialised
237  // only on the definition's first execution. Compilers will be well
238  // optimised for this behaviour, so there's a minimal one-time cost.
239  static auto const version_ok{internal::PQXX_VERSION_CHECK()};
240  ignore_unused(version_ok);
241 }
242 
243 
245 
247 struct PQXX_LIBEXPORT thread_safety_model
248 {
250  bool safe_libpq = false;
251 
253 
259  bool safe_kerberos = false;
260 
262  std::string description;
263 };
264 
265 
267 [[nodiscard]] PQXX_LIBEXPORT thread_safety_model describe_thread_safety();
268 
269 
270 #if defined(PQXX_HAVE_CONCEPTS)
271 # define PQXX_POTENTIAL_BINARY_ARG pqxx::potential_binary
272 #else
273 # define PQXX_POTENTIAL_BINARY_ARG typename
274 #endif
275 
276 
278 
295 template<PQXX_POTENTIAL_BINARY_ARG TYPE>
296 std::basic_string_view<std::byte> binary_cast(TYPE const &data)
297 {
298  static_assert(sizeof(value_type<TYPE>) == 1);
299  return {
300  reinterpret_cast<std::byte const *>(
301  const_cast<strip_t<decltype(*std::data(data))> const *>(
302  std::data(data))),
303  std::size(data)};
304 }
305 
306 
307 #if defined(PQXX_HAVE_CONCEPTS)
308 template<typename CHAR>
309 concept char_sized = (sizeof(CHAR) == 1);
310 # define PQXX_CHAR_SIZED_ARG char_sized
311 #else
312 # define PQXX_CHAR_SIZED_ARG typename
313 #endif
314 
316 
323 template<PQXX_CHAR_SIZED_ARG CHAR, typename SIZE>
324 std::basic_string_view<std::byte> binary_cast(CHAR const *data, SIZE size)
325 {
326  static_assert(sizeof(CHAR) == 1);
327  return {
328  reinterpret_cast<std::byte const *>(data),
329  check_cast<std::size_t>(size, "binary data size")};
330 }
331 
332 
334 constexpr oid oid_none{0};
335 } // namespace pqxx
336 
337 
339 
348 namespace pqxx::internal
349 {
350 using namespace std::literals;
351 
352 
354 
358 template<typename CHAR> inline constexpr bool is_digit(CHAR c) noexcept
359 {
360  return (c >= '0') and (c <= '9');
361 }
362 
363 
365 
367 [[nodiscard]] std::string
368 describe_object(std::string_view class_name, std::string_view name);
369 
370 
372 
384  void const *old_guest, std::string_view old_class, std::string_view old_name,
385  void const *new_guest, std::string_view new_class,
386  std::string_view new_name);
387 
388 
390 
394  void const *old_guest, std::string_view old_class, std::string_view old_name,
395  void const *new_guest, std::string_view new_class,
396  std::string_view new_name);
397 
398 
400 
403 inline constexpr std::size_t size_esc_bin(std::size_t binary_bytes) noexcept
404 {
405  return 2 + (2 * binary_bytes) + 1;
406 }
407 
408 
410 
412 inline constexpr std::size_t size_unesc_bin(std::size_t escaped_bytes) noexcept
413 {
414  return (escaped_bytes - 2) / 2;
415 }
416 
417 
418 // TODO: Use actual binary type for "data".
420 
425 void PQXX_LIBEXPORT
426 esc_bin(std::basic_string_view<std::byte> binary_data, char buffer[]) noexcept;
427 
428 
430 std::string PQXX_LIBEXPORT
431 esc_bin(std::basic_string_view<std::byte> binary_data);
432 
433 
435 void PQXX_LIBEXPORT
436 unesc_bin(std::string_view escaped_data, std::byte buffer[]);
437 
438 
440 std::basic_string<std::byte>
441  PQXX_LIBEXPORT unesc_bin(std::string_view escaped_data);
442 
443 
445 template<typename T> auto ssize(T const &c)
446 {
447 #if defined(__cpp_lib_ssize) && __cplusplus >= __cpp_lib_ssize
448  return std::ssize(c);
449 #else
450  using signed_t = std::make_signed_t<decltype(std::size(c))>;
451  return static_cast<signed_t>(std::size(c));
452 #endif // __cpp_lib_ssize
453 }
454 
455 
457 
461 template<typename RETURN, typename... ARGS>
462 std::tuple<ARGS...> args_f(RETURN (&func)(ARGS...));
463 
464 
466 
470 template<typename RETURN, typename... ARGS>
471 std::tuple<ARGS...> args_f(std::function<RETURN(ARGS...)> const &);
472 
473 
475 
479 template<typename CLASS, typename RETURN, typename... ARGS>
480 std::tuple<ARGS...> member_args_f(RETURN (CLASS::*)(ARGS...));
481 
482 
484 
488 template<typename CLASS, typename RETURN, typename... ARGS>
489 std::tuple<ARGS...> member_args_f(RETURN (CLASS::*)(ARGS...) const);
490 
491 
493 
499 template<typename CALLABLE>
500 auto args_f(CALLABLE const &f)
501  -> decltype(member_args_f(&CALLABLE::operator()));
502 
503 
505 template<typename CALLABLE>
506 using args_t = decltype(args_f(std::declval<CALLABLE>()));
507 
508 
510 
513 template<typename... TYPES>
514 std::tuple<strip_t<TYPES>...> strip_types(std::tuple<TYPES...> const &);
515 
516 
518 template<typename... TYPES>
519 using strip_types_t = decltype(strip_types(std::declval<TYPES...>()));
520 } // namespace pqxx::internal
521 #endif
Something is out of range, similar to std::out_of_range.
Definition: except.hxx:201
std::string describe_object(std::string_view class_name, std::string_view name)
Describe an object for humans, based on class name and optional name.
Definition: util.cxx:51
Descriptor of library&#39;s thread-safety model.
Definition: util.hxx:247
constexpr std::size_t size_esc_bin(std::size_t binary_bytes) noexcept
Compute buffer size needed to escape binary data for use as a BYTEA.
Definition: util.hxx:403
The home of all libpqxx classes, functions, templates, etc.
Definition: array.hxx:26
auto ssize(T const &c)
Transitional: std::ssize(), or custom implementation if not available.
Definition: util.hxx:445
constexpr bool is_digit(CHAR c) noexcept
A safer and more generic replacement for std::isdigit.
Definition: util.hxx:358
constexpr bool cmp_greater(LEFT lhs, RIGHT rhs) noexcept
C++20 std::cmp_greater, or workaround if not available.
Definition: util.hxx:78
void check_unique_register(void const *old_guest, std::string_view old_class, std::string_view old_name, void const *new_guest, std::string_view new_class, std::string_view new_name)
Check validity of registering a new "guest" in a "host.".
Definition: util.cxx:61
std::tuple< ARGS... > member_args_f(RETURN(CLASS::*)(ARGS...))
Helper for determining a member function&#39;s parameter types.
std::string description
A human-readable description of any thread-safety issues.
Definition: util.hxx:262
void check_version() noexcept
Definition: util.hxx:227
void esc_bin(std::basic_string_view< std::byte > binary_data, char buffer[]) noexcept
Hex-escape binary data into a buffer.
Definition: util.cxx:126
constexpr bool cmp_less_equal(LEFT lhs, RIGHT rhs) noexcept
C++20 std::cmp_less_equal, or workaround if not available.
Definition: util.hxx:91
constexpr oid oid_none
The "null" oid.
Definition: util.hxx:334
void check_unique_unregister(void const *old_guest, std::string_view old_class, std::string_view old_name, void const *new_guest, std::string_view new_class, std::string_view new_name)
Like check_unique_register, but for un-registering a guest.
Definition: util.cxx:78
std::tuple< strip_t< TYPES >... > strip_types(std::tuple< TYPES... > const &)
Helper: Apply strip_t to each of a tuple type&#39;s component types.
decltype(args_f(std::declval< CALLABLE >())) args_t
A callable&#39;s parameter types, as a tuple.
Definition: util.hxx:506
int PQXX_VERSION_CHECK() noexcept
Library version check stub.
Definition: version.cxx:23
constexpr void ignore_unused(T &&...) noexcept
Suppress compiler warning about an unused item.
Definition: util.hxx:135
std::remove_cv_t< std::remove_reference_t< TYPE > > strip_t
Remove any constness, volatile, and reference-ness from a type.
Definition: types.hxx:91
decltype(strip_types(std::declval< TYPES... >())) strip_types_t
Take a tuple type and apply strip_t to its component types.
Definition: util.hxx:519
Internal items for libpqxx&#39; own use. Do not use these yourself.
Definition: composite.hxx:82
thread_safety_model describe_thread_safety()
Describe thread safety available in this build.
Definition: util.cxx:33
TO check_cast(FROM value, std::string_view description)
Cast a numeric value to another type, or throw if it underflows/overflows.
Definition: util.hxx:144
constexpr std::size_t size_unesc_bin(std::size_t escaped_bytes) noexcept
Compute binary size from the size of its escaped version.
Definition: util.hxx:412
constexpr bool cmp_greater_equal(LEFT lhs, RIGHT rhs) noexcept
C++20 std::cmp_greater_equal, or workaround if not available.
Definition: util.hxx:104
std::string cat2(std::string_view x, std::string_view y)
Efficiently concatenate two strings.
Definition: util.hxx:118
constexpr bool cmp_less(LEFT lhs, RIGHT rhs) noexcept
Same as std::cmp_less, or a workaround where that&#39;s not available.
Definition: util.hxx:56
strip_t< decltype(*std::begin(std::declval< CONTAINER >()))> value_type
The type of a container&#39;s elements.
Definition: types.hxx:107
void unesc_bin(std::string_view escaped_data, std::byte buffer[])
Reconstitute binary data from its escaped version.
Definition: util.cxx:158
std::tuple< ARGS... > args_f(RETURN(&func)(ARGS...))
Helper for determining a function&#39;s parameter types.
std::basic_string_view< std::byte > binary_cast(TYPE const &data)
Cast binary data to a type that libpqxx will recognise as binary.
Definition: util.hxx:296