OCILIB (C and C++ Driver for Oracle)  4.7.6
Open source and cross platform Oracle Driver delivering efficient access to Oracle databases.
core.hpp
1 /*
2  * OCILIB - C Driver for Oracle (C Wrapper for Oracle OCI)
3  *
4  * Website: http://www.ocilib.net
5  *
6  * Copyright (c) 2007-2023 Vincent ROGIER <vince.rogier@ocilib.net>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */
20 
21 #pragma once
22 
23 #include <list>
24 #include <map>
25 
26 #include "ocilibcpp/config.hpp"
27 
28 // ReSharper disable CppClangTidyCppcoreguidelinesMacroUsage
29 // ReSharper disable CppClangTidyHicppSpecialMemberFunctions
30 // ReSharper disable CppClangTidyCppcoreguidelinesSpecialMemberFunctions
31 // ReSharper disable CppClangTidyModernizeUseNodiscard
32 // ReSharper disable CppClangTidyHicppUseEqualsDefault
33 // ReSharper disable CppClangTidyModernizeUseEqualsDefault
34 
35 namespace ocilib
36 {
43  namespace core
44  {
45 #ifdef OCILIBPP_HAS_ENABLEIF
46 
47  template<bool B, class T = void>
48  using EnableIf = std::enable_if<B, T>;
49 
50  template<class T, class U>
51  using IsSame = std::is_same<T, U>;
52 
53 #else
54 
55  template<bool B, class T = void>
56  struct EnableIf {};
57 
58  template<class T>
59  struct EnableIf<true, T> { typedef T type; };
60 
61  template<bool B>
62  struct BoolConstant { static const bool value = B; };
63 
64  template<class T, class U>
65  struct IsSame : BoolConstant<false> {};
66 
67  template<class T>
68  struct IsSame<T, T> : BoolConstant<true> {};
69 
70 #endif
71 
72 #define ARG_NOT_USED(a) (a) = (a)
73 
79  template<class T>
80  static T Check(T result);
81 
86  ostring MakeString(const otext* result, int size = -1);
87 
92  Raw MakeRaw(AnyPointer result, unsigned int size);
93 
98  template<class T>
100  {
101  typedef EnableIf<IsSame<T, short>::value ||
102  IsSame<T, unsigned short>::value ||
103  IsSame<T, int>::value ||
104  IsSame<T, unsigned int>::value ||
105  IsSame<T, big_int>::value ||
106  IsSame<T, big_uint>::value ||
107  IsSame<T, float>::value ||
108  IsSame<T, double>::value ||
109  IsSame<T, Number>::value> Type;
110  };
111 
116  template<class T>
117  class Enum
118  {
119  public:
120 
121  typedef T Type;
122 
123  Enum();
124  Enum(T value);
125 
126  T GetValue();
127 
128  operator T ();
129  operator unsigned int() const;
130 
131  bool operator == (const Enum& other) const;
132  bool operator != (const Enum& other) const;
133 
134  bool operator == (const T& other) const;
135  bool operator != (const T& other) const;
136 
137  private:
138 
139  T _value;
140  };
141 
146  template<class T>
147  class Flags
148  {
149  public:
150 
151  typedef T Type;
152 
153  Flags();
154  Flags(T flag);
155  Flags(const Flags& other);
156 
157  Flags& operator = (const Flags& other) noexcept;
158 
159  Flags operator~ () const;
160 
161  Flags operator | (T other) const;
162  Flags operator & (T other) const;
163  Flags operator ^ (T other) const;
164 
165  Flags operator | (const Flags& other) const;
166  Flags operator & (const Flags& other) const;
167  Flags operator ^ (const Flags& other) const;
168 
169  Flags& operator |= (T other);
170  Flags& operator &= (T other);
171  Flags& operator ^= (T other);
172 
173  Flags& operator |= (const Flags& other);
174  Flags& operator &= (const Flags& other);
175  Flags& operator ^= (const Flags& other);
176 
177  bool operator == (T other) const;
178  bool operator == (const Flags& other) const;
179 
180  unsigned int GetValues() const;
181 
182  bool IsSet(T other) const;
183 
184  private:
185 
186  Flags(unsigned int flags);
187 
188  unsigned int _flags;
189  };
190 
195  template< typename T>
197  {
198  public:
199  ManagedBuffer();
200  ManagedBuffer(size_t size);
201 
202  ~ManagedBuffer() noexcept;
203 
204  operator T* ();
205 
206  private:
207 
208  T* _buffer;
209  size_t _size;
210  };
211 
217  {
218  Unsafe,
219  Safe
220  };
221 
227  {
228  public:
229 
231  virtual ~SynchronizationGuard() noexcept;
232 
233  void Acquire() const;
234  void Release() const;
235 
236  void SetMode(SynchronizationMode mode);
237 
238  private:
239 
240  MutexHandle _mutex;
241  };
242 
248  {
249  public:
250 
251  Synchronizable();
252  virtual ~Synchronizable() noexcept;
253 
254  void SetGuard(SynchronizationGuard* guard);
255 
256  void Acquire() const;
257  void Release() const;
258 
259  private:
260 
261  SynchronizationGuard* _guard;
262  };
263 
268  template<class K, class V>
270  {
271  public:
272 
273  ConcurrentMap();
274  virtual ~ConcurrentMap() noexcept;
275 
276  void Remove(K key);
277  V Get(K key);
278  void Set(K key, V value);
279  void Clear();
280  size_t GetSize();
281 
282  private:
283 
284  std::map<K, V> _map;
285 
286  };
287 
292  template<class T>
294  {
295  public:
296 
297  ConcurrentList();
298  virtual ~ConcurrentList() noexcept;
299 
300  void Add(T value);
301  void Remove(T value);
302  void Clear();
303  size_t GetSize();
304  bool Exists(const T& value);
305 
306  template<class P>
307  bool FindIf(P predicate, T& value);
308 
309  template<class A>
310  void ForEach(A action);
311 
312  private:
313 
314  std::list<T> _list;
315  };
316 
317  /* Forward declaration */
318  class HandleStore;
319 
324  class Handle
325  {
326  public:
327 
328  virtual ~Handle() noexcept {}
329  virtual ConcurrentList<Handle*>& GetChildren() = 0;
330  virtual void DetachFromHolders() = 0;
331  virtual void DetachFromParent() = 0;
332  virtual HandleStore* GetStore() = 0;
333  };
334 
340  {
341  public:
342 
344 
345  template <class T>
346  T Get(AnyPointer ptr);
347 
348  template <class T>
349  void Set(AnyPointer ptr, T handle);
350 
351  static HandleStore& GetStoreForHandle(Handle*);
352 
353  template <class T>
354  static HandleStore& CreateStore();
355 
356  private:
357 
358  static HandleStore& GetDefaultStore();
359 
361  };
362 
367  template<class T>
369  {
370  public:
371 
372  bool IsNull() const;
373 
374  operator bool();
375  operator bool() const;
376 
377  operator T();
378  operator T() const;
379 
380  protected:
381 
382  class SmartHandle;
383 
384  HandleHolder(const HandleHolder& other);
385  HandleHolder();
386  ~HandleHolder() noexcept;
387 
388  HandleHolder& operator= (const HandleHolder& other) noexcept;
389 
390  typedef void(*SmartHandleFreeNotifyFunc)(SmartHandle* smartHandle);
391 
392  Handle* GetHandle() const;
393 
394  void AcquireAllocated(T handle, Handle* parent);
395  void AcquireTransient(T handle, Handle* parent);
396  void AcquireAllocatedWithNotification(T handle, Handle* parent, SmartHandleFreeNotifyFunc freeNotifyFunc);
397  void Acquire(HandleHolder& other);
398 
399  void Acquire(T handle, bool allocated, SmartHandleFreeNotifyFunc freeNotifyFunc, Handle* parent);
400  void Release();
401 
402  class SmartHandle : public Handle
403  {
404  public:
405 
406  SmartHandle(HandleHolder* holder, T handle, bool allocated, SmartHandleFreeNotifyFunc freeNotifyFunc, Handle* parent);
407  virtual ~SmartHandle() noexcept;
408 
409  void Acquire(HandleHolder* holder);
410  void Release(HandleHolder* holder);
411 
412  void Destroy();
413 
414  T GetHandle() const;
415 
416  Handle* GetParent() const;
417 
418  AnyPointer GetExtraInfos() const;
419  void SetExtraInfos(AnyPointer extraInfo);
420 
421  ConcurrentList<Handle*>& GetChildren() override;
422  void DetachFromHolders() override;
423  void DetachFromParent() override;
424  HandleStore* GetStore() override;
425 
426  private:
427 
428  static void DeleteHandle(Handle* handle);
429  static void ResetHolder(HandleHolder* holder);
430  static SynchronizationMode GetSynchronizationMode();
431 
433  ConcurrentList<Handle*> _children;
434 
435  SynchronizationGuard _guard;
436 
437  T _handle;
438  bool _allocated;
439  SmartHandleFreeNotifyFunc _freeNotifyFunc;
440  Handle* _parent;
441  AnyPointer _extraInfo;
442  HandleStore* _store;
443  };
444 
445  SmartHandle* _smartHandle;
446  };
447 
454  {
455  public:
456 
457  virtual ~Streamable() noexcept {}
458 
459  operator ostring() const
460  {
461  return ToString();
462  }
463 
464  virtual ostring ToString() const = 0;
465 
466  template<class T>
467  friend T& operator << (T& lhs, const Streamable& rhs)
468  {
469  lhs << static_cast<ostring>(rhs);
470  return lhs;
471  }
472  };
473  }
474 }
Internal usage. List supporting concurrent access from multiple threads.
Definition: core.hpp:294
Internal usage. Map supporting concurrent access from multiple threads.
Definition: core.hpp:270
Template Enumeration template class providing some type safety to some extends for manipulating enume...
Definition: core.hpp:118
Template Flags template class providing some type safety to some extends for manipulating flags set v...
Definition: core.hpp:148
Internal usage. Smart pointer class with reference counting for managing OCILIB object handles.
Definition: core.hpp:369
Internal usage. Interface for handling ownership and relationship of a C API handle.
Definition: core.hpp:325
Internal usage. Provide a store for C Handles to C++ Handles mapping.
Definition: core.hpp:340
Internal usage. Provide a buffer class with RAII capabilities.
Definition: core.hpp:197
Abstract class allowing derived classes to be compatible with any type supporting the operator << oci...
Definition: core.hpp:454
Internal usage. Base class for types that can be locked.
Definition: core.hpp:248
Internal usage. SynchronizationGuard object.
Definition: core.hpp:227
static T Check(T result)
Internal usage. Checks if the last OCILIB function call has raised an error. If so,...
Definition: Utils.hpp:53
ostring MakeString(const otext *result, int size=-1)
Internal usage. Constructs a C++ string object from the given OCILIB string pointer.
Definition: Utils.hpp:65
Raw MakeRaw(AnyPointer result, unsigned int size)
Internal usage. Constructs a C++ Raw object from the given OCILIB raw buffer.
Definition: Utils.hpp:70
SynchronizationMode
Internal usage. Synchronization mode enumeration.
Definition: core.hpp:217
OCILIB ++ Namespace.
std::basic_string< otext, std::char_traits< otext >, std::allocator< otext > > ostring
string class wrapping the OCILIB otext * type and OTEXT() macros ( see Character sets )
Definition: config.hpp:120
std::vector< unsigned char > Raw
C++ counterpart of SQL RAW data type.
Definition: config.hpp:138
OCI_Mutex * MutexHandle
Alias for an OCI_Mutex pointer.
Definition: config.hpp:147
void * AnyPointer
Alias for the generic void pointer.
Definition: config.hpp:129
Internal usage. Determine if the given type is a supported numeric type.
Definition: core.hpp:100