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-2025, 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 
21 namespace pqxx
22 {
24 
37 class zview : public std::string_view
38 {
39 public:
40  constexpr zview() noexcept = default;
41 
43  constexpr zview(char const text[], std::ptrdiff_t len) noexcept(
44  noexcept(std::string_view{text, static_cast<std::size_t>(len)})) :
45  std::string_view{text, static_cast<std::size_t>(len)}
46  {}
47 
49  constexpr zview(char text[], std::ptrdiff_t len) noexcept(
50  noexcept(std::string_view{text, static_cast<std::size_t>(len)})) :
51  std::string_view{text, static_cast<std::size_t>(len)}
52  {}
53 
55  explicit constexpr zview(std::string_view other) noexcept :
56  std::string_view{other}
57  {}
58 
60 
62  template<typename... Args>
63  explicit constexpr zview(Args &&...args) :
64  std::string_view(std::forward<Args>(args)...)
65  {}
66 
67  // C++20: constexpr.
69  zview(std::string const &str) noexcept :
70  std::string_view{str.c_str(), str.size()}
71  {}
72 
74 
78  constexpr zview(char const str[]) noexcept(noexcept(std::string_view{str})) :
79  std::string_view{str}
80  {}
81 
83 
91  template<size_t size>
92  constexpr zview(char const (&literal)[size]) : zview(literal, size - 1)
93  {}
94 
96  [[nodiscard]] constexpr char const *c_str() const & noexcept
97  {
98  return data();
99  }
100 };
101 
102 
104 
111 constexpr zview operator""_zv(char const str[], std::size_t len) noexcept
112 {
113  return zview{str, len};
114 }
115 
116 
117 template<>
118 PQXX_PURE constexpr inline std::string_view name_type<zview>() noexcept
119 {
120  return "pqxx::zview";
121 }
122 } // namespace pqxx
123 
124 
125 #if defined(PQXX_HAVE_CONCEPTS) && __has_include(<ranges>)
126 template<> inline constexpr bool std::ranges::enable_view<pqxx::zview>{true};
128 
129 
131 template<>
132 inline constexpr bool std::ranges::enable_borrowed_range<pqxx::zview>{true};
133 
134 namespace pqxx::internal
135 {
137 
141 template<typename T>
142 concept ZString = std::is_convertible_v<strip_t<T>, char const *> or
143  std::is_convertible_v<strip_t<T>, zview> or
144  std::is_convertible_v<T, std::string const &>;
145 } // namespace pqxx::internal
146 #endif // PQXX_HAVE_CONCEPTS
147 
148 
149 namespace pqxx::internal
150 {
152 inline constexpr char const *as_c_string(char const str[]) noexcept
153 {
154  return str;
155 }
157 template<std::size_t N>
158 inline constexpr char const *as_c_string(char (&str)[N]) noexcept
159 {
160  return str;
161 }
163 inline constexpr char const *as_c_string(pqxx::zview str) noexcept
164 {
165  return str.c_str();
166 }
167 // C++20: Make this constexpr.
169 inline char const *as_c_string(std::string const &str) noexcept
170 {
171  return str.c_str();
172 }
173 } // namespace pqxx::internal
174 #endif
Marker-type wrapper: zero-terminated std::string_view.
Definition: zview.hxx:37
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
constexpr char const * c_str() const &noexcept
Either a null pointer, or a zero-terminated text buffer.
Definition: zview.hxx:96
#define PQXX_PURE
Declare function "pure": no side effects, only reads globals and its args.
Definition: header-pre.hxx:77
Internal items for libpqxx' own use. Do not use these yourself.
Definition: encodings.cxx:32
constexpr zview() noexcept=default
constexpr zview(Args &&...args)
Construct from any initialiser you might use for std::string_view.
Definition: zview.hxx:63
constexpr zview(std::string_view other) noexcept
Explicitly promote a string_view to a zview.
Definition: zview.hxx:55
zview(std::string const &str) noexcept
Definition: zview.hxx:69
constexpr zview(char const (&literal)[size])
Construct a zview from a string literal.
Definition: zview.hxx:92
The home of all libpqxx classes, functions, templates, etc.
Definition: array.cxx:26
PQXX_PURE constexpr std::string_view name_type< zview >() noexcept
Definition: zview.hxx:118
constexpr zview(char const str[]) noexcept(noexcept(std::string_view{str}))
Construct a zview from a C-style string.
Definition: zview.hxx:78
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
char const * as_c_string(std::string const &str) noexcept
Get a raw C string pointer.
Definition: zview.hxx:169