22 #include <string_view>
23 #include <type_traits>
28 #if __has_include(<version>)
32 #include "pqxx/except.hxx"
33 #include "pqxx/internal/encodings.hxx"
34 #include "pqxx/types.hxx"
35 #include "pqxx/version.hxx"
42 #include <pqxx/internal/libpq-forward.hxx>
52 template<
typename LEFT,
typename RIGHT>
53 inline constexpr
bool cmp_less(LEFT lhs, RIGHT rhs)
55 #if defined(PQXX_HAVE_CMP)
56 return std::cmp_less(lhs, rhs);
58 if constexpr (std::is_signed_v<LEFT> == std::is_signed_v<RIGHT>)
60 else if constexpr (std::is_signed_v<LEFT>)
61 return (lhs <= 0) ? true : (std::make_unsigned_t<LEFT>(lhs) < rhs);
63 return (rhs <= 0) ? false : (lhs < std::make_unsigned_t<RIGHT>(rhs));
70 template<
typename LEFT,
typename RIGHT>
73 #if defined(PQXX_HAVE_CMP)
74 return std::cmp_greater(lhs, rhs);
83 template<
typename LEFT,
typename RIGHT>
86 #if defined(PQXX_HAVE_CMP)
87 return std::cmp_less_equal(lhs, rhs);
96 template<
typename LEFT,
typename RIGHT>
99 #if defined(PQXX_HAVE_CMP)
100 return std::cmp_greater_equal(lhs, rhs);
111 [[nodiscard]]
inline std::string
cat2(std::string_view x, std::string_view y)
114 auto const xs{std::size(x)}, ys{std::size(y)};
116 x.copy(std::data(buf), xs);
117 y.copy(std::data(buf) + xs, ys);
125 using namespace std::literals;
135 template<
typename TO,
typename FROM>
136 inline TO
check_cast(FROM value, std::string_view description)
138 static_assert(std::is_arithmetic_v<FROM>);
139 static_assert(std::is_arithmetic_v<TO>);
140 static_assert(std::is_integral_v<FROM> == std::is_integral_v<TO>);
144 if constexpr (std::is_same_v<FROM, bool>)
145 return static_cast<TO
>(value);
151 using from_limits = std::numeric_limits<decltype(value)>;
152 using to_limits = std::numeric_limits<TO>;
153 if constexpr (std::is_signed_v<FROM>)
155 if constexpr (std::is_signed_v<TO>)
157 if (value < to_limits::lowest())
167 "Casting negative value to unsigned type: "sv, description)};
176 if constexpr (std::is_integral_v<FROM>)
178 using unsigned_from = std::make_unsigned_t<FROM>;
179 using unsigned_to = std::make_unsigned_t<TO>;
181 constexpr
auto from_max{
static_cast<unsigned_from
>((from_limits::max)())};
183 constexpr
auto to_max{
static_cast<unsigned_to
>((to_limits::max)())};
184 if constexpr (from_max > to_max)
190 else if constexpr ((from_limits::max)() > (to_limits::max)())
192 if (value > (to_limits::max)())
196 return static_cast<TO
>(value);
244 bool safe_libpq =
false;
253 bool safe_kerberos =
false;
264 #if defined(PQXX_HAVE_CONCEPTS)
265 # define PQXX_POTENTIAL_BINARY_ARG pqxx::potential_binary
267 # define PQXX_POTENTIAL_BINARY_ARG typename
288 template<PQXX_POTENTIAL_BINARY_ARG TYPE>
293 reinterpret_cast<std::byte
const *
>(
294 const_cast<strip_t<decltype(*std::data(data))
> const *>(
300 #if defined(PQXX_HAVE_CONCEPTS)
301 template<
typename CHAR>
302 concept char_sized = (
sizeof(CHAR) == 1);
303 # define PQXX_CHAR_SIZED_ARG char_sized
305 # define PQXX_CHAR_SIZED_ARG typename
316 template<PQXX_CHAR_SIZED_ARG CHAR,
typename SIZE>
317 std::basic_string_view<std::byte>
binary_cast(CHAR
const *data, SIZE size)
319 static_assert(
sizeof(CHAR) == 1);
321 reinterpret_cast<std::byte
const *
>(data),
322 check_cast<std::size_t>(size,
"binary data size")};
344 using namespace std::literals;
354 return (c >=
'0') and (c <=
'9');
361 [[nodiscard]] std::string
378 void const *old_guest, std::string_view old_class, std::string_view old_name,
379 void const *new_guest, std::string_view new_class,
380 std::string_view new_name);
388 void const *old_guest, std::string_view old_class, std::string_view old_name,
389 void const *new_guest, std::string_view new_class,
390 std::string_view new_name);
399 return 2 + (2 * binary_bytes) + 1;
408 return (escaped_bytes - 2) / 2;
420 esc_bin(std::basic_string_view<std::byte> binary_data,
char buffer[]) noexcept;
424 std::string PQXX_LIBEXPORT
425 esc_bin(std::basic_string_view<std::byte> binary_data);
430 unesc_bin(std::string_view escaped_data, std::byte buffer[]);
434 std::basic_string<std::byte>
435 PQXX_LIBEXPORT
unesc_bin(std::string_view escaped_data);
439 template<
typename T>
auto ssize(T
const &c)
441 #if defined(__cpp_lib_ssize) && __cplusplus >= __cpp_lib_ssize
442 return std::ssize(c);
444 using signed_t = std::make_signed_t<decltype(std::size(c))>;
445 return static_cast<signed_t
>(std::size(c));
454 void PQXX_LIBEXPORT
wait_for(
unsigned int microseconds);
The home of all libpqxx classes, functions, templates, etc.
Definition: array.hxx:23
void ignore_unused(T &&...)
Suppress compiler warning about an unused item.
Definition: util.hxx:128
std::remove_cv_t< std::remove_reference_t< TYPE > > strip_t
Remove any constness, volatile, and reference-ness from a type.
Definition: types.hxx:87
thread_safety_model describe_thread_safety()
Describe thread safety available in this build.
Definition: util.cxx:54
void check_version()
Definition: util.hxx:221
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:289
decltype(*std::begin(std::declval< CONTAINER >())) value_type
The type of a container's elements.
Definition: types.hxx:103
constexpr oid oid_none
The "null" oid.
Definition: util.hxx:328
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:136
Internal items for libpqxx' own use. Do not use these yourself.
Definition: composite.hxx:80
void unesc_bin(std::string_view escaped_data, std::byte buffer[])
Reconstitute binary data from its escaped version.
Definition: util.cxx:179
constexpr bool cmp_greater_equal(LEFT lhs, RIGHT rhs)
C++20 std::cmp_greater_equal, or workaround if not available.
Definition: util.hxx:97
int PQXX_VERSION_CHECK() noexcept
Library version check stub.
Definition: version.cxx:18
void esc_bin(std::basic_string_view< std::byte > binary_data, char buffer[]) noexcept
Hex-escape binary data into a buffer.
Definition: util.cxx:148
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:397
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:99
void wait_for(unsigned int microseconds)
Wait.
Definition: util.cxx:218
constexpr bool cmp_greater(LEFT lhs, RIGHT rhs)
C++20 std::cmp_greater, or workaround if not available.
Definition: util.hxx:71
constexpr bool cmp_less(LEFT lhs, RIGHT rhs)
C++20 std::cmp_less, or workaround if not available.
Definition: util.hxx:53
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:82
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:72
std::string cat2(std::string_view x, std::string_view y)
Efficiently concatenate two strings.
Definition: util.hxx:111
auto ssize(T const &c)
Transitional: std::ssize(), or custom implementation if not available.
Definition: util.hxx:439
bool is_digit(CHAR c)
A safer and more generic replacement for std::isdigit.
Definition: util.hxx:352
constexpr bool cmp_less_equal(LEFT lhs, RIGHT rhs)
C++20 std::cmp_less_equal, or workaround if not available.
Definition: util.hxx:84
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:406
Something is out of range, similar to std::out_of_range.
Definition: except.hxx:190
Descriptor of library's thread-safety model.
Definition: util.hxx:242
std::string description
A human-readable description of any thread-safety issues.
Definition: util.hxx:256