libpqxx
util.hxx
1 
11 #ifndef PQXX_H_UTIL
12 #define PQXX_H_UTIL
13 
14 #include "pqxx/compiler-public.hxx"
15 
16 #include <cstdio>
17 #include <cctype>
18 #include <iterator>
19 #include <memory>
20 #include <stdexcept>
21 #include <string>
22 #include <type_traits>
23 #include <typeinfo>
24 #include <vector>
25 
26 #include "pqxx/strconv.hxx"
27 
28 
30 namespace pqxx {}
31 
32 #include <pqxx/internal/libpq-forward.hxx>
33 
34 
35 namespace pqxx
36 {
38 template<typename T> inline void ignore_unused(T) {}
39 
40 
42 
44 struct PQXX_LIBEXPORT thread_safety_model
45 {
47  bool have_safe_strerror = true;
48 
50  bool safe_libpq;
51 
53  bool safe_query_cancel = true;
54 
56  bool safe_result_copy = true;
57 
59 
66 
68  std::string description;
69 };
70 
71 
73 PQXX_LIBEXPORT thread_safety_model describe_thread_safety() noexcept;
74 
75 
77 constexpr oid oid_none = 0;
78 
79 
84 
86 
94 template<typename ITER, typename ACCESS> inline
95 std::string separated_list( //[t00]
96  const std::string &sep,
97  ITER begin,
98  ITER end,
99  ACCESS access)
100 {
101  std::string result;
102  if (begin != end)
103  {
104  result = to_string(access(begin));
105  for (++begin; begin != end; ++begin)
106  {
107  result += sep;
108  result += to_string(access(begin));
109  }
110  }
111  return result;
112 }
113 
114 
116 template<typename ITER> inline std::string
117 separated_list(const std::string &sep, ITER begin, ITER end) //[t00]
118  { return separated_list(sep, begin, end, [](ITER i){ return *i; }); }
119 
120 
122 template<typename CONTAINER> inline auto
123 separated_list(const std::string &sep, const CONTAINER &c) //[t10]
124  /*
125  Always std::string; necessary because SFINAE doesn't work with the
126  contents of function bodies, so the check for iterability has to be in
127  the signature.
128  */
129  -> typename std::enable_if<
130  (
131  not std::is_void<decltype(std::begin(c))>::value
132  and not std::is_void<decltype(std::end(c))>::value
133  ),
134  std::string
135  >::type
136 {
137  return separated_list(sep, std::begin(c), std::end(c));
138 }
139 
140 
142 template<
143  typename TUPLE,
144  std::size_t INDEX=0,
145  typename ACCESS,
146  typename std::enable_if<
147  (INDEX == std::tuple_size<TUPLE>::value-1),
148  int
149  >::type=0
150 >
151 inline std::string
153  const std::string & /* sep */,
154  const TUPLE &t,
155  const ACCESS& access
156 )
157 {
158  return to_string(access(&std::get<INDEX>(t)));
159 }
160 
161 template<
162  typename TUPLE,
163  std::size_t INDEX=0,
164  typename ACCESS,
165  typename std::enable_if<
166  (INDEX < std::tuple_size<TUPLE>::value-1),
167  int
168  >::type=0
169 >
170 inline std::string
171 separated_list(const std::string &sep, const TUPLE &t, const ACCESS& access)
172 {
173  return
174  to_string(access(&std::get<INDEX>(t))) +
175  sep +
176  separated_list<TUPLE, INDEX+1>(sep, t, access);
177 }
178 
179 template<
180  typename TUPLE,
181  std::size_t INDEX=0,
182  typename std::enable_if<
183  (INDEX <= std::tuple_size<TUPLE>::value),
184  int
185  >::type=0
186 >
187 inline std::string
188 separated_list(const std::string &sep, const TUPLE &t)
189 {
190  return separated_list(sep, t, [](const TUPLE &tup){return *tup;});
191 }
193 
194 
196 
205 namespace internal
206 {
207 PQXX_LIBEXPORT void freepqmem(const void *) noexcept;
208 template<typename P> inline void freepqmem_templated(P *p) noexcept
209 {
210  freepqmem(p);
211 }
212 
213 PQXX_LIBEXPORT void freemallocmem(const void *) noexcept;
214 template<typename P> inline void freemallocmem_templated(P *p) noexcept
215 {
216  freemallocmem(p);
217 }
218 
219 
221 
233 class PQXX_LIBEXPORT namedclass
234 {
235 public:
236  explicit namedclass(const std::string &Classname) :
237  m_classname{Classname},
238  m_name{}
239  {
240  }
241 
242  namedclass(const std::string &Classname, const std::string &Name) :
243  m_classname{Classname},
244  m_name{Name}
245  {
246  }
247 
249  const std::string &name() const noexcept { return m_name; } //[t01]
250 
252  const std::string &classname() const noexcept //[t73]
253  { return m_classname; }
254 
256  std::string description() const;
257 
258 private:
259  std::string m_classname, m_name;
260 };
261 
262 
263 PQXX_PRIVATE void CheckUniqueRegistration(
264  const namedclass *New, const namedclass *Old);
265 PQXX_PRIVATE void CheckUniqueUnregistration(
266  const namedclass *New, const namedclass *Old);
267 
268 
270 
273 template<typename GUEST>
274 class unique
275 {
276 public:
277  unique() =default;
278  unique(const unique &) =delete;
279  unique &operator=(const unique &) =delete;
280 
281  GUEST *get() const noexcept { return m_guest; }
282 
283  void register_guest(GUEST *G)
284  {
285  CheckUniqueRegistration(G, m_guest);
286  m_guest = G;
287  }
288 
289  void unregister_guest(GUEST *G)
290  {
291  CheckUniqueUnregistration(G, m_guest);
292  m_guest = nullptr;
293  }
294 
295 private:
296  GUEST *m_guest = nullptr;
297 };
298 
299 
301 
304 PQXX_LIBEXPORT void sleep_seconds(int);
305 
306 } // namespace internal
307 } // namespace pqxx
308 
309 #endif
void CheckUniqueRegistration(const namedclass *New, const namedclass *Old)
Definition: util.cxx:72
void ignore_unused(T)
Suppress compiler warning about an unused item.
Definition: util.hxx:38
void freepqmem(const void *) noexcept
Definition: util.cxx:106
void CheckUniqueUnregistration(const namedclass *New, const namedclass *Old)
Definition: util.cxx:88
Helper base class: object descriptions for error messages and such.
Definition: util.hxx:233
thread_safety_model describe_thread_safety() noexcept
Describe thread safety available in this build.
Definition: util.cxx:31
STL namespace.
Result set containing data returned by a query or command.
Definition: result.hxx:69
std::string description
A human-readable description of any thread-safety issues.
Definition: util.hxx:68
bool safe_kerberos
Is Kerberos thread-safe?
Definition: util.hxx:65
void register_guest(GUEST *G)
Definition: util.hxx:283
std::string to_string(const field &Obj)
Convert a field to a string.
Definition: result.cxx:451
constexpr oid oid_none
The "null" oid.
Definition: util.hxx:77
void unregister_guest(GUEST *G)
Definition: util.hxx:289
Descriptor of library&#39;s thread-safety model.
Definition: util.hxx:44
void freemallocmem_templated(P *p) noexcept
Definition: util.hxx:214
void freepqmem_templated(P *p) noexcept
Definition: util.hxx:208
const std::string & name() const noexcept
Object name, or the empty string if no name was given.
Definition: util.hxx:249
Ensure proper opening/closing of GUEST objects related to a "host" object.
Definition: util.hxx:274
namedclass(const std::string &Classname)
Definition: util.hxx:236
namedclass(const std::string &Classname, const std::string &Name)
Definition: util.hxx:242
void freemallocmem(const void *) noexcept
Definition: util.cxx:112
bool safe_libpq
Is the underlying libpq build thread-safe?
Definition: util.hxx:50
const std::string & classname() const noexcept
Class name.
Definition: util.hxx:252
void sleep_seconds(int)
Sleep for the given number of seconds.
Definition: util.cxx:118
std::string separated_list(const std::string &sep, ITER begin, ITER end, ACCESS access)
Represent sequence of values as a string, joined by a given separator.
Definition: util.hxx:95
The home of all libpqxx classes, functions, templates, etc.
Definition: array.hxx:25