Skip to content

Commit

Permalink
Some minor tweaks for char_traits work. (#792)
Browse files Browse the repository at this point in the history
* Some minor tweaks for `char_traits` work.

Patches up some very minor details after merging @tambry's great contribution in #751.

After this I'll run the automatic formatter as well.

* Don't define char traits' `length()`.

This function is just not appropriate to a `std::byte` specialisation.
There is no way inspect the contents of a raw byte string and figure
out from there what the string's length is.

So, remove the function's definition.  The _declaration_ is required,
but there can't be a usable definition.
  • Loading branch information
jtv committed Feb 9, 2024
1 parent e765554 commit a5e52dd
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 26 deletions.
5 changes: 1 addition & 4 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@
- `std::basic_string<std::byte>` and `std::basic_string_view<std::byte>` have
been replaced with `pqxx::bytes` and `pqxx::bytes_view` respectively to
support the removal of generic `std::char_traits` in libc++ 19. Users must
switch to these aliases to support versions from libc++ 18 onward.
In a future major release that start requiring C++20 these are likely to
become aliases for `std::vector<std::byte>` and `std::span<std::byte>` to
better match the intent of the interfaces. (#726)
switch to these aliases to support versions from libc++ 18 onward. (#726)
7.8.1
- Regenerate build files. Should fix ARM Mac build. (#715)
- Reinstate `<ciso646>` that MSVC can't live with or without. (#713)
Expand Down
4 changes: 2 additions & 2 deletions include/pqxx/field.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,8 @@ public:
}
else
{
auto const bytes{c_str()};
from_string(bytes, obj);
auto const data{c_str()};
from_string(data, obj);
return true;
}
}
Expand Down
46 changes: 26 additions & 20 deletions include/pqxx/util.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -280,10 +280,11 @@ struct PQXX_LIBEXPORT thread_safety_model
# define PQXX_POTENTIAL_BINARY_ARG typename
#endif

// A custom std::char_traits if the standard library lacks a generic
// implementation or a specialisation for std::byte. Notably they aren't
// required to provide either.
// libc++ 19 removed its generic implementation.
/// Custom `std::char_trast` if the compiler does not provide one.
/** Needed if the standard library lacks a generic implementation or a
* specialisation for std::byte. They aren't strictly required to provide
* either, and libc++ 19 removed its generic implementation.
*/
struct byte_char_traits : std::char_traits<char>
{
using char_type = std::byte;
Expand All @@ -297,14 +298,14 @@ struct byte_char_traits : std::char_traits<char>
return std::memcmp(a, b, size);
}

// This is nonsense: we can't determine the length of a random sequence of
// bytes.
// But std::char_traits requires us to implement this so we treat 0 as a
// terminator for our "string".
static size_t length(const std::byte *data)
{
return std::strlen(reinterpret_cast<const char*>(data));
}
/// Deliberately undefined: "guess" the length of an array of bytes.
/* This is nonsense: we can't determine the length of a random sequence of
* bytes. There is no terminating zero like there is for C strings.
*
* But `std::char_traits` requires us to provide this function, so we
* declare it without defining it.
*/
static size_t length(const std::byte *data);

static const std::byte *
find(const std::byte *data, std::size_t size, const std::byte &value)
Expand Down Expand Up @@ -360,24 +361,29 @@ inline constexpr bool has_generic_bytes_char_traits =
// Necessary for libc++ 18.
#include "pqxx/internal/ignore-deprecated-pre.hxx"

// Type alias for a container containing bytes.
// Required to support standard libraries without a generic implementation for
// std::char_traits<std::byte>.
// WARNING: Will change to std::vector<std::byte> in the next major release.
// C++20: Change this type.
/// Type alias for a container containing bytes.
/* Required to support standard libraries without a generic implementation for
* `std::char_traits<std::byte>`.
* @warn Will change to `std::vector<std::byte>` in the next major release.
*/
using bytes = std::conditional<
has_generic_bytes_char_traits, std::basic_string<std::byte>,
std::basic_string<std::byte, byte_char_traits>>::type;

// Type alias for a view of bytes.
// Required to support standard libraries without a generic implementation for
// std::char_traits<std::byte>.
// WARNING: Will change to std::span<std::byte> in the next major release.
// C++20: Change this type.
/// Type alias for a view of bytes.
/* Required to support standard libraries without a generic implementation for
* `std::char_traits<std::byte>`.
* @warn Will change to `std::span<std::byte>` in the next major release.
*/
using bytes_view = std::conditional<
has_generic_bytes_char_traits, std::basic_string_view<std::byte>,
std::basic_string_view<std::byte, byte_char_traits>>::type;

#include "pqxx/internal/ignore-deprecated-post.hxx"


/// Cast binary data to a type that libpqxx will recognise as binary.
/** There are many different formats for storing binary data in memory. You
* may have yours as a `std::string`, or a `std::vector<uchar_t>`, or one of
Expand Down

0 comments on commit a5e52dd

Please sign in to comment.