libpqxx  7.1.0
util.hxx
1 /* Various utility definitions for libpqxx.
2  *
3  * DO NOT INCLUDE THIS FILE DIRECTLY; include pqxx/util 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_UTIL
12 #define PQXX_H_UTIL
13 
14 #include "pqxx/compiler-public.hxx"
15 #include "pqxx/internal/compiler-internal-pre.hxx"
16 
17 #include <cctype>
18 #include <cstdio>
19 #include <iterator>
20 #include <limits>
21 #include <memory>
22 #include <stdexcept>
23 #include <string>
24 #include <string_view>
25 #include <type_traits>
26 #include <typeinfo>
27 #include <vector>
28 
29 #include "pqxx/except.hxx"
30 #include "pqxx/types.hxx"
31 #include "pqxx/version.hxx"
32 
33 
35 namespace pqxx
36 {}
37 
38 #include <pqxx/internal/libpq-forward.hxx>
39 
40 
41 namespace pqxx
42 {
44 template<typename... T> inline void ignore_unused(T &&...) {}
45 
46 
48 
51 template<typename TO, typename FROM>
52 inline TO check_cast(FROM value, char const description[])
53 {
54  static_assert(std::is_arithmetic_v<FROM>);
55  static_assert(std::is_arithmetic_v<TO>);
56  static_assert(std::is_integral_v<FROM> == std::is_integral_v<TO>);
57 
58  // The rest of this code won't quite work for bool, but bool is trivially
59  // convertible to other arithmetic types as far as I can see.
60  if constexpr (std::is_same_v<FROM, bool>)
61  return static_cast<TO>(value);
62 
63  // Depending on our "if constexpr" conditions, this parameter may not be
64  // needed. Some compilers will warn.
65  ignore_unused(description);
66 
67  using from_limits = std::numeric_limits<decltype(value)>;
68  using to_limits = std::numeric_limits<TO>;
69  if constexpr (std::is_signed_v<FROM>)
70  {
71  if constexpr (std::is_signed_v<TO>)
72  {
73  if (value < (to_limits::min)())
74  throw range_error(std::string{"Cast underflow: "} + description);
75  }
76  else
77  {
78  // FROM is signed, but TO is not. Treat this as a special case, because
79  // there may not be a good broader type in which the compiler can even
80  // perform our check.
81  if (value < 0)
82  throw range_error(
83  std::string{"Casting negative value to unsigned type: "} +
84  description);
85  }
86  }
87  else
88  {
89  // No need to check: the value is unsigned so can't fall below the range
90  // of the TO type.
91  }
92 
93  if constexpr (std::is_integral_v<FROM>)
94  {
95  using unsigned_from = std::make_unsigned_t<FROM>;
96  using unsigned_to = std::make_unsigned_t<TO>;
97  constexpr auto from_max{static_cast<unsigned_from>((from_limits::max)())};
98  constexpr auto to_max{static_cast<unsigned_to>((to_limits::max)())};
99  if constexpr (from_max > to_max)
100  {
101  if (static_cast<unsigned_from>(value) > to_max)
102  throw range_error(std::string{"Cast overflow: "} + description);
103  }
104  }
105  else if constexpr ((from_limits::max)() > (to_limits::max)())
106  {
107  if (value > (to_limits::max)())
108  throw range_error(std::string{"Cast overflow: "} + description);
109  }
110 
111  return static_cast<TO>(value);
112 }
113 
114 
116 template<typename TYPE>
117 using strip_t = std::remove_cv_t<std::remove_reference_t<TYPE>>;
118 
119 
141 inline PQXX_PRIVATE void check_version()
142 {
143  // There is no particular reason to do this here in @c connection, except
144  // to ensure that every meaningful libpqxx client will execute it. The call
145  // must be in the execution path somewhere or the compiler won't try to link
146  // it. We can't use it to initialise a global or class-static variable,
147  // because a smart compiler might resolve it at compile time.
148  //
149  // On the other hand, we don't want to make a useless function call too
150  // often for performance reasons. A local static variable is initialised
151  // only on the definition's first execution. Compilers will be well
152  // optimised for this behaviour, so there's a minimal one-time cost.
153  static auto const version_ok{internal::PQXX_VERSION_CHECK()};
154  ignore_unused(version_ok);
155 }
156 
157 
159 
161 struct PQXX_LIBEXPORT thread_safety_model
162 {
164  bool safe_libpq = false;
165 
167 
173  bool safe_kerberos = false;
174 
176  std::string description;
177 };
178 
179 
181 [[nodiscard]] PQXX_LIBEXPORT thread_safety_model describe_thread_safety();
182 
183 
185 constexpr oid oid_none{0};
186 } // namespace pqxx
187 
188 
190 
199 namespace pqxx::internal
200 {
202 
214 class PQXX_LIBEXPORT namedclass
215 {
216 public:
217  explicit namedclass(std::string_view classname) : m_classname{classname} {}
218 
219  namedclass(std::string_view classname, std::string_view name) :
220  m_classname{classname},
221  m_name{name}
222  {}
223 
224  namedclass(std::string_view classname, char const name[]) :
225  m_classname{classname},
226  m_name{name}
227  {}
228 
229  namedclass(std::string_view classname, std::string &&name) :
230  m_classname{classname},
231  m_name{std::move(name)}
232  {}
233 
235  std::string const &name() const noexcept { return m_name; }
236 
238  std::string const &classname() const noexcept { return m_classname; }
239 
241  std::string description() const;
242 
243 private:
244  std::string m_classname, m_name;
245 };
246 
247 
248 PQXX_PRIVATE void check_unique_registration(
249  namedclass const *new_ptr, namedclass const *old_ptr);
250 PQXX_PRIVATE void check_unique_unregistration(
251  namedclass const *new_ptr, namedclass const *old_ptr);
252 
253 
255 
258 template<typename GUEST> class unique
259 {
260 public:
261  constexpr unique() = default;
262  constexpr unique(unique const &) = delete;
263  constexpr unique(unique &&rhs) : m_guest(rhs.m_guest)
264  {
265  rhs.m_guest = nullptr;
266  }
267  constexpr unique &operator=(unique const &) = delete;
268  constexpr unique &operator=(unique &&rhs)
269  {
270  m_guest = rhs.m_guest;
271  rhs.m_guest = nullptr;
272  return *this;
273  }
274 
275  constexpr GUEST *get() const noexcept { return m_guest; }
276 
277  constexpr void register_guest(GUEST *G)
278  {
279  check_unique_registration(G, m_guest);
280  m_guest = G;
281  }
282 
283  constexpr void unregister_guest(GUEST *G)
284  {
285  check_unique_unregistration(G, m_guest);
286  m_guest = nullptr;
287  }
288 
289 private:
290  GUEST *m_guest = nullptr;
291 };
292 } // namespace pqxx::internal
293 
294 #include "pqxx/internal/compiler-internal-post.hxx"
295 #endif
Something is out of range, similar to std::out_of_range.
Definition: except.hxx:192
Private namespace for libpqxx&#39;s internal use; do not access.
Definition: connection.hxx:60
namedclass(std::string_view classname)
Definition: util.hxx:217
constexpr void register_guest(GUEST *G)
Definition: util.hxx:277
void check_unique_unregistration(namedclass const *new_ptr, namedclass const *old_ptr)
Definition: util.cxx:75
constexpr unique & operator=(unique &&rhs)
Definition: util.hxx:268
namedclass(std::string_view classname, std::string &&name)
Definition: util.hxx:229
TO check_cast(FROM value, char const description[])
Cast a numeric value to another type, or throw if it underflows/overflows.
Definition: util.hxx:52
namedclass(std::string_view classname, char const name[])
Definition: util.hxx:224
std::remove_cv_t< std::remove_reference_t< TYPE > > strip_t
Remove any constness, volatile, and reference-ness from a type.
Definition: util.hxx:117
std::string const & name() const noexcept
Object name, or the empty string if no name was given.
Definition: util.hxx:235
The home of all libpqxx classes, functions, templates, etc.
Definition: array.hxx:25
constexpr oid oid_none
The "null" oid.
Definition: util.hxx:185
Helper base class: object descriptions for error messages and such.
Definition: util.hxx:214
void check_unique_registration(namedclass const *new_ptr, namedclass const *old_ptr)
Definition: util.cxx:60
namedclass(std::string_view classname, std::string_view name)
Definition: util.hxx:219
thread_safety_model describe_thread_safety()
Describe thread safety available in this build.
Definition: util.cxx:26
constexpr unique(unique &&rhs)
Definition: util.hxx:263
std::string description
A human-readable description of any thread-safety issues.
Definition: util.hxx:176
int PQXX_VERSION_CHECK() noexcept
Library version check stub.
Definition: version.cxx:18
void check_version()
Definition: util.hxx:141
Descriptor of library&#39;s thread-safety model.
Definition: util.hxx:161
void ignore_unused(T &&...)
Suppress compiler warning about an unused item.
Definition: util.hxx:44
std::string const & classname() const noexcept
Class name.
Definition: util.hxx:238
Ensure proper opening/closing of GUEST objects related to a "host" object.
Definition: util.hxx:258
constexpr void unregister_guest(GUEST *G)
Definition: util.hxx:283