OCILIB (C and C++ Driver for Oracle)  4.7.3
Open source and cross platform Oracle Driver delivering efficient access to Oracle databases.
SmartHandle.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 "ocilibcpp/core.hpp"
24 
25 namespace ocilib
26 {
27  namespace core
28  {
29 
30  template<class T>
31  HandleHolder<T>::SmartHandle::SmartHandle
32  (
33  HandleHolder* holder, T handle, HandleFreeFunc handleFreefunc,
34  SmartHandleFreeNotifyFunc freeNotifyFunc, Handle* parent
35  )
36  : _holders(), _handle(handle), _handleFreeFunc(handleFreefunc),
37  _freeNotifyFunc(freeNotifyFunc), _parent(parent), _extraInfo(nullptr)
38  {
40 
41  _holders.SetLocker(&_locker);
42  _children.SetLocker(&_locker);
43 
44  Environment::SetSmartHandle<SmartHandle*>(handle, this);
45 
46  Acquire(holder);
47 
48  if (_parent && _handle)
49  {
50  _parent->GetChildren().Add(this);
51  }
52  }
53 
54  template<class T>
55  HandleHolder<T>::SmartHandle::~SmartHandle() noexcept
56  {
57  SILENT_CATCH((Destroy()))
58  }
59 
60  template<class T>
61  void HandleHolder<T>::SmartHandle::Destroy()
62  {
63  if (_parent && _handle)
64  {
65  _parent->GetChildren().Remove(this);
66  }
67 
68  _children.ForEach(DeleteHandle);
69  _children.Clear();
70 
71  _holders.SetLocker(nullptr);
72  _children.SetLocker(nullptr);
73 
74  Environment::SetSmartHandle<SmartHandle*>(_handle, nullptr);
75 
76  if (_freeNotifyFunc)
77  {
78  _freeNotifyFunc(this);
79  }
80 
81  if (_handleFreeFunc && _handle)
82  {
83  _handleFreeFunc(_handle);
84  }
85  }
86 
87  template<class T>
88  void HandleHolder<T>::SmartHandle::DeleteHandle(Handle* handle)
89  {
90  if (handle)
91  {
92  handle->DetachFromParent();
93  handle->DetachFromHolders();
94 
95  delete core::OnDeallocate(handle);
96  }
97  }
98 
99  template<class T>
100  void HandleHolder<T>::SmartHandle::ResetHolder(HandleHolder* holder)
101  {
102  if (holder)
103  {
104  holder->_smartHandle = nullptr;
105  }
106  }
107 
108  template<class T>
109  void HandleHolder<T>::SmartHandle::Acquire(HandleHolder* holder)
110  {
111  _holders.Add(holder);
112  }
113 
114  template<class T>
115  void HandleHolder<T>::SmartHandle::Release(HandleHolder* holder)
116  {
117  _holders.Remove(holder);
118 
119  if (_holders.GetSize() == 0)
120  {
121  delete core::OnDeallocate(this);
122  }
123 
124  holder->_smartHandle = nullptr;
125  }
126 
127  template<class T>
128  T HandleHolder<T>::SmartHandle::GetHandle() const
129  {
130  return _handle;
131  }
132 
133  template<class T>
134  Handle* HandleHolder<T>::SmartHandle::GetParent() const
135  {
136  return _parent;
137  }
138 
139  template<class T>
140  AnyPointer HandleHolder<T>::SmartHandle::GetExtraInfos() const
141  {
142  return _extraInfo;
143  }
144 
145  template<class T>
146  void HandleHolder<T>::SmartHandle::SetExtraInfos(AnyPointer extraInfo)
147  {
148  _extraInfo = extraInfo;
149  }
150 
151  template<class T>
152  ConcurrentList<Handle*>& HandleHolder<T>::SmartHandle::GetChildren()
153  {
154  return _children;
155  }
156 
157  template<class T>
158  void HandleHolder<T>::SmartHandle::DetachFromHolders()
159  {
160  _holders.ForEach(ResetHolder);
161  _holders.Clear();
162  }
163 
164  template<class T>
165  void HandleHolder<T>::SmartHandle::DetachFromParent()
166  {
167  _parent = nullptr;
168  }
169 
170  }
171 }
OCILIB ++ Namespace.
static Environment::EnvironmentFlags GetMode()
Return the Environment mode flags.
Definition: Environment.hpp:52
void * AnyPointer
Alias for the generic void pointer.
Definition: config.hpp:129