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 
38 struct PQXX_LIBEXPORT thread_safety_model
39 {
41  bool have_safe_strerror = true;
42 
44 
51  bool safe_libpq;
52 
54  bool safe_query_cancel = true;
55 
57  bool safe_result_copy = true;
58 
60 
67 
69  std::string description;
70 };
71 
72 
74 PQXX_LIBEXPORT thread_safety_model describe_thread_safety() noexcept;
75 
76 
78 constexpr oid oid_none = 0;
79 
80 
85 
87 
95 template<typename ITER, typename ACCESS> inline
96 std::string separated_list( //[t00]
97  const std::string &sep,
98  ITER begin,
99  ITER end,
100  ACCESS access)
101 {
102  std::string result;
103  if (begin != end)
104  {
105  result = to_string(access(begin));
106  for (++begin; begin != end; ++begin)
107  {
108  result += sep;
109  result += to_string(access(begin));
110  }
111  }
112  return result;
113 }
114 
115 
117 template<typename ITER> inline std::string
118 separated_list(const std::string &sep, ITER begin, ITER end) //[t00]
119  { return separated_list(sep, begin, end, [](ITER i){ return *i; }); }
120 
121 
123 template<typename CONTAINER> inline std::string
124 separated_list(const std::string &sep, const CONTAINER &c) //[t10]
125  { return separated_list(sep, c.begin(), c.end()); }
127 
128 
130 
139 namespace internal
140 {
141 PQXX_LIBEXPORT void freepqmem(const void *) noexcept;
142 template<typename P> inline void freepqmem_templated(P *p) noexcept
143 {
144  freepqmem(p);
145 }
146 
147 PQXX_LIBEXPORT void freemallocmem(const void *) noexcept;
148 template<typename P> inline void freemallocmem_templated(P *p) noexcept
149 {
150  freemallocmem(p);
151 }
152 
153 
155 
167 class PQXX_LIBEXPORT namedclass
168 {
169 public:
170  explicit namedclass(const std::string &Classname) :
171  m_classname(Classname),
172  m_name()
173  {
174  }
175 
176  namedclass(const std::string &Classname, const std::string &Name) :
177  m_classname(Classname),
178  m_name(Name)
179  {
180  }
181 
183  const std::string &name() const noexcept { return m_name; } //[t01]
184 
186  const std::string &classname() const noexcept //[t73]
187  { return m_classname; }
188 
190  std::string description() const;
191 
192 private:
193  std::string m_classname, m_name;
194 };
195 
196 
197 PQXX_PRIVATE void CheckUniqueRegistration(
198  const namedclass *New, const namedclass *Old);
199 PQXX_PRIVATE void CheckUniqueUnregistration(
200  const namedclass *New, const namedclass *Old);
201 
202 
204 
207 template<typename GUEST>
208 class unique
209 {
210 public:
211  unique() =default;
212  unique(const unique &) =delete;
213  unique &operator=(const unique &) =delete;
214 
215  GUEST *get() const noexcept { return m_guest; }
216 
217  void register_guest(GUEST *G)
218  {
219  CheckUniqueRegistration(G, m_guest);
220  m_guest = G;
221  }
222 
223  void unregister_guest(GUEST *G)
224  {
225  CheckUniqueUnregistration(G, m_guest);
226  m_guest = nullptr;
227  }
228 
229 private:
230  GUEST *m_guest = nullptr;
231 };
232 
233 
235 
238 PQXX_LIBEXPORT void sleep_seconds(int);
239 
241 using cstring = const char *;
242 
243 
245 constexpr char
246  sql_begin_work[] = "BEGIN",
247  sql_commit_work[] = "COMMIT",
248  sql_rollback_work[] = "ROLLBACK";
249 
250 } // namespace internal
251 } // namespace pqxx
252 
253 #endif
const std::string & name() const noexcept
Object name, or the empty string if no name was given.
Definition: util.hxx:183
void freemallocmem(const void *) noexcept
Definition: util.cxx:109
namedclass(const std::string &Classname, const std::string &Name)
Definition: util.hxx:176
Descriptor of library&#39;s thread-safety model.
Definition: util.hxx:38
bool safe_libpq
Is the underlying libpq build thread-safe?
Definition: util.hxx:51
constexpr char sql_rollback_work[]
Definition: util.hxx:248
constexpr char sql_begin_work[]
Commonly used SQL commands.
Definition: util.hxx:246
The home of all libpqxx classes, functions, templates, etc.
Definition: array.hxx:22
void sleep_seconds(int)
Sleep for the given number of seconds.
Definition: util.cxx:115
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:96
Ensure proper opening/closing of GUEST objects related to a "host" object.
Definition: util.hxx:208
namedclass(const std::string &Classname)
Definition: util.hxx:170
const std::string & classname() const noexcept
Class name.
Definition: util.hxx:186
void CheckUniqueRegistration(const namedclass *New, const namedclass *Old)
Definition: util.cxx:69
Result set containing data returned by a query or command.
Definition: result.hxx:67
bool safe_kerberos
Is Kerberos thread-safe?
Definition: util.hxx:66
constexpr char sql_commit_work[]
Definition: util.hxx:247
void CheckUniqueUnregistration(const namedclass *New, const namedclass *Old)
Definition: util.cxx:85
STL namespace.
void register_guest(GUEST *G)
Definition: util.hxx:217
void freepqmem(const void *) noexcept
Definition: util.cxx:103
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:241
std::string description
A human-readable description of any thread-safety issues.
Definition: util.hxx:69
std::string to_string(const field &Obj)
Convert a field to a string.
Definition: result.cxx:434
Helper base class: object descriptions for error messages and such.
Definition: util.hxx:167
void unregister_guest(GUEST *G)
Definition: util.hxx:223
void freemallocmem_templated(P *p) noexcept
Definition: util.hxx:148
constexpr oid oid_none
The "null" oid.
Definition: util.hxx:78
void freepqmem_templated(P *p) noexcept
Definition: util.hxx:142