OCILIB (C and C++ Driver for Oracle)  4.7.3
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-2021 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  ManagedBuffer(T* buffer, size_t size);
202 
203  ~ManagedBuffer() noexcept;
204 
205  operator T* ();
206 
207  private:
208 
209  T* _buffer;
210  size_t _size;
211  };
212 
217  class Locker
218  {
219  public:
220 
221  Locker();
222  virtual ~Locker() noexcept;
223 
224  void Lock() const;
225  void Unlock() const;
226 
227  void SetAccessMode(bool threaded);
228 
229  private:
230 
231  MutexHandle _mutex;
232  };
233 
238  class Lockable
239  {
240  public:
241 
242  Lockable();
243  virtual ~Lockable() noexcept;
244 
245  void SetLocker(Locker* locker);
246 
247  void Lock() const;
248  void Unlock() const;
249 
250  private:
251 
252  Locker* _locker;
253  };
254 
259  template<class K, class V>
260  class ConcurrentMap : public Lockable
261  {
262  public:
263 
264  ConcurrentMap();
265  virtual ~ConcurrentMap() noexcept;
266 
267  void Remove(K key);
268  V Get(K key);
269  void Set(K key, V value);
270  void Clear();
271  size_t GetSize();
272 
273  private:
274 
275  std::map<K, V> _map;
276 
277  };
278 
283  template<class T>
284  class ConcurrentList : public Lockable
285  {
286  public:
287 
288  ConcurrentList();
289  virtual ~ConcurrentList() noexcept;
290 
291  void Add(T value);
292  void Remove(T value);
293  void Clear();
294  size_t GetSize();
295  bool Exists(const T& value);
296 
297  template<class P>
298  bool FindIf(P predicate, T& value);
299 
300  template<class A>
301  void ForEach(A action);
302 
303  private:
304 
305  std::list<T> _list;
306  };
307 
312  class Handle
313  {
314  public:
315 
316  virtual ~Handle() noexcept {}
317  virtual ConcurrentList<Handle*>& GetChildren() = 0;
318  virtual void DetachFromHolders() = 0;
319  virtual void DetachFromParent() = 0;
320  };
321 
326  template<class T>
328  {
329  public:
330 
331  bool IsNull() const;
332 
333  operator bool();
334  operator bool() const;
335 
336  operator T();
337  operator T() const;
338 
339  protected:
340 
341  class SmartHandle;
342 
343  HandleHolder(const HandleHolder& other);
344  HandleHolder();
345  ~HandleHolder() noexcept;
346 
347  HandleHolder& operator= (const HandleHolder& other) noexcept;
348 
349  typedef boolean(OCI_API* HandleFreeFunc)(AnyPointer handle);
350 
351  typedef void(*SmartHandleFreeNotifyFunc)(SmartHandle* smartHandle);
352 
353  Handle* GetHandle() const;
354 
355  void Acquire(T handle, HandleFreeFunc handleFreefunc, SmartHandleFreeNotifyFunc freeNotifyFunc, Handle* parent);
356  void Acquire(HandleHolder& other);
357  void Release();
358 
359  class SmartHandle : public Handle
360  {
361  public:
362 
363  SmartHandle(HandleHolder* holder, T handle, HandleFreeFunc handleFreefunc, SmartHandleFreeNotifyFunc freeNotifyFunc, Handle* parent);
364  virtual ~SmartHandle() noexcept;
365 
366  void Acquire(HandleHolder* holder);
367  void Release(HandleHolder* holder);
368 
369  void Destroy();
370 
371  T GetHandle() const;
372 
373  Handle* GetParent() const;
374 
375  AnyPointer GetExtraInfos() const;
376  void SetExtraInfos(AnyPointer extraInfo);
377 
378  ConcurrentList<Handle*>& GetChildren() override;
379  void DetachFromHolders() override;
380  void DetachFromParent() override;
381 
382  private:
383 
384  static void DeleteHandle(Handle* handle);
385  static void ResetHolder(HandleHolder* holder);
386 
388  ConcurrentList<Handle*> _children;
389 
390  Locker _locker;
391 
392  T _handle;
393  HandleFreeFunc _handleFreeFunc;
394  SmartHandleFreeNotifyFunc _freeNotifyFunc;
395  Handle* _parent;
396  AnyPointer _extraInfo;
397  };
398 
399  SmartHandle* _smartHandle;
400  };
401 
408  {
409  public:
410 
411  virtual ~Streamable() noexcept {}
412 
413  operator ostring() const
414  {
415  return ToString();
416  }
417 
418  virtual ostring ToString() const = 0;
419 
420  template<class T>
421  friend T& operator << (T& lhs, const Streamable& rhs)
422  {
423  lhs << static_cast<ostring>(rhs);
424  return lhs;
425  }
426  };
427 
428  }
429 }
Internal usage. Interface for handling ownership and relationship of a C API handle.
Definition: core.hpp:312
OCILIB ++ Namespace.
Raw MakeRaw(AnyPointer result, unsigned int size)
Internal usage. Constructs a C++ Raw object from the given OCILIB raw buffer.
Definition: Utils.hpp:70
OCI_Mutex * MutexHandle
Alias for an OCI_Mutex pointer.
Definition: config.hpp:147
Abstract class allowing derived classes to be compatible with any type supporting the operator << oci...
Definition: core.hpp:407
Internal usage. Locker object.
Definition: core.hpp:217
static T Check(T result)
Internal usage. Checks if the last OCILIB function call has raised an error. If so, it raises a C++ exception using the retrieved error handle.
Definition: Utils.hpp:53
Template Flags template class providing some type safety to some extends for manipulating flags set v...
Definition: core.hpp:147
Internal usage. Map supporting concurrent access from multiple threads.
Definition: core.hpp:260
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
Internal usage. Determine if the given type is a supported numeric type.
Definition: core.hpp:99
Internal usage. Smart pointer class with reference counting for managing OCILIB object handles...
Definition: core.hpp:327
void * AnyPointer
Alias for the generic void pointer.
Definition: config.hpp:129
Template Enumeration template class providing some type safety to some extends for manipulating enume...
Definition: core.hpp:117
Internal usage. Provide a buffer class with RAII capabilities.
Definition: core.hpp:196
std::vector< unsigned char > Raw
C++ counterpart of SQL RAW data type.
Definition: config.hpp:138
Internal usage. List supporting concurrent access from multiple threads.
Definition: core.hpp:284
Internal usage. Base class for types that can be locked.
Definition: core.hpp:238
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