OCILIB (C and C++ Driver for Oracle)  4.7.7
Open source and cross platform Oracle Driver delivering efficient access to Oracle databases.
Loading...
Searching...
No Matches
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-2025 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
35namespace 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:
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
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
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
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 private:
354
356 };
357
362 template<class T>
364 {
365 public:
366
367 bool IsNull() const;
368
369 operator bool();
370 operator bool() const;
371
372 operator T();
373 operator T() const;
374
375 protected:
376
377 class SmartHandle;
378
379 HandleHolder(const HandleHolder& other);
380 HandleHolder();
381 ~HandleHolder() noexcept;
382
383 HandleHolder& operator= (const HandleHolder& other) noexcept;
384
385 typedef void(*SmartHandleFreeNotifyFunc)(SmartHandle* smartHandle);
386
387 Handle* GetHandle() const;
388
389 void AcquireAllocated(T handle, Handle* parent);
390 void AcquireTransient(T handle, Handle* parent);
391 void AcquireAllocatedWithNotification(T handle, Handle* parent, SmartHandleFreeNotifyFunc freeNotifyFunc);
392 void Acquire(HandleHolder& other);
393
394 void Acquire(T handle, bool allocated, SmartHandleFreeNotifyFunc freeNotifyFunc, Handle* parent);
395 void Release();
396
397 class SmartHandle : public Handle
398 {
399 public:
400
401 SmartHandle(HandleHolder* holder, T handle, bool allocated, SmartHandleFreeNotifyFunc freeNotifyFunc, Handle* parent);
402 virtual ~SmartHandle() noexcept;
403
404 void Acquire(HandleHolder* holder);
405 void Release(HandleHolder* holder);
406
407 void Destroy();
408
409 T GetHandle() const;
410
411 Handle* GetParent() const;
412
413 AnyPointer GetExtraInfos() const;
414 void SetExtraInfos(AnyPointer extraInfo);
415
416 ConcurrentList<Handle*>& GetChildren() override;
417 void DetachFromHolders() override;
418 void DetachFromParent() override;
419 HandleStore* GetStore() override;
420
421 private:
422
423 static void DeleteHandle(Handle* handle);
424 static void ResetHolder(HandleHolder* holder);
425 static SynchronizationMode GetSynchronizationMode();
426
428 ConcurrentList<Handle*> _children;
429
431
432 T _handle;
433 bool _allocated;
434 SmartHandleFreeNotifyFunc _freeNotifyFunc;
435 Handle* _parent;
436 AnyPointer _extraInfo;
437 HandleStore* _store;
438 };
439
440 SmartHandle* _smartHandle;
441 };
442
449 {
450 public:
451
452 virtual ~Streamable() noexcept {}
453
454 operator ostring() const
455 {
456 return ToString();
457 }
458
459 virtual ostring ToString() const = 0;
460
461 template<class T>
462 friend T& operator << (T& lhs, const Streamable& rhs)
463 {
464 lhs << static_cast<ostring>(rhs);
465 return lhs;
466 }
467 };
468 }
469}
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:364
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:449
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