GCC Code Coverage Report


Directory: src/gate/
File: src/gate/objects.hpp
Date: 2025-12-12 23:40:09
Exec Total Coverage
Lines: 54 59 91.5%
Functions: 60 98 61.2%
Branches: 5 8 62.5%

Line Branch Exec Source
1 /* GATE PROJECT LICENSE:
2 +----------------------------------------------------------------------------+
3 | Copyright(c) 2018-2025, 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_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 256 ObjectImpl(impl_t* impl) noexcept
92 256 : Object((gate_object_ptr_t)impl), impl_ptr(impl)
93 {
94 256 }
95 43 ObjectImpl(ObjectImpl const& src) noexcept
96 43 : Object(src), impl_ptr(src.impl_ptr)
97 {
98 43 }
99 33 ObjectImpl& operator=(ObjectImpl const& that) noexcept
100 {
101 33 Object::operator=(that);
102 33 this->impl_ptr = that.impl_ptr;
103 33 return *this;
104 }
105 292 ~ObjectImpl() noexcept
106 {
107 292 }
108 269 impl_t* c_impl() const noexcept
109 {
110 269 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_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_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
1/2
✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
40 ret = dynamic_cast<T*>(obj->parent);
193 }
194 40 return ret;
195 }
196
197
198 struct GATE_CORE_CPP_API VtblObject : public obj_vtbl_t
199 {
200 21 VtblObject()
201 {
202 21 GATE_INTERFACE_VTBL(gate_object)* this_ptr = reinterpret_cast<GATE_INTERFACE_VTBL(gate_object)*>(this);
203 21 this_ptr->release = &object_release;
204 21 this_ptr->retain = &object_retain;
205 21 this_ptr->get_interface_name = &object_get_interface_name;
206 21 }
207 };
208
209 private:
210 cpp_object c_interface;
211 AtomicInt refCounter;
212 String interfaceName;
213
214 protected:
215 23 IObjectBuilder(obj_vtbl_t const* vtbl_ptr, char const* name)
216 23 : c_interface(vtbl_ptr, NULL), refCounter(1), interfaceName(String::createStatic(name))
217 {
218 23 this->c_interface.parent = this;
219 23 }
220
221 public:
222 9 virtual ObjectStruct* c_impl()
223 {
224 9 return &this->c_interface;
225 }
226
227 };
228
229
230 /// @brief
231 class GATE_CORE_CPP_API Startable : public ObjectImpl<gate_startable_t>
232 {
233 public:
234 Startable(gate_startable_t* obj);
235 Startable(Startable const& src);
236 Startable& operator=(Startable const& src);
237
238 void start();
239 void stop();
240 enumint_t getStatus();
241 };
242
243
244 /// @brief
245 class GATE_CORE_CPP_API MemoryBlock : public ObjectImpl<gate_memoryblock_t>
246 {
247 public:
248 MemoryBlock(void const* sourceBlockBytes, size_t blockLength);
249 MemoryBlock(size_t blockLength = 0);
250
251 MemoryBlock(gate_memoryblock_t* obj);
252 MemoryBlock(MemoryBlock const& src);
253 MemoryBlock& operator=(MemoryBlock const& src);
254
255 size_t getSize() const;
256 void* getContent();
257 void const* getContent() const;
258
259 intptr_t compare(MemoryBlock const& block) const;
260 intptr_t compare(void const* block, size_t blockLength) const;
261
262 bool equals(MemoryBlock const& block) const;
263 bool equals(void const* block, size_t blockLength) const;
264
265 };
266
267
268 /// @brief
269 /// @tparam T
270 template<class T> class TypedMemoryBlock : public MemoryBlock
271 {
272 public:
273 typedef TypedMemoryBlock<T> self_t;
274
275 TypedMemoryBlock()
276 {
277 }
278
279 TypedMemoryBlock(self_t const& src)
280 : MemoryBlock(src)
281 {
282 }
283
284 self_t& operator=(self_t const& src)
285 {
286 MemoryBlock::operator=(src);
287 return *this;
288 }
289
290 T* getContent()
291 {
292 return static_cast<T*>(MemoryBlock::getContent());
293 }
294
295 T const* getContent() const
296 {
297 return static_cast<T const*>(MemoryBlock::getContent());
298 }
299
300 bool equals(MemoryBlock const& block) const;
301 bool equals(void const* block, size_t blockLength) const;
302
303
304 T* operator->()
305 {
306 return this->getContent();
307 }
308 T const* operator->() const
309 {
310 return this->getContent();
311 }
312
313 T& operator*()
314 {
315 return *this->getContent();
316 }
317 T const& operator*() const
318 {
319 return *this->getContent();
320 }
321 };
322
323 } // end of namespace gate
324
325 #endif
326