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 <typeinfo>
23 #include <vector>
24 
25 #include "pqxx/strconv.hxx"
26 
27 
29 namespace pqxx {}
30 
31 #include <pqxx/internal/libpq-forward.hxx>
32 
33 
34 namespace pqxx
35 {
37 template<typename T> inline void ignore_unused(T) {}
38 
40 
42 struct PQXX_LIBEXPORT thread_safety_model
43 {
45  bool have_safe_strerror = true;
46 
48 
55  bool safe_libpq;
56 
58  bool safe_query_cancel = true;
59 
61  bool safe_result_copy = true;
62 
64 
71 
73  std::string description;
74 };
75 
76 
78 PQXX_LIBEXPORT thread_safety_model describe_thread_safety() noexcept;
79 
80 
82 constexpr oid oid_none = 0;
83 
84 
89 
91 
99 template<typename ITER, typename ACCESS> inline
100 std::string separated_list( //[t00]
101  const std::string &sep,
102  ITER begin,
103  ITER end,
104  ACCESS access)
105 {
106  std::string result;
107  if (begin != end)
108  {
109  result = to_string(access(begin));
110  for (++begin; begin != end; ++begin)
111  {
112  result += sep;
113  result += to_string(access(begin));
114  }
115  }
116  return result;
117 }
118 
119 
121 template<typename ITER> inline std::string
122 separated_list(const std::string &sep, ITER begin, ITER end) //[t00]
123  { return separated_list(sep, begin, end, [](ITER i){ return *i; }); }
124 
125 
127 template<typename CONTAINER> inline std::string
128 separated_list(const std::string &sep, const CONTAINER &c) //[t10]
129  { return separated_list(sep, std::begin(c), std::end(c)); }
131 
132 
134 
143 namespace internal
144 {
145 PQXX_LIBEXPORT void freepqmem(const void *) noexcept;
146 template<typename P> inline void freepqmem_templated(P *p) noexcept
147 {
148  freepqmem(p);
149 }
150 
151 PQXX_LIBEXPORT void freemallocmem(const void *) noexcept;
152 template<typename P> inline void freemallocmem_templated(P *p) noexcept
153 {
154  freemallocmem(p);
155 }
156 
157 
159 
171 class PQXX_LIBEXPORT namedclass
172 {
173 public:
174  explicit namedclass(const std::string &Classname) :
175  m_classname(Classname),
176  m_name()
177  {
178  }
179 
180  namedclass(const std::string &Classname, const std::string &Name) :
181  m_classname(Classname),
182  m_name(Name)
183  {
184  }
185 
187  const std::string &name() const noexcept { return m_name; } //[t01]
188 
190  const std::string &classname() const noexcept //[t73]
191  { return m_classname; }
192 
194  std::string description() const;
195 
196 private:
197  std::string m_classname, m_name;
198 };
199 
200 
201 PQXX_PRIVATE void CheckUniqueRegistration(
202  const namedclass *New, const namedclass *Old);
203 PQXX_PRIVATE void CheckUniqueUnregistration(
204  const namedclass *New, const namedclass *Old);
205 
206 
208 
211 template<typename GUEST>
212 class unique
213 {
214 public:
215  unique() =default;
216  unique(const unique &) =delete;
217  unique &operator=(const unique &) =delete;
218 
219  GUEST *get() const noexcept { return m_guest; }
220 
221  void register_guest(GUEST *G)
222  {
223  CheckUniqueRegistration(G, m_guest);
224  m_guest = G;
225  }
226 
227  void unregister_guest(GUEST *G)
228  {
229  CheckUniqueUnregistration(G, m_guest);
230  m_guest = nullptr;
231  }
232 
233 private:
234  GUEST *m_guest = nullptr;
235 };
236 
237 
239 
242 PQXX_LIBEXPORT void sleep_seconds(int);
243 
245 using cstring = const char *;
246 
247 
249 constexpr char
250  sql_begin_work[] = "BEGIN",
251  sql_commit_work[] = "COMMIT",
252  sql_rollback_work[] = "ROLLBACK";
253 
254 } // namespace internal
255 } // namespace pqxx
256 
257 #endif
void freemallocmem(const void *) noexcept
Definition: util.cxx:109
Result set containing data returned by a query or command.
Definition: result.hxx:65
const std::string & classname() const noexcept
Class name.
Definition: util.hxx:190
std::string to_string(const field &Obj)
Convert a field to a string.
Definition: result.cxx:434
std::string separated_list(const std::string &sep, ITER begin, ITER end, ACCESS access)
Represent sequence of values as a separated string.
Definition: util.hxx:100
Ensure proper opening/closing of GUEST objects related to a "host" object.
Definition: util.hxx:212
void register_guest(GUEST *G)
Definition: util.hxx:221
bool safe_libpq
Is the underlying libpq build thread-safe?
Definition: util.hxx:55
namedclass(const std::string &Classname)
Definition: util.hxx:174
void sleep_seconds(int)
Sleep for the given number of seconds.
Definition: util.cxx:115
constexpr char sql_rollback_work[]
Definition: util.hxx:252
thread_safety_model describe_thread_safety() noexcept
Describe thread safety available in this build.
Definition: util.cxx:28
const char * cstring
Work around problem with library export directives and pointers.
Definition: util.hxx:245
const std::string & name() const noexcept
Object name, or the empty string if no name was given.
Definition: util.hxx:187
void CheckUniqueRegistration(const namedclass *New, const namedclass *Old)
Definition: util.cxx:69
constexpr char sql_commit_work[]
Definition: util.hxx:251
The home of all libpqxx classes, functions, templates, etc.
Definition: array.hxx:22
constexpr char sql_begin_work[]
Commonly used SQL commands.
Definition: util.hxx:250
Helper base class: object descriptions for error messages and such.
Definition: util.hxx:171
constexpr oid oid_none
The "null" oid.
Definition: util.hxx:82
bool safe_kerberos
Is Kerberos thread-safe?
Definition: util.hxx:70
void unregister_guest(GUEST *G)
Definition: util.hxx:227
Descriptor of library&#39;s thread-safety model.
Definition: util.hxx:42
void CheckUniqueUnregistration(const namedclass *New, const namedclass *Old)
Definition: util.cxx:85
STL namespace.
void freemallocmem_templated(P *p) noexcept
Definition: util.hxx:152
namedclass(const std::string &Classname, const std::string &Name)
Definition: util.hxx:180
void freepqmem(const void *) noexcept
Definition: util.cxx:103
void freepqmem_templated(P *p) noexcept
Definition: util.hxx:146
std::string description
A human-readable description of any thread-safety issues.
Definition: util.hxx:73
void ignore_unused(T)
Suppress compiler warning about an unused item.
Definition: util.hxx:37