Loading...
Searching...
No Matches
zview.hxx
/* Zero-terminated string view.
*
* DO NOT INCLUDE THIS FILE DIRECTLY; include pqxx/zview instead.
*
* Copyright (c) 2000-2024, Jeroen T. Vermeulen.
*
* See COPYING for copyright license. If you did not receive a file called
* COPYING with this source code, please notify the distributor of this
* mistake, or contact the author.
*/
#ifndef PQXX_H_ZVIEW
#define PQXX_H_ZVIEW
#include <string>
#include <string_view>
#include <type_traits>
#include "pqxx/types.hxx"
namespace pqxx
{
class zview : public std::string_view
{
public:
constexpr zview() noexcept = default;
constexpr zview(char const text[], std::ptrdiff_t len) noexcept(
noexcept(std::string_view{text, static_cast<std::size_t>(len)})) :
std::string_view{text, static_cast<std::size_t>(len)}
{}
constexpr zview(char text[], std::ptrdiff_t len) noexcept(
noexcept(std::string_view{text, static_cast<std::size_t>(len)})) :
std::string_view{text, static_cast<std::size_t>(len)}
{}
explicit constexpr zview(std::string_view other) noexcept :
std::string_view{other}
{}
template<typename... Args>
explicit constexpr zview(Args &&...args) :
std::string_view(std::forward<Args>(args)...)
{}
// C++20: constexpr.
zview(std::string const &str) noexcept :
std::string_view{str.c_str(), str.size()}
{}
constexpr zview(char const str[]) noexcept(noexcept(std::string_view{str})) :
std::string_view{str}
{}
template<size_t size>
constexpr zview(char const (&literal)[size]) : zview(literal, size - 1)
{}
[[nodiscard]] constexpr char const *c_str() const & noexcept
{
return data();
}
};
constexpr zview operator"" _zv(char const str[], std::size_t len) noexcept
{
return zview{str, len};
}
} // namespace pqxx
#if defined(PQXX_HAVE_CONCEPTS)
template<> inline constexpr bool std::ranges::enable_view<pqxx::zview>{true};
template<>
inline constexpr bool std::ranges::enable_borrowed_range<pqxx::zview>{true};
namespace pqxx::internal
{
template<typename T>
concept ZString = std::is_convertible_v<strip_t<T>, char const *> or
std::is_convertible_v<strip_t<T>, zview> or
std::is_convertible_v<T, std::string const &>;
} // namespace pqxx::internal
#endif // PQXX_HAVE_CONCEPTS
namespace pqxx::internal
{
inline constexpr char const *as_c_string(char const str[]) noexcept
{
return str;
}
template<std::size_t N>
inline constexpr char const *as_c_string(char (&str)[N]) noexcept
{
return str;
}
inline constexpr char const *as_c_string(pqxx::zview str) noexcept
{
return str.c_str();
}
// C++20: Make this constexpr.
inline char const *as_c_string(std::string const &str) noexcept
{
return str.c_str();
}
} // namespace pqxx::internal
#endif
pqxx::zview
Marker-type wrapper: zero-terminated std::string_view.
Definition zview.hxx:38
pqxx::zview::zview
constexpr zview(char const (&literal)[size])
Construct a zview from a string literal.
Definition zview.hxx:92
pqxx::zview::c_str
constexpr char const * c_str() const &noexcept
Either a null pointer, or a zero-terminated text buffer.
Definition zview.hxx:96
pqxx::zview::zview
constexpr zview(Args &&...args)
Construct from any initialiser you might use for std::string_view.
Definition zview.hxx:63
pqxx::zview::zview
constexpr zview(char text[], std::ptrdiff_t len) noexcept(noexcept(std::string_view{text, static_cast< std::size_t >(len)}))
Convenience overload: construct using pointer and signed length.
Definition zview.hxx:49
pqxx::zview::zview
constexpr zview(char const text[], std::ptrdiff_t len) noexcept(noexcept(std::string_view{text, static_cast< std::size_t >(len)}))
Convenience overload: construct using pointer and signed length.
Definition zview.hxx:43
pqxx::zview::zview
constexpr zview(char const str[]) noexcept(noexcept(std::string_view{str}))
Construct a zview from a C-style string.
Definition zview.hxx:78
pqxx::zview::zview
constexpr zview(std::string_view other) noexcept
Explicitly promote a string_view to a zview.
Definition zview.hxx:55
pqxx::zview::zview
zview(std::string const &str) noexcept
Definition zview.hxx:69
pqxx::internal
Internal items for libpqxx' own use. Do not use these yourself.
Definition encodings.cxx:33
pqxx::internal::as_c_string
constexpr char const * as_c_string(char const str[]) noexcept
Get a raw C string pointer.
Definition zview.hxx:145
pqxx
The home of all libpqxx classes, functions, templates, etc.
Definition array.cxx:27