GCC Code Coverage Report


Directory: ./
File: basic/life_cycle.h
Date: 2026-11-30 05:45:57
Exec Total Coverage
Lines: 6 51 11.8%
Functions: 1 208 0.5%
Branches: 0 29 0.0%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2000-2022 Inria
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * * Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * * Neither the name of the ALICE Project-Team nor the names of its
14 * contributors may be used to endorse or promote products derived from this
15 * software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
28 *
29 * Contact: Bruno Levy
30 *
31 * https://www.inria.fr/fr/bruno-levy
32 *
33 * Inria,
34 * Domaine de Voluceau,
35 * 78150 Le Chesnay - Rocquencourt
36 * FRANCE
37 *
38 */
39
40 #ifndef GEOGRAM_BASIC_LIFE_CYCLE
41 #define GEOGRAM_BASIC_LIFE_CYCLE
42
43 #include <geogram/basic/common.h>
44 #include <geogram/basic/memory.h>
45 #include <geogram/basic/counted.h>
46
47 /**
48 * \file geogram/basic/life_cycle.h
49 * \brief Implementation of generic object lifecycle service
50 */
51
52 #ifdef GEO_COMPILER_CLANG
53 #pragma GCC diagnostic push
54 #pragma GCC diagnostic ignored "-Wcast-align"
55 # if __has_warning("-Wallocator-wrappers")
56 # pragma clang diagnostic ignored "-Wallocator-wrappers"
57 # endif
58 #endif
59
60 namespace GEO {
61
62 /**
63 * \brief Manages the life cycle of an object.
64 * \details Provides ways to call the constructor, destructor, copy
65 * constructor and assignation operator on objects known only from
66 * their addresses as raw untyped pointers. This is used by the
67 * Attribute mechanism of Geogram and by the Any class in Graphite/GOM.
68 */
69 class GEOGRAM_API LifeCycle : public Counted {
70 public:
71
72 /**
73 * \brief LifeCycle constructor.
74 * \param[in] object_size the size of an object in bytes.
75 */
76 1 LifeCycle(size_t object_size, bool is_pod=false) :
77 1 object_size_(object_size), is_pod_(is_pod) {
78 }
79
80 /**
81 * \brief LifeCycle destructor.
82 */
83 ~LifeCycle() override;
84
85 /**
86 * \brief Gets the size of an object.
87 * \return the size of an object in bytes.
88 */
89 size_t object_size() const {
90 return object_size_;
91 }
92
93 /**
94 * \brief Tests whether object is pod (plain ordinary
95 * datatype).
96 * \details Plain ordinary datatypes can be copied with
97 * memcpy().
98 * \retval true if object is pod.
99 * \retval false otherwise.
100 */
101 bool is_pod() const {
102 return is_pod_;
103 }
104
105 /**
106 * \brief Calls the constructor of an object.
107 * \param[in] address the address of the object
108 * to be constructed.
109 * \details No memory allocation is done.
110 */
111 virtual void construct(Memory::pointer address) = 0;
112
113 /**
114 * \brief Copy-Constructs an object at a given address.
115 * \param[in] lhs the address where the object should
116 * be constructed.
117 * \param[in] rhs the address of the right-hand-side object
118 * to be copied.
119 * \details No memory allocation is done.
120 */
121 virtual void copy_construct(
122 Memory::pointer lhs, Memory::const_pointer rhs
123 ) = 0;
124
125 /**
126 * \brief Calls the destructor of an object.
127 * \param[in] address the address of the object to be destructed.
128 * \details No memory deallocation is done.
129 */
130 virtual void destroy(Memory::pointer address) = 0;
131
132 /**
133 * \brief Calls the assignment operator.
134 * \param[in] lhs the address of the left hand side.
135 * \param[in] rhs the address of the right hand side.
136 */
137 virtual void assign(Memory::pointer lhs, Memory::const_pointer rhs) = 0;
138
139 /**
140 * \brief Resets an object to its default value.
141 * \details Creates a new object with the default constructor and assigns
142 * it to the object at \p address.
143 * \param[in] address the address of an already initialized object.
144 */
145 virtual void reset(Memory::pointer address) = 0;
146
147
148 /**
149 * \brief Swaps the objects at two addresses
150 * \param[in] a , b pointers to the two objects to be swapped
151 */
152 virtual void swap(Memory::pointer a, Memory::pointer b) = 0;
153
154 /**
155 * \brief Calls the constructor of objects in an array.
156 * \param[in] address the address of the object array
157 * to be constructed.
158 * \param[in] nb number of objects.
159 * \details No memory allocation is done.
160 */
161 virtual void construct_array(Memory::pointer address, index_t nb) = 0;
162
163 /**
164 * \brief Copy-Constructs an array of object at a given address.
165 * \param[in] lhs the address of the objects to be constructed.
166 * \param[in] rhs the address of the right-hand-side objects
167 * to be copied.
168 * \param[in] nb number of objects
169 * \details No memory allocation is done.
170 */
171 virtual void copy_construct_array(
172 Memory::pointer lhs, Memory::const_pointer rhs, index_t nb
173 ) = 0;
174
175 /**
176 * \brief Destroys an array of object at a given address.
177 * \param[in] address the address of the objects to be destroyed.
178 * \param[in] nb number of objects
179 * \details No memory deallocation is done.
180 */
181 virtual void destroy_array(Memory::pointer address, index_t nb) = 0;
182
183 /**
184 * \brief Copies an array of object at a given address.
185 * \param[in] lhs the address of the left-hand-side objects
186 * \param[in] rhs the address of the right-hand-side objects
187 * to be copied.
188 * \param[in] nb number of objects
189 * \details No memory allocation is done.
190 */
191 virtual void assign_array(
192 Memory::pointer lhs, Memory::const_pointer rhs, index_t nb
193 ) = 0;
194
195
196 /**
197 * \brief Resets all objects in an array to their default value.
198 * \details creates an object with the default value and assigns
199 * it to the element for each element in the array.
200 * \param[in] address the address of the object array
201 * to be reset.
202 * \param[in] nb number of objects.
203 */
204 virtual void reset_array(Memory::pointer address, index_t nb) = 0;
205
206 /**
207 * \brief Dynamically allocates a new object.
208 * \return the address of the new object.
209 */
210 virtual Memory::pointer new_object() = 0;
211
212 /**
213 * \brief Dynamically allocates a new object with copy constructor.
214 * \param[in] rhs the address of the right-hand-side objects
215 * to be copied.
216 * \return the address of the new object.
217 */
218 virtual Memory::pointer new_object(Memory::const_pointer rhs) = 0;
219
220 /**
221 * \brief Deletes an object.
222 * \param[in] address the address of the object.
223 */
224 virtual void delete_object(Memory::pointer address) = 0;
225
226 /**
227 * \brief Dynamically allocates an array of objects.
228 * \param[in] nb number of objects.
229 */
230 virtual Memory::pointer new_array(index_t nb) = 0;
231
232 /**
233 * \brief Deletes an array of objects.
234 * \param[in] address the address of the object array to
235 * be deleted.
236 */
237 virtual void delete_array(Memory::pointer address) = 0;
238
239
240 private:
241 size_t object_size_;
242 bool is_pod_;
243 };
244
245 /**
246 * \brief A reference_counted pointer to a LifeCycle.
247 */
248 typedef SmartPointer<LifeCycle> LifeCycle_var;
249
250 /*************************************************************************/
251
252 /**
253 * \brief Concrete implementation of LifeCycle.
254 */
255 template <class T> class GenericLifeCycle : public LifeCycle {
256 public:
257
258 /**
259 * \brief GenericLifeCycle constructor.
260 */
261 GenericLifeCycle() :
262 LifeCycle(
263 sizeof(T),
264 (std::is_standard_layout<T>::value && std::is_trivial<T>::value)
265 1 ) {
266 }
267
268 /**
269 * \copydoc LifeCycle::construct()
270 */
271 ✗ void construct(Memory::pointer address) override {
272 new(address)T;
273 ✗ }
274
275 /**
276 * \copydoc LifeCycle::copy_construct()
277 */
278 ✗ void copy_construct(
279 Memory::pointer lhs, Memory::const_pointer rhs
280 ) override {
281 ✗ new(lhs)T(*(const T*)rhs);
282 ✗ }
283
284 /**
285 * \copydoc LifeCycle::destroy()
286 */
287 ✗ void destroy(Memory::pointer address) override {
288 geo_argused(address); // Silences a MSVC warning.
289 ✗ ((T*)address)->~T();
290 ✗ }
291
292 /**
293 * \copydoc LifeCycle::assign()
294 */
295 ✗ void assign(Memory::pointer lhs, Memory::const_pointer rhs) override {
296 ✗ *(T*)lhs=*(const T*)rhs;
297 ✗ }
298
299 /**
300 * \copydoc LifeCycle::reset()
301 */
302 163800 void reset(Memory::pointer address) override {
303 163800 *(T*)address = T();
304 163800 }
305 /**
306 * \copydoc LifeCycle::swap()
307 */
308 ✗ void swap(Memory::pointer a, Memory::pointer b) override {
309 std::swap(*(T*)a, *(T*)b);
310 ✗ }
311
312 /**
313 * \copydoc LifeCycle::construct_array()
314 */
315 ✗ void construct_array(Memory::pointer address, index_t nb) override {
316 T* array = (T*)address;
317 ✗ for(index_t i=0; i<nb; ++i) {
318 ✗ new(&array[i])T;
319 }
320 ✗ }
321
322 /**
323 * \copydoc LifeCycle::copy_construct_array()
324 */
325 ✗ void copy_construct_array(
326 Memory::pointer lhs, Memory::const_pointer rhs, index_t nb
327 ) override {
328 T* lhs_array = (T*)lhs;
329 const T* rhs_array = (const T*)rhs;
330 ✗ for(index_t i=0; i<nb; ++i) {
331 ✗ new(&lhs_array[i])T(rhs_array[i]);
332 }
333 ✗ }
334
335 /**
336 * \copydoc LifeCycle::destroy_array()
337 */
338 ✗ void destroy_array(Memory::pointer address, index_t nb) override {
339 T* array = (T*)address;
340 geo_argused(array); // Silences a MSVC warning.
341 ✗ for(index_t i=0; i<nb; ++i) {
342 ✗ array[i].~T();
343 }
344 ✗ }
345
346 /**
347 * \copydoc LifeCycle::assign_array()
348 */
349 ✗ void assign_array(
350 Memory::pointer lhs, Memory::const_pointer rhs, index_t nb
351 ) override {
352 T* lhs_array = (T*)lhs;
353 const T* rhs_array = (const T*)rhs;
354 ✗ for(index_t i=0; i<nb; ++i) {
355 ✗ lhs_array[i] = rhs_array[i];
356 }
357 ✗ }
358
359 /**
360 * \copydoc LifeCycle::reset_array()
361 */
362 ✗ void reset_array(Memory::pointer address, index_t nb) override {
363 T* array = (T*)address;
364 geo_argused(array); // Silences a MSVC warning.
365 ✗ for(index_t i=0; i<nb; ++i) {
366 ✗ array[i] = T();
367 }
368 ✗ }
369
370 /**
371 * \copydoc LifeCycle::new_object()
372 */
373 ✗ Memory::pointer new_object() override {
374 ✗ return Memory::pointer(new T);
375 }
376
377 /**
378 * \copydoc LifeCycle::new_object()
379 */
380 ✗ Memory::pointer new_object(Memory::const_pointer rhs) override {
381 ✗ return Memory::pointer(new T(*(const T*)rhs));
382 }
383
384 /**
385 * \copydoc LifeCycle::delete_object()
386 */
387 ✗ void delete_object(Memory::pointer address) override {
388 ✗ delete((T*)address);
389 ✗ }
390
391 /**
392 * \copydoc LifeCycle::new_array()
393 */
394 ✗ Memory::pointer new_array(index_t nb) override {
395 ✗ return Memory::pointer(new T[nb]);
396 }
397
398 /**
399 * \copydoc LifeCycle::delete_array()
400 */
401 ✗ void delete_array(Memory::pointer address) override {
402 ✗ delete[]((T*)address);
403 ✗ }
404
405 };
406
407 }
408
409 #ifdef GEO_COMPILER_CLANG
410 #pragma GCC diagnostic pop
411 #endif
412
413 #endif
414