libpqxx
The C++ client library for PostgreSQL
zview.hxx
Go to the documentation of this file.
1 /* Zero-terminated string view.
2  *
3  * DO NOT INCLUDE THIS FILE DIRECTLY; include pqxx/zview instead.
4  *
5  * Copyright (c) 2000-2026, 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_ZVIEW_HXX
12 #define PQXX_ZVIEW_HXX
13 
14 #include <cassert>
15 #include <string>
16 #include <string_view>
17 #include <type_traits>
18 
19 #include "pqxx/types.hxx"
20 
21 
22 namespace pqxx
23 {
24 class zview;
25 
26 
28 
32 template<typename T>
33 concept ZString =
34  (std::is_convertible_v<std::remove_cvref_t<T>, char const *> or
35  std::is_convertible_v<std::remove_cvref_t<T>, zview> or
36  std::is_convertible_v<T, std::string const &>) and
37  not std::is_convertible_v<T, std::nullptr_t>;
38 
39 
41 
54 class PQXX_LIBEXPORT zview final : public std::string_view
55 {
56 public:
58  constexpr zview() noexcept : zview{""} {}
59 
61 
64  PQXX_ZARGS constexpr zview(char const text[], std::ptrdiff_t len) noexcept(
65  noexcept(std::string_view{text, static_cast<std::size_t>(len)})) :
66  std::string_view{text, static_cast<std::size_t>(len)}
67  {
68  invariant();
69  }
70 
72  constexpr zview(char text[], std::ptrdiff_t len) noexcept(
73  noexcept(std::string_view{text, static_cast<std::size_t>(len)})) :
74  std::string_view{text, static_cast<std::size_t>(len)}
75  {
76  invariant();
77  }
78 
80 
83  explicit constexpr zview(std::string_view other) noexcept :
84  std::string_view{other}
85  {
86  invariant();
87  }
88 
89  // clang-tidy rule bug:
90  // NOLINTBEGIN(
91  // cppcoreguidelines-pro-bounds-array-to-pointer-decay,
92  // hicpp-no-array-decay
93  // )
94 
96 
98  template<typename... Args>
99  explicit constexpr zview(Args &&...args) :
100  std::string_view(std::forward<Args>(args)...)
101  {
102  invariant();
103  }
104 
105  // NOLINTEND(
106  // cppcoreguidelines-pro-bounds-array-to-pointer-decay,
107  // hicpp-no-array-decay
108  // )
109 
110  // NOLINTBEGIN(google-explicit-constructor,hicpp-explicit-conversions)
111 
113  constexpr zview(std::string const &str) noexcept :
114  std::string_view{str.c_str(), str.size()}
115  {
116  invariant();
117  }
118 
120 
124  PQXX_ZARGS constexpr zview(char const str[]) noexcept(
125  noexcept(std::string_view{str})) :
126  std::string_view{str}
127  {
128  invariant();
129  }
130 
131  zview(std::nullptr_t) = delete;
132 
134 
142  template<size_t size>
143  PQXX_ZARGS constexpr zview(char const (&literal)[size]) :
144  zview(literal, size - 1)
145  {}
146 
147  // NOLINTEND(google-explicit-constructor,hicpp-explicit-conversions)
148 
150  [[nodiscard]] constexpr char const *c_str() const & noexcept
151  {
152  return data();
153  }
154 
155  // NOLINTBEGIN(google-explicit-constructor,hicpp-explicit-conversions)
157  constexpr operator char const *() const noexcept { return data(); }
158  // NOLINTEND(google-explicit-constructor,hicpp-explicit-conversions)
159 
161  constexpr bool operator==(zview const &rhs) const noexcept
162  {
163  return std::string_view{*this} == std::string_view{rhs};
164  }
166  constexpr bool operator!=(zview const &rhs) const noexcept
167  {
168  return std::string_view{*this} != std::string_view{rhs};
169  }
170 
171 private:
173  [[maybe_unused]] constexpr void invariant() const noexcept
174  {
175  assert(std::data(*this) != nullptr);
176  assert(std::data(*this)[std::size(*this)] == '\0');
177  }
178 };
179 
180 
181 template<> inline constexpr std::string_view name_type<zview>() noexcept
182 {
183  return "pqxx::zview";
184 }
185 
186 
188 
195 PQXX_ZARGS constexpr zview
196 operator""_zv(char const str[], std::size_t len) noexcept
197 {
198  return zview{str, len};
199 }
200 
201 
203 constexpr inline bool operator==(char const lhs[], pqxx::zview rhs) noexcept
204 {
205  return std::string_view{lhs} == std::string_view{rhs};
206 }
207 
208 
210 constexpr inline bool operator!=(char const lhs[], pqxx::zview rhs) noexcept
211 {
212  return std::string_view{lhs} != std::string_view{rhs};
213 }
214 } // namespace pqxx
215 
216 
218 template<> inline constexpr bool std::ranges::enable_view<pqxx::zview>{true};
219 
220 
222 template<>
223 inline constexpr bool std::ranges::enable_borrowed_range<pqxx::zview>{true};
224 #endif
Marker-type wrapper: zero-terminated std::string_view.
Definition: zview.hxx:55
constexpr zview() noexcept
Default constructor produces a zero-terminated empty string.
Definition: zview.hxx:58
constexpr char const * c_str() const &noexcept
Return as C string.
Definition: zview.hxx:150
constexpr PQXX_ZARGS zview(char const str[]) noexcept(noexcept(std::string_view{str}))
Construct a zview from a C-style string.
Definition: zview.hxx:124
constexpr zview(Args &&...args)
Construct from any initialiser you might use for std::string_view.
Definition: zview.hxx:99
constexpr PQXX_ZARGS zview(char const (&literal)[size])
Construct a zview from a string literal.
Definition: zview.hxx:143
zview(std::nullptr_t)=delete
constexpr PQXX_ZARGS 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:64
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:72
constexpr bool operator!=(zview const &rhs) const noexcept
Disambiguating comparison operator: leave it to std::string_view.
Definition: zview.hxx:166
constexpr zview(std::string_view other) noexcept
Explicitly promote a string_view to a zview.
Definition: zview.hxx:83
constexpr zview(std::string const &str) noexcept
Definition: zview.hxx:113
constexpr bool operator==(zview const &rhs) const noexcept
Disambiguating comparison operator: leave it to std::string_view.
Definition: zview.hxx:161
#define PQXX_ZARGS
Definition: header-pre.hxx:136
#define PQXX_LIBEXPORT
Definition: header-pre.hxx:206
The home of all libpqxx classes, functions, templates, etc.
Definition: array.cxx:26
constexpr bool operator!=(char const lhs[], pqxx::zview rhs) noexcept
Disambiguating comparison operator: leave it to std::string_view.
Definition: zview.hxx:210
concept ZString
Concept: T is a known zero-terminated string type.
Definition: zview.hxx:33
constexpr std::string_view name_type< zview >() noexcept
Definition: zview.hxx:181
constexpr bool operator==(char const lhs[], pqxx::zview rhs) noexcept
Disambiguating comparison operator: leave it to std::string_view.
Definition: zview.hxx:203