libpqxx  7.6.0
zview.hxx
1 /* Zero-terminated string view.
2  *
3  * DO NOT INCLUDE THIS FILE DIRECTLY; include pqxx/zview instead.
4  *
5  * Copyright (c) 2000-2021, 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 "pqxx/compiler-public.hxx"
15 
16 #include <string>
17 #include <string_view>
18 #include <type_traits>
19 
20 #include "pqxx/types.hxx"
21 
22 
23 namespace pqxx
24 {
26 
39 class zview : public std::string_view
40 {
41 public:
42  constexpr zview() noexcept = default;
43 
45  constexpr zview(char const text[], std::ptrdiff_t len) :
46  std::string_view{text, static_cast<std::size_t>(len)}
47  {}
48 
50  constexpr zview(char text[], std::ptrdiff_t len) :
51  std::string_view{text, static_cast<std::size_t>(len)}
52  {}
53 
55 
57  template<typename... Args>
58  explicit constexpr zview(Args &&...args) :
59  std::string_view(std::forward<Args>(args)...)
60  {}
61 
63  zview(std::string const &str) : std::string_view{str.c_str(), std::size(str)}
64  {}
65 
67 
71  constexpr zview(char const str[]) : std::string_view{str} {}
72 
74 
82  template<size_t size>
83  constexpr zview(char const (&literal)[size]) : zview(literal, size - 1)
84  {}
85 
87  [[nodiscard]] constexpr char const *c_str() const noexcept { return data(); }
88 };
89 
90 
92 
97 constexpr zview operator"" _zv(char const str[], std::size_t len) noexcept
98 {
99  return zview{str, len};
100 }
101 } // namespace pqxx
102 
103 
104 #if defined(PQXX_HAVE_CONCEPTS)
105 template<> inline constexpr bool std::ranges::enable_view<pqxx::zview>{true};
107 
108 
110 template<>
111 inline constexpr bool std::ranges::enable_borrowed_range<pqxx::zview>{true};
112 
113 namespace pqxx::internal
114 {
116 
120 template<typename T>
121 concept ZString = std::is_convertible_v < strip_t<T>,
122 char const * > or std::is_convertible_v<strip_t<T>, zview> or
123  std::is_convertible_v<T, std::string const &>;
124 } // namespace pqxx::internal
125 #endif // PQXX_HAVE_CONCEPTS
126 
127 
128 namespace pqxx::internal
129 {
131 inline constexpr char const *as_c_string(char const str[]) noexcept
132 {
133  return str;
134 }
136 template<std::size_t N>
137 inline constexpr char const *as_c_string(char (&str)[N]) noexcept
138 {
139  return str;
140 }
142 inline constexpr char const *as_c_string(pqxx::zview str) noexcept
143 {
144  return str.c_str();
145 }
146 // TODO: constexpr as of C++20.
148 inline char const *as_c_string(std::string const &str) noexcept
149 {
150  return str.c_str();
151 }
152 } // namespace pqxx::internal
153 
154 #endif
constexpr char const * c_str() const noexcept
Either a null pointer, or a zero-terminated text buffer.
Definition: zview.hxx:87
constexpr zview(char const text[], std::ptrdiff_t len)
Convenience overload: construct using pointer and signed length.
Definition: zview.hxx:45
constexpr zview(char text[], std::ptrdiff_t len)
Convenience overload: construct using pointer and signed length.
Definition: zview.hxx:50
Marker-type wrapper: zero-terminated std::string_view.
Definition: zview.hxx:39
constexpr char const * as_c_string(char const str[]) noexcept
Get a raw C string pointer.
Definition: zview.hxx:131
The home of all libpqxx classes, functions, templates, etc.
Definition: array.hxx:25
constexpr zview() noexcept=default
constexpr zview(char const (&literal)[size])
Construct a zview from a string literal.
Definition: zview.hxx:83
constexpr zview(Args &&...args)
Construct from any initialiser you might use for std::string_view.
Definition: zview.hxx:58
zview(std::string const &str)
Definition: zview.hxx:63
STL namespace.
constexpr zview(char const str[])
Construct a zview from a C-style string.
Definition: zview.hxx:71
Internal items for libpqxx&#39; own use. Do not use these yourself.
Definition: composite.hxx:73