fb-cpp 0.0.2
A modern C++ wrapper for the Firebird database API
Loading...
Searching...
No Matches
SmartPtrs.h
1/*
2 * MIT License
3 *
4 * Copyright (c) 2025 Adriano dos Santos Fernandes
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24
25#ifndef FBCPP_SMART_PTRS_H
26#define FBCPP_SMART_PTRS_H
27
28#include "fb-api.h"
29#include <memory>
30
31
35namespace fbcpp
36{
37 namespace impl
38 {
39 struct FbDisposeDeleter final
40 {
41 void operator()(fb::IDisposable* obj) noexcept
42 {
43 if (obj)
44 obj->dispose();
45 }
46 };
47 } // namespace impl
48
52 template <typename T>
53 using FbUniquePtr = std::unique_ptr<T, impl::FbDisposeDeleter>;
54
58 template <typename T>
59 FbUniquePtr<T> fbUnique(T* obj) noexcept
60 {
61 return FbUniquePtr<T>{obj};
62 }
63
67 template <typename T>
68 class FbRef final
69 {
70 template <typename Y>
71 friend class FbRef;
72
73 public:
74 FbRef() noexcept
75 : ptr{nullptr}
76 {
77 }
78
79 FbRef(std::nullptr_t) noexcept
80 : ptr{nullptr}
81 {
82 }
83
84 explicit FbRef(T* p) noexcept
85 : ptr{p}
86 {
87 }
88
89 template <typename Y>
90 explicit FbRef(Y* p) noexcept
91 : ptr{p}
92 {
93 }
94
95 FbRef(FbRef& r) noexcept
96 : ptr{nullptr}
97 {
98 assign(r.ptr, true);
99 }
100
101 template <typename Y>
102 FbRef(FbRef<Y>& r) noexcept
103 : ptr{nullptr}
104 {
105 assign(r.ptr, true);
106 }
107
108 FbRef(FbRef&& r) noexcept
109 : ptr{r.ptr}
110 {
111 r.ptr = nullptr;
112 }
113
114 ~FbRef() noexcept
115 {
116 if (ptr)
117 ptr->release();
118 }
119
120 void reset(T* p = nullptr) noexcept
121 {
122 assign(p, false);
123 }
124
130 T* release() noexcept
131 {
132 T* tmp = ptr;
133 ptr = nullptr;
134 return tmp;
135 }
136
137 FbRef& operator=(FbRef& r) noexcept
138 {
139 assign(r.ptr, true);
140 return *this;
141 }
142
143 template <typename Y>
144 FbRef& operator=(FbRef<Y>& r) noexcept
145 {
146 assign(r.ptr, true);
147 return *this;
148 }
149
150 FbRef& operator=(FbRef&& r) noexcept
151 {
152 if (ptr != r.ptr)
153 {
154 if (ptr)
155 assign(nullptr, false);
156
157 ptr = r.ptr;
158 r.ptr = nullptr;
159 }
160
161 return *this;
162 }
163
164 T* operator->() noexcept
165 {
166 return ptr;
167 }
168
169 const T* operator->() const noexcept
170 {
171 return ptr;
172 }
173
174 explicit operator bool() const noexcept
175 {
176 return ptr != nullptr;
177 }
178
179 bool operator!() const noexcept
180 {
181 return ptr == nullptr;
182 }
183
184 bool operator==(const FbRef& other) const noexcept
185 {
186 return ptr == other.ptr;
187 }
188
189 bool operator==(const T* other) const noexcept
190 {
191 return ptr == other;
192 }
193
194 bool operator!=(const FbRef& other) const noexcept
195 {
196 return ptr != other.ptr;
197 }
198
199 bool operator!=(const T* other) const noexcept
200 {
201 return ptr != other;
202 }
203
204 T* get() noexcept
205 {
206 return ptr;
207 }
208
209 const T* get() const noexcept
210 {
211 return ptr;
212 }
213
214 private:
215 T* assign(T* const p, bool addRef) noexcept
216 {
217 if (ptr != p)
218 {
219 if (p && addRef)
220 p->addRef();
221
222 T* tmp = ptr;
223 ptr = p;
224
225 if (tmp)
226 tmp->release();
227 }
228
229 return p;
230 }
231
232 private:
233 T* ptr;
234 };
235
239 template <typename T>
240 FbRef<T> fbRef(T* arg) noexcept
241 {
242 return FbRef<T>{arg};
243 }
244} // namespace fbcpp
245
246
247#endif // FBCPP_SMART_PTRS_H
Reference-counted smart pointer for Firebird objects using addRef/release semantics.
Definition SmartPtrs.h:69
T * release() noexcept
Relinquishes ownership of the pointer without releasing it and returns it.
Definition SmartPtrs.h:130
fb-cpp namespace.
Definition Array.h:43
FbRef< T > fbRef(T *arg) noexcept
Creates a reference-counted smart pointer for a Firebird object.
Definition SmartPtrs.h:240
FbUniquePtr< T > fbUnique(T *obj) noexcept
Creates a unique pointer for a Firebird disposable object.
Definition SmartPtrs.h:59
std::unique_ptr< T, impl::FbDisposeDeleter > FbUniquePtr
Unique pointer type for Firebird disposable objects.
Definition SmartPtrs.h:53