libpqxx 7.7.5
zview.hxx
1/* Zero-terminated string view.
2 *
3 * DO NOT INCLUDE THIS FILE DIRECTLY; include pqxx/zview instead.
4 *
5 * Copyright (c) 2000-2023, 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_ZVIEW
12#define PQXX_H_ZVIEW
13
14#include <string>
15#include <string_view>
16#include <type_traits>
17
18#include "pqxx/types.hxx"
19
20
21namespace pqxx
22{
24
37class zview : public std::string_view
38{
39public:
40 constexpr zview() noexcept = default;
41
43 constexpr zview(char const text[], std::ptrdiff_t len) :
44 std::string_view{text, static_cast<std::size_t>(len)}
45 {}
46
48 constexpr zview(char text[], std::ptrdiff_t len) :
49 std::string_view{text, static_cast<std::size_t>(len)}
50 {}
51
53 explicit constexpr zview(std::string_view other) noexcept :
54 std::string_view{other}
55 {}
56
58
60 template<typename... Args>
61 explicit constexpr zview(Args &&...args) :
62 std::string_view(std::forward<Args>(args)...)
63 {}
64
65 // C++20: constexpr.
67 zview(std::string const &str) noexcept :
68 std::string_view{str.c_str(), str.size()}
69 {}
70
72
76 constexpr zview(char const str[]) : std::string_view{str} {}
77
79
87 template<size_t size>
88 constexpr zview(char const (&literal)[size]) : zview(literal, size - 1)
89 {}
90
92 [[nodiscard]] constexpr char const *c_str() const &noexcept
93 {
94 return data();
95 }
96};
97
98
100
107constexpr zview operator"" _zv(char const str[], std::size_t len) noexcept
108{
109 return zview{str, len};
110}
111} // namespace pqxx
112
113
114#if defined(PQXX_HAVE_CONCEPTS)
116template<> inline constexpr bool std::ranges::enable_view<pqxx::zview>{true};
117
118
120template<>
121inline constexpr bool std::ranges::enable_borrowed_range<pqxx::zview>{true};
122
123namespace pqxx::internal
124{
126
130template<typename T>
131concept ZString = std::is_convertible_v < strip_t<T>,
132char const * > or std::is_convertible_v<strip_t<T>, zview> or
133 std::is_convertible_v<T, std::string const &>;
134} // namespace pqxx::internal
135#endif // PQXX_HAVE_CONCEPTS
136
137
138namespace pqxx::internal
139{
141inline constexpr char const *as_c_string(char const str[]) noexcept
142{
143 return str;
144}
146template<std::size_t N>
147inline constexpr char const *as_c_string(char (&str)[N]) noexcept
148{
149 return str;
150}
152inline constexpr char const *as_c_string(pqxx::zview str) noexcept
153{
154 return str.c_str();
155}
156// C++20: Make this constexpr.
158inline char const *as_c_string(std::string const &str) noexcept
159{
160 return str.c_str();
161}
162} // namespace pqxx::internal
163#endif
The home of all libpqxx classes, functions, templates, etc.
Definition: array.hxx:27
Internal items for libpqxx' own use. Do not use these yourself.
Definition: composite.hxx:83
constexpr char const * as_c_string(char const str[]) noexcept
Get a raw C string pointer.
Definition: zview.hxx:141
Marker-type wrapper: zero-terminated std::string_view.
Definition: zview.hxx:38
constexpr zview(char const (&literal)[size])
Construct a zview from a string literal.
Definition: zview.hxx:88
constexpr char const * c_str() const &noexcept
Either a null pointer, or a zero-terminated text buffer.
Definition: zview.hxx:92
constexpr zview(Args &&...args)
Construct from any initialiser you might use for std::string_view.
Definition: zview.hxx:61
constexpr zview() noexcept=default
constexpr zview(char text[], std::ptrdiff_t len)
Convenience overload: construct using pointer and signed length.
Definition: zview.hxx:48
constexpr zview(std::string_view other) noexcept
Explicitly promote a string_view to a zview.
Definition: zview.hxx:53
zview(std::string const &str) noexcept
Definition: zview.hxx:67
constexpr zview(char const str[])
Construct a zview from a C-style string.
Definition: zview.hxx:76