libpqxx  7.3.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-2020, 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 
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) :
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 
55  template<typename... Args>
56  explicit constexpr zview(Args &&...args) :
57  std::string_view(std::forward<Args>(args)...)
58  {}
59 
61  zview(std::string const &str) : std::string_view{str.c_str(), std::size(str)}
62  {}
63 
65 
69  constexpr zview(char const str[]) : std::string_view{str} {}
70 
71  template<size_t size>
72  constexpr zview(char const (&literal)[size]) : zview(literal, size - 1)
73  {}
74 
76  [[nodiscard]] constexpr char const *c_str() const noexcept { return data(); }
77 };
78 
79 
81 
86 constexpr zview operator"" _zv(char const str[], std::size_t len) noexcept
87 {
88  return zview{str, len};
89 }
90 } // namespace pqxx
91 
92 
93 #if defined(PQXX_HAVE_CONCEPTS)
94 namespace pqxx::internal
95 {
97 
101 template<typename T>
102 concept ZString =
103  std::is_convertible_v<T, char const *> or std::is_convertible_v<T, zview> or
104  std::is_convertible_v<T, std::string const &>;
105 } // namespace pqxx::internal
106 #endif // PQXX_HAVE_CONCEPTS
107 
108 
109 namespace pqxx::internal
110 {
112 inline constexpr char const *as_c_string(char const str[]) noexcept
113 {
114  return str;
115 }
117 template<std::size_t N>
118 inline constexpr char const *as_c_string(char (&str)[N]) noexcept
119 {
120  return str;
121 }
123 inline constexpr char const *as_c_string(pqxx::zview str) noexcept
124 {
125  return str.c_str();
126 }
127 // TODO: constexpr as of C++20.
129 inline char const *as_c_string(std::string const &str) noexcept
130 {
131  return str.c_str();
132 }
133 } // namespace pqxx::internal
134 
135 #endif
pqxx::zview::zview
constexpr zview(char text[], std::ptrdiff_t len)
Convenience overload: construct using pointer and signed length.
Definition: zview.hxx:48
pqxx
The home of all libpqxx classes, functions, templates, etc.
Definition: array.hxx:26
pqxx::zview::zview
constexpr zview(Args &&...args)
Construct from any initialiser you might use for std::string_view.
Definition: zview.hxx:56
pqxx::internal
Internal items for libpqxx' own use. Do not use these yourself.
Definition: composite.hxx:74
pqxx::zview::zview
zview(std::string const &str)
Definition: zview.hxx:61
pqxx::internal::as_c_string
constexpr char const * as_c_string(char const str[]) noexcept
Get a raw C string pointer.
Definition: zview.hxx:112
pqxx::zview::zview
constexpr zview() noexcept=default
pqxx::zview::zview
constexpr zview(char const str[])
Construct a zview from a C-style string.
Definition: zview.hxx:69
pqxx::zview
Marker-type wrapper: zero-terminated std::string_view.
Definition: zview.hxx:38
pqxx::zview::c_str
constexpr char const * c_str() const noexcept
Either a null pointer, or a zero-terminated text buffer.
Definition: zview.hxx:76
pqxx::zview::zview
constexpr zview(char const (&literal)[size])
Definition: zview.hxx:72