libpqxx 7.8.1
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) 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
111constexpr zview operator"" _zv(char const str[], std::size_t len) noexcept
112{
113 return zview{str, len};
114}
115} // namespace pqxx
116
117
118#if defined(PQXX_HAVE_CONCEPTS)
120template<> inline constexpr bool std::ranges::enable_view<pqxx::zview>{true};
121
122
124template<>
125inline constexpr bool std::ranges::enable_borrowed_range<pqxx::zview>{true};
126
127namespace pqxx::internal
128{
130
134template<typename T>
135concept ZString = std::is_convertible_v<strip_t<T>, char const *> or
136 std::is_convertible_v<strip_t<T>, zview> or
137 std::is_convertible_v<T, std::string const &>;
138} // namespace pqxx::internal
139#endif // PQXX_HAVE_CONCEPTS
140
141
142namespace pqxx::internal
143{
145inline constexpr char const *as_c_string(char const str[]) noexcept
146{
147 return str;
148}
150template<std::size_t N>
151inline constexpr char const *as_c_string(char (&str)[N]) noexcept
152{
153 return str;
154}
156inline constexpr char const *as_c_string(pqxx::zview str) noexcept
157{
158 return str.c_str();
159}
160// C++20: Make this constexpr.
162inline char const *as_c_string(std::string const &str) noexcept
163{
164 return str.c_str();
165}
166} // namespace pqxx::internal
167#endif
The home of all libpqxx classes, functions, templates, etc.
Definition array.hxx:33
Internal items for libpqxx' own use. Do not use these yourself.
Definition composite.hxx:84
constexpr char const * as_c_string(char const str[]) noexcept
Get a raw C string pointer.
Definition zview.hxx:145
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:92
constexpr char const * c_str() const &noexcept
Either a null pointer, or a zero-terminated text buffer.
Definition zview.hxx:96
constexpr zview(Args &&...args)
Construct from any initialiser you might use for std::string_view.
Definition zview.hxx:63
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 zview() noexcept=default
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(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