libpqxx  7.0.5
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 
136 inline PQXX_PRIVATE void check_version()
137 {
138  // There is no particular reason to do this here in @c connection, except
139  // to ensure that every meaningful libpqxx client will execute it. The call
140  // must be in the execution path somewhere or the compiler won't try to link
141  // it. We can't use it to initialise a global or class-static variable,
142  // because a smart compiler might resolve it at compile time.
143  //
144  // On the other hand, we don't want to make a useless function call too
145  // often for performance reasons. A local static variable is initialised
146  // only on the definition's first execution. Compilers will be well
147  // optimised for this behaviour, so there's a minimal one-time cost.
148  static auto const version_ok{internal::PQXX_VERSION_CHECK()};
149  ignore_unused(version_ok);
150 }
151 
152 
154 
156 struct PQXX_LIBEXPORT thread_safety_model
157 {
159  bool safe_libpq = false;
160 
162 
168  bool safe_kerberos = false;
169 
171  std::string description;
172 };
173 
174 
176 [[nodiscard]] PQXX_LIBEXPORT thread_safety_model describe_thread_safety();
177 
178 
180 constexpr oid oid_none{0};
181 } // namespace pqxx
182 
183 
185 
194 namespace pqxx::internal
195 {
197 
209 class PQXX_LIBEXPORT namedclass
210 {
211 public:
212  explicit namedclass(std::string_view classname) : m_classname{classname} {}
213 
214  namedclass(std::string_view classname, std::string_view name) :
215  m_classname{classname},
216  m_name{name}
217  {}
218 
219  namedclass(std::string_view classname, char const name[]) :
220  m_classname{classname},
221  m_name{name}
222  {}
223 
224  namedclass(std::string_view classname, std::string &&name) :
225  m_classname{classname},
226  m_name{std::move(name)}
227  {}
228 
230  std::string const &name() const noexcept { return m_name; }
231 
233  std::string const &classname() const noexcept { return m_classname; }
234 
236  std::string description() const;
237 
238 private:
239  std::string m_classname, m_name;
240 };
241 
242 
243 PQXX_PRIVATE void check_unique_registration(
244  namedclass const *new_ptr, namedclass const *old_ptr);
245 PQXX_PRIVATE void check_unique_unregistration(
246  namedclass const *new_ptr, namedclass const *old_ptr);
247 
248 
250 
253 template<typename GUEST> class unique
254 {
255 public:
256  constexpr unique() = default;
257  constexpr unique(unique const &) = delete;
258  constexpr unique(unique &&rhs) : m_guest(rhs.m_guest)
259  {
260  rhs.m_guest = nullptr;
261  }
262  constexpr unique &operator=(unique const &) = delete;
263  constexpr unique &operator=(unique &&rhs)
264  {
265  m_guest = rhs.m_guest;
266  rhs.m_guest = nullptr;
267  return *this;
268  }
269 
270  constexpr GUEST *get() const noexcept { return m_guest; }
271 
272  constexpr void register_guest(GUEST *G)
273  {
274  check_unique_registration(G, m_guest);
275  m_guest = G;
276  }
277 
278  constexpr void unregister_guest(GUEST *G)
279  {
280  check_unique_unregistration(G, m_guest);
281  m_guest = nullptr;
282  }
283 
284 private:
285  GUEST *m_guest = nullptr;
286 };
287 } // namespace pqxx::internal
288 
289 #include "pqxx/internal/compiler-internal-post.hxx"
290 #endif
constexpr void register_guest(GUEST *G)
Definition: util.hxx:272
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:59
std::string const & classname() const noexcept
Class name.
Definition: util.hxx:233
void check_unique_unregistration(namedclass const *new_ptr, namedclass const *old_ptr)
Definition: util.cxx:75
std::string description
A human-readable description of any thread-safety issues.
Definition: util.hxx:171
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, std::string &&name)
Definition: util.hxx:224
std::string const & name() const noexcept
Object name, or the empty string if no name was given.
Definition: util.hxx:230
The home of all libpqxx classes, functions, templates, etc.
Definition: array.hxx:25
namedclass(std::string_view classname)
Definition: util.hxx:212
constexpr oid oid_none
The "null" oid.
Definition: util.hxx:180
Ensure proper opening/closing of GUEST objects related to a "host" object.
Definition: util.hxx:253
void check_unique_registration(namedclass const *new_ptr, namedclass const *old_ptr)
Definition: util.cxx:60
thread_safety_model describe_thread_safety()
Describe thread safety available in this build.
Definition: util.cxx:26
constexpr unique(unique &&rhs)
Definition: util.hxx:258
int PQXX_VERSION_CHECK() noexcept
Library version check stub.
Definition: version.cxx:18
namedclass(std::string_view classname, std::string_view name)
Definition: util.hxx:214
void check_version()
Definition: util.hxx:136
void ignore_unused(T &&)
Suppress compiler warning about an unused item.
Definition: util.hxx:44
Helper base class: object descriptions for error messages and such.
Definition: util.hxx:209
constexpr unique & operator=(unique &&rhs)
Definition: util.hxx:263
constexpr void unregister_guest(GUEST *G)
Definition: util.hxx:278
namedclass(std::string_view classname, char const name[])
Definition: util.hxx:219
Descriptor of library&#39;s thread-safety model.
Definition: util.hxx:156