fb-cpp 0.0.1
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 obj->dispose();
44 }
45 };
46 } // namespace impl
47
48 template <typename T>
49 using FbUniquePtr = std::unique_ptr<T, impl::FbDisposeDeleter>;
50
51 template <typename T>
52 FbUniquePtr<T> fbUnique(T* obj) noexcept
53 {
54 return FbUniquePtr<T>{obj};
55 }
56
57 // FIXME: Review every usage to see if is not leaking one reference count.
58 template <typename T>
59 class FbRef final
60 {
61 template <typename Y>
62 friend class FbRef;
63
64 public:
65 FbRef() noexcept
66 : ptr{nullptr}
67 {
68 }
69
70 FbRef(std::nullptr_t) noexcept
71 : ptr{nullptr}
72 {
73 }
74
75 explicit FbRef(T* p) noexcept
76 : ptr{p}
77 {
78 }
79
80 template <typename Y>
81 explicit FbRef(Y* p) noexcept
82 : ptr{p}
83 {
84 }
85
86 FbRef(FbRef& r) noexcept
87 : ptr{nullptr}
88 {
89 assign(r.ptr, true);
90 }
91
92 template <typename Y>
93 FbRef(FbRef<Y>& r) noexcept
94 : ptr{nullptr}
95 {
96 assign(r.ptr, true);
97 }
98
99 FbRef(FbRef&& r) noexcept
100 : ptr{r.ptr}
101 {
102 r.ptr = nullptr;
103 }
104
105 ~FbRef() noexcept
106 {
107 if (ptr)
108 ptr->release();
109 }
110
111 void reset(T* p = nullptr) noexcept
112 {
113 assign(p, false);
114 }
115
116 FbRef& operator=(FbRef& r) noexcept
117 {
118 assign(r.ptr, true);
119 return *this;
120 }
121
122 template <typename Y>
123 FbRef& operator=(FbRef<Y>& r) noexcept
124 {
125 assign(r.ptr, true);
126 return *this;
127 }
128
129 FbRef& operator=(FbRef&& r) noexcept
130 {
131 if (ptr != r.ptr)
132 {
133 if (ptr)
134 assign(nullptr, false);
135
136 ptr = r.ptr;
137 r.ptr = nullptr;
138 }
139
140 return *this;
141 }
142
143 T* operator->() noexcept
144 {
145 return ptr;
146 }
147
148 const T* operator->() const noexcept
149 {
150 return ptr;
151 }
152
153 explicit operator bool() const noexcept
154 {
155 return ptr != nullptr;
156 }
157
158 bool operator!() const noexcept
159 {
160 return ptr == nullptr;
161 }
162
163 bool operator==(const FbRef& other) const noexcept
164 {
165 return ptr == other.ptr;
166 }
167
168 bool operator==(const T* other) const noexcept
169 {
170 return ptr == other;
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 T* get() noexcept
184 {
185 return ptr;
186 }
187
188 const T* get() const noexcept
189 {
190 return ptr;
191 }
192
193 private:
194 T* assign(T* const p, bool addRef) noexcept
195 {
196 if (ptr != p)
197 {
198 if (p && addRef)
199 p->addRef();
200
201 T* tmp = ptr;
202 ptr = p;
203
204 if (tmp)
205 tmp->release();
206 }
207
208 return p;
209 }
210
211 private:
212 T* ptr;
213 };
214
215 template <typename T>
216 FbRef<T> fbRef(T* arg) noexcept
217 {
218 return FbRef<T>{arg};
219 }
220} // namespace fbcpp
221
222
223#endif // FBCPP_SMART_PTRS_H
fb-cpp namespace.
Definition Attachment.h:42