GCC Code Coverage Report


Directory: src/gate/
File: src/gate/objects.hpp
Date: 2026-03-20 22:56:14
Exec Total Coverage
Lines: 54 59 91.5%
Functions: 69 103 67.0%
Branches: 5 8 62.5%

Line Branch Exec Source
1 /* GATE PROJECT LICENSE:
2 +----------------------------------------------------------------------------+
3 | Copyright (c) 2018-2026, Stefan Meislinger <sm@opengate.at> |
4 | All rights reserved. |
5 | |
6 | Redistribution and use in source and binary forms, with or without |
7 | modification, are permitted provided that the following conditions are met:|
8 | |
9 | 1. Redistributions of source code must retain the above copyright notice, |
10 | this list of conditions and the following disclaimer. |
11 | 2. Redistributions in binary form must reproduce the above copyright |
12 | notice, this list of conditions and the following disclaimer in the |
13 | documentation and/or other materials provided with the distribution. |
14 | |
15 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"|
16 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
17 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
18 | ARE DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
19 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
20 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
21 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
22 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
23 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
24 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
25 | THE POSSIBILITY OF SUCH DAMAGE. |
26 +----------------------------------------------------------------------------+
27 */
28
29 /** @file
30 * @brief Object interface wrappers, utilities and implementations
31 * @ingroup gatecore_cpp
32 */
33
34
35 #ifndef GATE_OBJECTS_HPP_INCLUDED
36 #define GATE_OBJECTS_HPP_INCLUDED
37
38 #include "gate/gate_core_api.hpp"
39 #include "gate/objects.h"
40 #include "gate/gatetypes.hpp"
41 #include "gate/strings.hpp"
42 #include "gate/memalloc.hpp"
43 #include "gate/atomics.hpp"
44
45 namespace gate
46 {
47
48 /// @brief
49 class GATE_CORE_CPP_API Object
50 {
51 public:
52 Object(gate_object_ptr_t obj_ptr) noexcept; // takes ownership of an object pointer
53 Object(Object const& obj) noexcept;
54 Object& operator=(Object const& obj) noexcept;
55 ~Object() noexcept;
56
57 String getInterfaceName();
58 bool_t implementsInterface(char const* ifaceName);
59 gate_object_ptr_t c_impl() noexcept;
60
61 bool_t operator!() const noexcept;
62 bool_t empty() const noexcept;
63
64 protected:
65 void swap(Object& that) noexcept;
66
67 private:
68 gate_object_ptr_t object_ref_ptr;
69 };
70
71
72 /// @brief
73 /// @tparam T
74 template<class T> class GATE_CORE_CPP_TEMPLATE_API ObjectImpl : public Object
75 {
76 public:
77 typedef T impl_t;
78 typedef ObjectImpl<T> object_impl_t;
79
80 protected:
81 impl_t* impl_ptr;
82
83 protected:
84 5 void swap(object_impl_t& that) noexcept
85 {
86 5 Object::swap(that);
87 5 swapRefs(this->impl_ptr, that.impl_ptr);
88 5 }
89
90 public:
91 307 ObjectImpl(impl_t* impl) noexcept
92 307 : Object((gate_object_ptr_t)impl), impl_ptr(impl)
93 {
94 307 }
95 45 ObjectImpl(ObjectImpl const& src) noexcept
96 45 : Object(src), impl_ptr(src.impl_ptr)
97 {
98 45 }
99 35 ObjectImpl& operator=(ObjectImpl const& that) noexcept
100 {
101 35 Object::operator=(that);
102 35 this->impl_ptr = that.impl_ptr;
103 35 return *this;
104 }
105 343 ~ObjectImpl() noexcept
106 {
107 343 }
108 371 impl_t* c_impl() const noexcept
109 {
110 371 return this->impl_ptr;
111 }
112 };
113
114
115 /// @brief
116 class GATE_CORE_CPP_API IObject
117 {
118 public:
119 virtual ~IObject();
120
121 virtual void release() noexcept = 0;
122 virtual int retain() noexcept = 0;
123 virtual String getInterfaceName() noexcept = 0;
124 };
125
126
127 /// @brief
128 /// @tparam ObjectStruct
129 /// @tparam VtblStruct
130 template<class ObjectStruct, class VtblStruct>
131 class GATE_CORE_CPP_TEMPLATE_API IObjectBuilder : public IObject
132 {
133 public:
134 typedef ObjectStruct obj_struct_t;
135 typedef VtblStruct obj_vtbl_t;
136
137 typedef IObjectBuilder<obj_struct_t, obj_vtbl_t> ObjectBuilder;
138
139 24 virtual ~IObjectBuilder()
140 {
141 24 }
142
143 36 virtual void release() noexcept
144 {
145
2/2
✓ Branch 1 taken 9 times.
✓ Branch 2 taken 17 times.
36 if (--this->refCounter == 0)
146 {
147
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
12 delete this;
148 }
149 36 }
150 24 virtual int retain() noexcept
151 {
152 24 return ++this->refCounter;
153 }
154 virtual String getInterfaceName() noexcept
155 {
156 return this->interfaceName;
157 }
158
159 protected:
160 struct GATE_CORE_CPP_TEMPLATE_API cpp_object : public obj_struct_t
161 {
162 ObjectBuilder* parent;
163
164 23 cpp_object(obj_vtbl_t const* vtbl_ptr, ObjectBuilder* parent_ptr)
165 23 : parent(parent_ptr)
166 {
167 23 this->vtbl = vtbl_ptr;
168 23 }
169 };
170
171 51 static void object_release(void* obj_ptr) noexcept
172 {
173 51 cpp_object* obj = static_cast<cpp_object*>(obj_ptr);
174 51 obj->parent->release();
175 51 }
176 34 static int object_retain(void* obj_ptr) noexcept
177 {
178 34 cpp_object* obj = static_cast<cpp_object*>(obj_ptr);
179 34 return obj->parent->retain();
180 }
181 static char const* object_get_interface_name(void* obj_ptr) noexcept
182 {
183 cpp_object* obj = static_cast<cpp_object*>(obj_ptr);
184 return obj->parent->interfaceName.c_str();
185 }
186
187 40 template<class T> static T* getInterface(cpp_object* obj)
188 {
189 40 T* ret = NULL;
190
1/2
✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
40 if (obj)
191 {
192 #if defined(GATEXX_NO_RTTI)
193 // TODO
194 ret = static_cast<T*>(obj->parent);
195 #else
196
1/2
✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
40 ret = dynamic_cast<T*>(obj->parent);
197 #endif
198 }
199 40 return ret;
200 }
201
202
203 struct GATE_CORE_CPP_TEMPLATE_API VtblObject : public obj_vtbl_t
204 {
205 21 VtblObject()
206 {
207 21 GATE_INTERFACE_VTBL(gate_object)* this_ptr = reinterpret_cast<GATE_INTERFACE_VTBL(gate_object)*>(this);
208 21 this_ptr->release = &object_release;
209 21 this_ptr->retain = &object_retain;
210 21 this_ptr->get_interface_name = &object_get_interface_name;
211 21 }
212 };
213
214 private:
215 cpp_object c_interface;
216 AtomicInt refCounter;
217 String interfaceName;
218
219 protected:
220 23 IObjectBuilder(obj_vtbl_t const* vtbl_ptr, char const* name)
221 23 : c_interface(vtbl_ptr, NULL), refCounter(1), interfaceName(String::createStatic(name))
222 {
223 23 this->c_interface.parent = this;
224 23 }
225
226 public:
227 9 virtual ObjectStruct* c_impl()
228 {
229 9 return &this->c_interface;
230 }
231
232 };
233
234
235 /// @brief
236 class GATE_CORE_CPP_API Startable : public ObjectImpl<gate_startable_t>
237 {
238 public:
239 Startable(gate_startable_t* obj);
240 Startable(Startable const& src);
241 Startable& operator=(Startable const& src);
242
243 void start();
244 void stop();
245 enumint_t getStatus();
246 };
247
248
249 /// @brief
250 class GATE_CORE_CPP_API MemoryBlock : public ObjectImpl<gate_memoryblock_t>
251 {
252 public:
253 MemoryBlock(void const* sourceBlockBytes, size_t blockLength);
254 MemoryBlock(size_t blockLength = 0);
255
256 MemoryBlock(gate_memoryblock_t* obj);
257 MemoryBlock(MemoryBlock const& src);
258 MemoryBlock& operator=(MemoryBlock const& src);
259
260 size_t getSize() const;
261 void* getContent();
262 void const* getContent() const;
263
264 intptr_t compare(MemoryBlock const& block) const;
265 intptr_t compare(void const* block, size_t blockLength) const;
266
267 bool equals(MemoryBlock const& block) const;
268 bool equals(void const* block, size_t blockLength) const;
269
270 };
271
272
273 /// @brief
274 /// @tparam T
275 template<class T> class TypedMemoryBlock : public MemoryBlock
276 {
277 public:
278 typedef TypedMemoryBlock<T> self_t;
279
280 TypedMemoryBlock()
281 {
282 }
283
284 TypedMemoryBlock(self_t const& src)
285 : MemoryBlock(src)
286 {
287 }
288
289 self_t& operator=(self_t const& src)
290 {
291 MemoryBlock::operator=(src);
292 return *this;
293 }
294
295 T* getContent()
296 {
297 return static_cast<T*>(MemoryBlock::getContent());
298 }
299
300 T const* getContent() const
301 {
302 return static_cast<T const*>(MemoryBlock::getContent());
303 }
304
305 bool equals(MemoryBlock const& block) const;
306 bool equals(void const* block, size_t blockLength) const;
307
308
309 T* operator->()
310 {
311 return this->getContent();
312 }
313 T const* operator->() const
314 {
315 return this->getContent();
316 }
317
318 T& operator*()
319 {
320 return *this->getContent();
321 }
322 T const& operator*() const
323 {
324 return *this->getContent();
325 }
326 };
327
328 } // end of namespace gate
329
330 #endif
331