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 <memory>
19 #include <stdexcept>
20 #include <string>
21 #include <typeinfo>
22 #include <vector>
23 
24 #include "pqxx/strconv.hxx"
25 
26 
28 namespace pqxx {}
29 
30 #include <pqxx/internal/libpq-forward.hxx>
31 
32 
33 namespace pqxx
34 {
36 template<typename T> inline void ignore_unused(T) {}
37 
39 
41 struct PQXX_LIBEXPORT thread_safety_model
42 {
44  bool have_safe_strerror = true;
45 
47 
54  bool safe_libpq;
55 
57  bool safe_query_cancel = true;
58 
60  bool safe_result_copy = true;
61 
63 
70 
72  std::string description;
73 };
74 
75 
77 PQXX_LIBEXPORT thread_safety_model describe_thread_safety() noexcept;
78 
79 
81 constexpr oid oid_none = 0;
82 
83 
88 
90 
98 template<typename ITER, typename ACCESS> inline
99 std::string separated_list( //[t00]
100  const std::string &sep,
101  ITER begin,
102  ITER end,
103  ACCESS access)
104 {
105  std::string result;
106  if (begin != end)
107  {
108  result = to_string(access(begin));
109  for (++begin; begin != end; ++begin)
110  {
111  result += sep;
112  result += to_string(access(begin));
113  }
114  }
115  return result;
116 }
117 
118 
120 template<typename ITER> inline std::string
121 separated_list(const std::string &sep, ITER begin, ITER end) //[t00]
122  { return separated_list(sep, begin, end, [](ITER i){ return *i; }); }
123 
124 
126 template<typename CONTAINER> inline std::string
127 separated_list(const std::string &sep, const CONTAINER &c) //[t10]
128  { return separated_list(sep, c.begin(), c.end()); }
130 
131 
133 
142 namespace internal
143 {
144 PQXX_LIBEXPORT void freepqmem(const void *) noexcept;
145 template<typename P> inline void freepqmem_templated(P *p) noexcept
146 {
147  freepqmem(p);
148 }
149 
150 PQXX_LIBEXPORT void freemallocmem(const void *) noexcept;
151 template<typename P> inline void freemallocmem_templated(P *p) noexcept
152 {
153  freemallocmem(p);
154 }
155 
156 
158 
170 class PQXX_LIBEXPORT namedclass
171 {
172 public:
173  explicit namedclass(const std::string &Classname) :
174  m_classname(Classname),
175  m_name()
176  {
177  }
178 
179  namedclass(const std::string &Classname, const std::string &Name) :
180  m_classname(Classname),
181  m_name(Name)
182  {
183  }
184 
186  const std::string &name() const noexcept { return m_name; } //[t01]
187 
189  const std::string &classname() const noexcept //[t73]
190  { return m_classname; }
191 
193  std::string description() const;
194 
195 private:
196  std::string m_classname, m_name;
197 };
198 
199 
200 PQXX_PRIVATE void CheckUniqueRegistration(
201  const namedclass *New, const namedclass *Old);
202 PQXX_PRIVATE void CheckUniqueUnregistration(
203  const namedclass *New, const namedclass *Old);
204 
205 
207 
210 template<typename GUEST>
211 class unique
212 {
213 public:
214  unique() =default;
215  unique(const unique &) =delete;
216  unique &operator=(const unique &) =delete;
217 
218  GUEST *get() const noexcept { return m_guest; }
219 
220  void register_guest(GUEST *G)
221  {
222  CheckUniqueRegistration(G, m_guest);
223  m_guest = G;
224  }
225 
226  void unregister_guest(GUEST *G)
227  {
228  CheckUniqueUnregistration(G, m_guest);
229  m_guest = nullptr;
230  }
231 
232 private:
233  GUEST *m_guest = nullptr;
234 };
235 
236 
238 
241 PQXX_LIBEXPORT void sleep_seconds(int);
242 
244 using cstring = const char *;
245 
246 
248 constexpr char
249  sql_begin_work[] = "BEGIN",
250  sql_commit_work[] = "COMMIT",
251  sql_rollback_work[] = "ROLLBACK";
252 
253 } // namespace internal
254 } // namespace pqxx
255 
256 #endif
void freemallocmem(const void *) noexcept
Definition: util.cxx:109
Result set containing data returned by a query or command.
Definition: result.hxx:65
namedclass(const std::string &Classname, const std::string &Name)
Definition: util.hxx:179
const std::string & classname() const noexcept
Class name.
Definition: util.hxx:189
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:99
void register_guest(GUEST *G)
Definition: util.hxx:220
Descriptor of library&#39;s thread-safety model.
Definition: util.hxx:41
namedclass(const std::string &Classname)
Definition: util.hxx:173
std::string description
A human-readable description of any thread-safety issues.
Definition: util.hxx:72
void sleep_seconds(int)
Sleep for the given number of seconds.
Definition: util.cxx:115
constexpr char sql_rollback_work[]
Definition: util.hxx:251
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:244
bool safe_kerberos
Is Kerberos thread-safe?
Definition: util.hxx:69
bool safe_libpq
Is the underlying libpq build thread-safe?
Definition: util.hxx:54
void CheckUniqueRegistration(const namedclass *New, const namedclass *Old)
Definition: util.cxx:69
constexpr char sql_commit_work[]
Definition: util.hxx:250
The home of all libpqxx classes, functions, templates, etc.
Definition: array.hxx:22
void unregister_guest(GUEST *G)
Definition: util.hxx:226
constexpr char sql_begin_work[]
Commonly used SQL commands.
Definition: util.hxx:249
Ensure proper opening/closing of GUEST objects related to a "host" object.
Definition: util.hxx:211
constexpr oid oid_none
The "null" oid.
Definition: util.hxx:81
Helper base class: object descriptions for error messages and such.
Definition: util.hxx:170
void CheckUniqueUnregistration(const namedclass *New, const namedclass *Old)
Definition: util.cxx:85
STL namespace.
const std::string & name() const noexcept
Object name, or the empty string if no name was given.
Definition: util.hxx:186
void freemallocmem_templated(P *p) noexcept
Definition: util.hxx:151
void freepqmem(const void *) noexcept
Definition: util.cxx:103
void freepqmem_templated(P *p) noexcept
Definition: util.hxx:145
void ignore_unused(T)
Suppress compiler warning about an unused item.
Definition: util.hxx:36