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
64 // FIXME: Review every usage to see if is not leaking one reference count.
68 template <typename T>
69 class FbRef final
70 {
71 template <typename Y>
72 friend class FbRef;
73
74 public:
75 FbRef() noexcept
76 : ptr{nullptr}
77 {
78 }
79
80 FbRef(std::nullptr_t) noexcept
81 : ptr{nullptr}
82 {
83 }
84
85 explicit FbRef(T* p) noexcept
86 : ptr{p}
87 {
88 }
89
90 template <typename Y>
91 explicit FbRef(Y* p) noexcept
92 : ptr{p}
93 {
94 }
95
96 FbRef(FbRef& r) noexcept
97 : ptr{nullptr}
98 {
99 assign(r.ptr, true);
100 }
101
102 template <typename Y>
103 FbRef(FbRef<Y>& r) noexcept
104 : ptr{nullptr}
105 {
106 assign(r.ptr, true);
107 }
108
109 FbRef(FbRef&& r) noexcept
110 : ptr{r.ptr}
111 {
112 r.ptr = nullptr;
113 }
114
115 ~FbRef() noexcept
116 {
117 if (ptr)
118 ptr->release();
119 }
120
121 void reset(T* p = nullptr) noexcept
122 {
123 assign(p, false);
124 }
125
126 FbRef& operator=(FbRef& r) noexcept
127 {
128 assign(r.ptr, true);
129 return *this;
130 }
131
132 template <typename Y>
133 FbRef& operator=(FbRef<Y>& r) noexcept
134 {
135 assign(r.ptr, true);
136 return *this;
137 }
138
139 FbRef& operator=(FbRef&& r) noexcept
140 {
141 if (ptr != r.ptr)
142 {
143 if (ptr)
144 assign(nullptr, false);
145
146 ptr = r.ptr;
147 r.ptr = nullptr;
148 }
149
150 return *this;
151 }
152
153 T* operator->() noexcept
154 {
155 return ptr;
156 }
157
158 const T* operator->() const noexcept
159 {
160 return ptr;
161 }
162
163 explicit operator bool() const noexcept
164 {
165 return ptr != nullptr;
166 }
167
168 bool operator!() const noexcept
169 {
170 return ptr == nullptr;
171 }
172
173 bool operator==(const FbRef& other) const noexcept
174 {
175 return ptr == other.ptr;
176 }
177
178 bool operator==(const T* other) const noexcept
179 {
180 return ptr == other;
181 }
182
183 bool operator!=(const FbRef& other) const noexcept
184 {
185 return ptr != other.ptr;
186 }
187
188 bool operator!=(const T* other) const noexcept
189 {
190 return ptr != other;
191 }
192
193 T* get() noexcept
194 {
195 return ptr;
196 }
197
198 const T* get() const noexcept
199 {
200 return ptr;
201 }
202
203 private:
204 T* assign(T* const p, bool addRef) noexcept
205 {
206 if (ptr != p)
207 {
208 if (p && addRef)
209 p->addRef();
210
211 T* tmp = ptr;
212 ptr = p;
213
214 if (tmp)
215 tmp->release();
216 }
217
218 return p;
219 }
220
221 private:
222 T* ptr;
223 };
224
228 template <typename T>
229 FbRef<T> fbRef(T* arg) noexcept
230 {
231 return FbRef<T>{arg};
232 }
233} // namespace fbcpp
234
235
236#endif // FBCPP_SMART_PTRS_H
Reference-counted smart pointer for Firebird objects using addRef/release semantics.
Definition SmartPtrs.h:70
fb-cpp namespace.
Definition Attachment.h:42
FbRef< T > fbRef(T *arg) noexcept
Creates a reference-counted smart pointer for a Firebird object.
Definition SmartPtrs.h:229
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