libpqxx  7.2.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_view>
17 #include <type_traits>
18 
19 
20 namespace pqxx
21 {
23 
36 class zview : public std::string_view
37 {
38 public:
39  constexpr zview() noexcept = default;
40 
42  constexpr zview(char const text[], std::ptrdiff_t len) :
43  std::string_view{text, static_cast<std::size_t>(len)}
44  {}
45 
47  constexpr zview(char text[], std::ptrdiff_t len) :
48  std::string_view{text, static_cast<std::size_t>(len)}
49  {}
50 
52 
54  template<typename... Args>
55  explicit constexpr zview(Args &&... args) :
56  std::string_view(std::forward<Args>(args)...)
57  {}
58 
59  /* implicit */
60  zview(std::string const &str) : std::string_view(str) {}
61 
62  /* implicit */
63  template<size_t size>
64  constexpr zview(char const (&literal)[size]) : zview(literal, size - 1)
65  {}
66 
68  [[nodiscard]] constexpr char const *c_str() const noexcept { return data(); }
69 };
70 } // namespace pqxx
71 
72 
73 #if defined(PQXX_HAVE_CONCEPTS)
74 namespace pqxx::internal
75 {
77 
81 template<typename T>
82 concept ZString =
83  std::is_convertible_v<T, char const *> or std::is_convertible_v<T, zview> or
84  std::is_convertible_v<T, std::string const &>;
85 } // namespace pqxx::internal
86 #endif // PQXX_HAVE_CONCEPTS
87 
88 
89 namespace pqxx::internal
90 {
92 inline constexpr char const *as_c_string(char const str[]) noexcept
93 {
94  return str;
95 }
97 template<std::size_t N>
98 inline constexpr char const *as_c_string(char (&str)[N]) noexcept
99 {
100  return str;
101 }
103 inline constexpr char const *as_c_string(pqxx::zview str) noexcept
104 {
105  return str.c_str();
106 }
107 // TODO: constexpr as of C++20.
109 inline char const *as_c_string(std::string const &str) noexcept
110 {
111  return str.c_str();
112 }
113 } // namespace pqxx::internal
114 
115 #endif
constexpr zview(char const text[], std::ptrdiff_t len)
Convenience overload: construct using pointer and signed length.
Definition: zview.hxx:42
Private namespace for libpqxx&#39;s internal use; do not access.
Definition: composite.hxx:70
constexpr char const * c_str() const noexcept
Either a null pointer, or a zero-terminated text buffer.
Definition: zview.hxx:68
constexpr char const * as_c_string(char const str[]) noexcept
Get a raw C string pointer.
Definition: zview.hxx:92
constexpr zview() noexcept=default
The home of all libpqxx classes, functions, templates, etc.
Definition: array.hxx:25
constexpr zview(Args &&... args)
Construct from any initialiser you might use for std::string_view.
Definition: zview.hxx:55
constexpr zview(char const (&literal)[size])
Definition: zview.hxx:64
zview(std::string const &str)
Definition: zview.hxx:60
STL namespace.
Marker-type wrapper: zero-terminated std::string_view.
Definition: zview.hxx:36
constexpr zview(char text[], std::ptrdiff_t len)
Convenience overload: construct using pointer and signed length.
Definition: zview.hxx:47