Debian Codegraph @

gi/boxed.hpp

raw symbols SCIP
1 #ifndef GI_BOXED_HPP
2 #define GI_BOXED_HPP
3
4 #include "gi_inc.hpp"
5
6 GI_MODULE_EXPORT
7 namespace gi
8 {
9 namespace repository
10 {
11 // specialize to enable copyable boxed wrapper
12 // ideally only used in case where the copy is (equivalently);
13 // * cheap
14 // * does not change the underlying pointer
15 // * refcount based
16 // * has no semantics effects
17 // (e.g. GstMiniObject refcount affects writable)
18 template<typename CType>
19 struct enable_boxed_copy :
20 #if GI_ENABLE_BOXED_COPY_ALL
21 public std::true_type
22 #else
23 public std::false_type
24 #endif
25 {};
26
27 } // namespace repository
28
29 namespace detail
30 {
31 // class tags
32 class Boxed
33 {};
34 class CBoxed : public Boxed
35 {};
36 class GBoxed : public Boxed
37 {};
38
39 template<typename CType>
40 class SharedWrapper
41 {
42 public:
43 typedef SharedWrapper self;
44
45 protected:
46 CType *data_ = nullptr;
47
48 public:
49 CType *gobj_() { return data_; }
50 const CType *gobj_() const { return data_; }
51
52 explicit operator bool() const { return (bool)data_; }
53
54 bool operator==(const SharedWrapper &other) const
55 {
56 return data_ == other.data_;
57 }
58
59 bool operator==(std::nullptr_t o) const { return data_ == o; }
60
61 bool operator!=(const SharedWrapper &other) const
62 {
63 return data_ != other.data_;
64 }
65
66 bool operator!=(std::nullptr_t o) const { return data_ != o; }
67
68 CType *release_()
69 {
70 auto tmp = this->data_;
71 this->data_ = nullptr;
72 return tmp;
73 }
74 };
75
76 template<typename CppType, typename CType>
77 struct GBoxedFuncs
78 {
79 static CType *copy(const void *data)
80 {
81 return (CType *)g_boxed_copy(CppType::get_type_(), data);
82 }
83 static void free(void *data) { g_boxed_free(CppType::get_type_(), data); }
84 };
85
86 struct CBoxedFuncsBase
87 {
88 static void free(void *data) { g_free(data); }
89 };
90
91 template<typename CppType, typename CType>
92 struct CBoxedFuncs : CBoxedFuncsBase
93 {
94 static CType *copy(const void *data)
95 {
96 #if GLIB_CHECK_VERSION(2, 68, 0)
97 return (CType *)g_memdup2(data, sizeof(CType));
98 #else
99 return (CType *)g_memdup(data, sizeof(CType));
100 #endif
101 }
102 };
103
104 template<typename CType, typename Funcs, typename TagType>
105 class BoxedWrapper : public SharedWrapper<CType>, public TagType
106 {
107 typedef BoxedWrapper self;
108
109 protected:
110 static void _deleter(CType *obj, ...)
111 {
112 if (obj)
113 Funcs::free(obj);
114 }
115
116 static CType *_copy(const CType *obj)
117 {
118 return obj ? Funcs::copy(obj) : nullptr;
119 }
120
121 public:
122 typedef CType BaseObjectType;
123
124 BoxedWrapper(CType *obj = nullptr) noexcept { this->data_ = obj; }
125
126 CType *gobj_copy_() const { return _copy(this->gobj_()); }
127
128 // resulting casted type determines ownership
129 template<typename Cpp>
130 static Cpp wrap(const typename Cpp::BaseObjectType *obj)
131 {
132 static_assert(sizeof(Cpp) == sizeof(self), "type wrap not supported");
133 static_assert(std::is_base_of<self, Cpp>::value, "type wrap not supported");
134 BoxedWrapper w(const_cast<typename Cpp::BaseObjectType *>(obj));
135 return std::move(*static_cast<Cpp *>(&w));
136 }
137 };
138
139 // in templates below, Base should be a subclass of BoxedWrapper
140 // so we re-use the members it provides, as well as the wrap template
141 // to avoid ambiguous reference to the latter
142 // (if BoxedWrapper were inherited from again)
143
144 // the nullptr_t constructor (indirectly) supports `= nullptr` (assignment)
145
146 template<typename CppType, typename CType>
147 using GBoxedWrapperBase =
148 BoxedWrapper<CType, GBoxedFuncs<CppType, CType>, GBoxed>;
149
150 // assuming Base has suitable members such as above BoxedWrapper
151 template<typename Base>
152 class MoveWrapper : public Base
153 {
154 typedef Base super;
155
156 public:
157 using super::super;
158
159 ~MoveWrapper() { this->_deleter(this->data_, static_cast<Base *>(this)); }
160
161 MoveWrapper(const MoveWrapper &) = delete;
162 MoveWrapper &operator=(const MoveWrapper &) = delete;
163
164 MoveWrapper(MoveWrapper &&o) noexcept { *this = std::move(o); }
165 MoveWrapper &operator=(MoveWrapper &&o) noexcept
166 {
167 if (this != &o) {
168 this->_deleter(this->data_, static_cast<Base *>(this));
169 (Base &)(*this) = std::move(o);
170 o.data_ = nullptr;
171 }
172 return *this;
173 }
174 };
175
176 template<typename Base>
177 class CopyWrapper : public MoveWrapper<Base>
178 {
179 typedef MoveWrapper<Base> super;
180
181 public:
182 using super::super;
183
184 CopyWrapper(const CopyWrapper &o) noexcept : super()
185 {
186 this->data_ = this->_copy(o.data_);
187 }
188 CopyWrapper &operator=(const CopyWrapper &o) noexcept
189 {
190 if (this != &o) {
191 this->_deleter(this->data_, static_cast<Base *>(this));
192 if (sizeof(Base) > sizeof(this->data_))
193 (Base &)(*this) = std::move(o);
194 this->data_ = this->_copy(o.data_);
195 }
196 return *this;
197 }
198
199 CopyWrapper(CopyWrapper &&o) noexcept = default;
200 CopyWrapper &operator=(CopyWrapper &&o) noexcept = default;
201
202 // also accept from corresponding Reference (also based on Base)
203 CopyWrapper(const Base &o) noexcept : super()
204 {
205 this->data_ = this->_copy(((CopyWrapper &)o).data_);
206 }
207 CopyWrapper(Base &&o) noexcept : super()
208 {
209 (super &)(*this) = std::move((super &)o);
210 }
211 };
212
213 template<typename CType, typename Base>
214 using SelectWrapper =
215 typename std::conditional<repository::enable_boxed_copy<CType>::value,
216 CopyWrapper<Base>, MoveWrapper<Base>>::type;
217
218 // basis for registered boxed types
219 template<typename CppType, typename CType,
220 typename Base = GBoxedWrapperBase<CppType, CType>, typename RefType = void>
221 class GBoxedWrapper : public SelectWrapper<CType, Base>
222 {
223 typedef SelectWrapper<CType, Base> super;
224
225 public:
226 typedef RefType ReferenceType;
227
228 using super::super;
229
230 GBoxedWrapper(std::nullptr_t = nullptr) {}
231
232 void allocate_()
233 {
234 if (this->data_)
235 return;
236 // make sure we match boxed allocation with boxed free
237 // still guessing here that all-0 makes for a decent init :-(
238 CType tmp;
239 memset(&tmp, 0, sizeof(tmp));
240 this->data_ = (CType *)g_boxed_copy(this->get_type_(), &tmp);
241 }
242
243 // essentially g_boxed_copy
244 CppType copy_() const
245 {
246 return super::template wrap<CppType>(this->gobj_copy_());
247 }
248 };
249
250 template<typename CppType, typename CType>
251 using CBoxedWrapperBase =
252 BoxedWrapper<CType, CBoxedFuncs<CppType, CType>, CBoxed>;
253
254 // basis for non-registered plain C boxed type
255 template<typename CppType, typename CType,
256 typename Base = CBoxedWrapperBase<CppType, CType>, typename RefType = void>
257 class CBoxedWrapper : public MoveWrapper<Base>
258 {
259 typedef Base super;
260
261 public:
262 typedef RefType ReferenceType;
263
264 CBoxedWrapper(std::nullptr_t = nullptr) {}
265
266 void allocate_()
267 {
268 if (this->data_)
269 return;
270 this->data_ = g_new0(CType, 1);
271 }
272
273 static CppType new_()
274 {
275 return super::template wrap<CppType>(g_new0(CType, 1));
276 }
277 };
278
279 // allocate helper;
280 // dispatch to method if available
281 template<typename T, typename Enable = void>
282 struct allocator : public std::false_type
283 {
284 static void allocate(T &) {}
285 };
286
287 template<typename T>
288 struct allocator<T, decltype(T().allocate_())> : public std::true_type
289 {
290 static void allocate(T &v) { v.allocate_(); }
291 };
292
293 template<typename T>
294 void
295 allocate(T &v)
296 {
297 allocator<T>::allocate(v);
298 }
299
300 // basis for ref-holding to registered box type
301 template<typename CppType, typename CType, typename Base>
302 class GBoxedRefWrapper : public Base
303 {
304 typedef Base super;
305
306 public:
307 typedef CppType BoxType;
308
309 GBoxedRefWrapper(std::nullptr_t = nullptr) {}
310
311 // construct from owning version, but not the other way around
312 // (which requires an explicit copy)
313 GBoxedRefWrapper(const CppType &other)
314 {
315 (super &)(*this) = super::template wrap<super>(other.gobj_());
316 }
317
318 // support way to convert to owning box
319 // (by means of copy as opposed to a simple ref)
320 CppType copy_() const
321 {
322 return super::template wrap<CppType>(this->gobj_copy_());
323 }
324 };
325
326 // basis for ref-holding to non-registered plain C boxed type
327 template<typename CppType, typename CType, typename Base>
328 class CBoxedRefWrapper : public Base
329 {
330 typedef Base super;
331
332 public:
333 typedef CppType BoxType;
334
335 CBoxedRefWrapper(std::nullptr_t = nullptr) {}
336
337 // construct from owning version, but not the other way around
338 // (which requires an explicit copy)
339 CBoxedRefWrapper(const CppType &other)
340 {
341 (super &)(*this) = super::template wrap<super>(other.gobj_());
342 }
343 };
344
345 } // namespace detail
346
347 } // namespace gi
348
349 #endif // GI_BOXED_HPP