OCILIB (C and C++ Driver for Oracle)  4.9.0
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-2026 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>
118 {
119 typedef EnableIf<IsSame<T, unsigned char>::value ||
120 IsSame<T, char>::value ||
121 IsSame<T, float>::value ||
122 IsSame<T, double>::value> Type;
123 };
124
129 template<class T>
130 class Enum
131 {
132 public:
133
134 typedef T Type;
135
136 Enum();
137 Enum(T value);
138
139 T GetValue();
140
141 operator T ();
142 operator unsigned int() const;
143
144 bool operator == (const Enum& other) const;
145 bool operator != (const Enum& other) const;
146
147 bool operator == (const T& other) const;
148 bool operator != (const T& other) const;
149
150 private:
151
152 T _value;
153 };
154
159 template<class T>
160 class Flags
161 {
162 public:
163
164 typedef T Type;
165
166 Flags();
167 Flags(T flag);
168 Flags(const Flags& other);
169
170 Flags& operator = (const Flags& other) noexcept;
171
172 Flags operator~ () const;
173
174 Flags operator | (T other) const;
175 Flags operator & (T other) const;
176 Flags operator ^ (T other) const;
177
178 Flags operator | (const Flags& other) const;
179 Flags operator & (const Flags& other) const;
180 Flags operator ^ (const Flags& other) const;
181
182 Flags& operator |= (T other);
183 Flags& operator &= (T other);
184 Flags& operator ^= (T other);
185
186 Flags& operator |= (const Flags& other);
187 Flags& operator &= (const Flags& other);
188 Flags& operator ^= (const Flags& other);
189
190 bool operator == (T other) const;
191 bool operator == (const Flags& other) const;
192
193 unsigned int GetValues() const;
194
195 bool IsSet(T other) const;
196
197 private:
198
199 Flags(unsigned int flags);
200
201 unsigned int _flags;
202 };
203
208 template< typename T>
210 {
211 public:
213 ManagedBuffer(size_t size);
214
215 ~ManagedBuffer() noexcept;
216
217 operator T* ();
218
219 private:
220
221 T* _buffer;
222 size_t _size;
223 };
224
230 {
231 Unsafe,
232 Safe
233 };
234
240 {
241 public:
242
244 virtual ~SynchronizationGuard() noexcept;
245
246 void Acquire() const;
247 void Release() const;
248
249 void SetMode(SynchronizationMode mode);
250
251 private:
252
253 MutexHandle _mutex;
254 };
255
261 {
262 public:
263
265 virtual ~Synchronizable() noexcept;
266
267 void SetGuard(SynchronizationGuard* guard);
268
269 void Acquire() const;
270 void Release() const;
271
272 private:
273
274 SynchronizationGuard* _guard;
275 };
276
281 template<class K, class V>
283 {
284 public:
285
287 virtual ~ConcurrentMap() noexcept;
288
289 void Remove(K key);
290 V Get(K key);
291 void Set(K key, V value);
292 void Clear();
293 size_t GetSize();
294
295 private:
296
297 std::map<K, V> _map;
298
299 };
300
305 template<class T>
307 {
308 public:
309
311 virtual ~ConcurrentList() noexcept;
312
313 void Add(T value);
314 void Remove(T value);
315 void Clear();
316 size_t GetSize();
317 bool Exists(const T& value);
318
319 template<class P>
320 bool FindIf(P predicate, T& value);
321
322 template<class A>
323 void ForEach(A action);
324
325 private:
326
327 std::list<T> _list;
328 };
329
330 /* Forward declaration */
331 class HandleStore;
332
337 class Handle
338 {
339 public:
340
341 virtual ~Handle() noexcept {}
342 virtual ConcurrentList<Handle*>& GetChildren() = 0;
343 virtual void DetachFromHolders() = 0;
344 virtual void DetachFromParent() = 0;
345 virtual HandleStore* GetStore() const = 0;
346 virtual Handle* GetParent() const = 0;
347 };
348
354 {
355 public:
356
358
359 template <class T>
360 T Get(AnyPointer ptr);
361
362 template <class T>
363 void Set(AnyPointer ptr, T handle);
364
365 static HandleStore& GetStoreForHandle(Handle*);
366
367 private:
368
370 };
371
376 template<class T>
378 {
379 public:
380
381 bool IsNull() const;
382
383 operator bool();
384 operator bool() const;
385
386 operator T();
387 operator T() const;
388
389 protected:
390
391 class SmartHandle;
392
393 HandleHolder(const HandleHolder& other);
394 HandleHolder();
395 ~HandleHolder() noexcept;
396
397 HandleHolder& operator= (const HandleHolder& other) noexcept;
398
399 typedef void(*SmartHandleFreeNotifyFunc)(SmartHandle* smartHandle);
400
401 Handle* GetHandle() const;
402
403 void AcquireAllocated(T handle, Handle* parent);
404 void AcquireTransient(T handle, Handle* parent);
405 void AcquireAllocatedWithNotification(T handle, Handle* parent, SmartHandleFreeNotifyFunc freeNotifyFunc);
406 void Acquire(HandleHolder& other);
407
408 void Acquire(T handle, bool allocated, SmartHandleFreeNotifyFunc freeNotifyFunc, Handle* parent);
409 void Release();
410
411 class SmartHandle : public Handle
412 {
413 public:
414
415 SmartHandle(HandleHolder* holder, T handle, bool allocated, SmartHandleFreeNotifyFunc freeNotifyFunc, Handle* parent);
416 virtual ~SmartHandle() noexcept;
417
418 void Acquire(HandleHolder* holder);
419 void Release(HandleHolder* holder);
420
421 void Destroy();
422
423 T GetHandle() const;
424
425 AnyPointer GetExtraInfos() const;
426 void SetExtraInfos(AnyPointer extraInfo);
427
428 ConcurrentList<Handle*>& GetChildren() override;
429 void DetachFromHolders() override;
430 void DetachFromParent() override;
431 HandleStore* GetStore() const override;
432 Handle* GetParent() const override;
433
434 private:
435
436 static void DeleteHandle(Handle* handle);
437 static void ResetHolder(HandleHolder* holder);
438 static SynchronizationMode GetSynchronizationMode();
439
441 ConcurrentList<Handle*> _children;
442
444
445 T _handle;
446 bool _allocated;
447 SmartHandleFreeNotifyFunc _freeNotifyFunc;
448 Handle* _parent;
449 AnyPointer _extraInfo;
450 HandleStore* _store;
451 };
452
453 SmartHandle* _smartHandle;
454 };
455
462 {
463 public:
464
465 virtual ~Streamable() noexcept {}
466
467 operator ostring() const
468 {
469 return ToString();
470 }
471
472 virtual ostring ToString() const = 0;
473
474 template<class T>
475 friend T& operator << (T& lhs, const Streamable& rhs)
476 {
477 lhs << static_cast<ostring>(rhs);
478 return lhs;
479 }
480 };
481 }
482}
Internal usage. List supporting concurrent access from multiple threads.
Definition: core.hpp:307
Internal usage. Map supporting concurrent access from multiple threads.
Definition: core.hpp:283
Template Enumeration template class providing some type safety to some extends for manipulating enume...
Definition: core.hpp:131
Template Flags template class providing some type safety to some extends for manipulating flags set v...
Definition: core.hpp:161
Internal usage. Smart pointer class with reference counting for managing OCILIB object handles.
Definition: core.hpp:378
Internal usage. Interface for handling ownership and relationship of a C API handle.
Definition: core.hpp:338
Internal usage. Provide a store for C Handles to C++ Handles mapping.
Definition: core.hpp:354
Internal usage. Provide a buffer class with RAII capabilities.
Definition: core.hpp:210
Abstract class allowing derived classes to be compatible with any type supporting the operator << oci...
Definition: core.hpp:462
Internal usage. Base class for types that can be locked.
Definition: core.hpp:261
Internal usage. SynchronizationGuard object.
Definition: core.hpp:240
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:230
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
Internal usage. Determine if the given type is a supported numeric type.
Definition: core.hpp:118