Geogram Version 1.9.8
A programming library of geometric algorithms
Loading...
Searching...
No Matches
life_cycle.h
Go to the documentation of this file.
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
46
52#ifdef GEO_COMPILER_CLANG
53#pragma GCC diagnostic push
54#pragma GCC diagnostic ignored "-Wcast-align"
55#endif
56
57namespace GEO {
58
66 class GEOGRAM_API LifeCycle : public Counted {
67 public:
68
73 LifeCycle(size_t object_size, bool is_pod=false) :
74 object_size_(object_size), is_pod_(is_pod) {
75 }
76
80 ~LifeCycle() override;
81
86 size_t object_size() const {
87 return object_size_;
88 }
89
98 bool is_pod() const {
99 return is_pod_;
100 }
101
108 virtual void construct(Memory::pointer address) = 0;
109
118 virtual void copy_construct(
120 ) = 0;
121
127 virtual void destroy(Memory::pointer address) = 0;
128
134 virtual void assign(Memory::pointer lhs, Memory::const_pointer rhs) = 0;
135
142 virtual void reset(Memory::pointer address) = 0;
143
144
149 virtual void swap(Memory::pointer a, Memory::pointer b) = 0;
150
158 virtual void construct_array(Memory::pointer address, index_t nb) = 0;
159
170 ) = 0;
171
178 virtual void destroy_array(Memory::pointer address, index_t nb) = 0;
179
188 virtual void assign_array(
190 ) = 0;
191
192
201 virtual void reset_array(Memory::pointer address, index_t nb) = 0;
202
208
216
221 virtual void delete_object(Memory::pointer address) = 0;
222
228
234 virtual void delete_array(Memory::pointer address) = 0;
235
236
237 private:
238 size_t object_size_;
239 bool is_pod_;
240 };
241
246
247 /*************************************************************************/
248
252 template <class T> class GenericLifeCycle : public LifeCycle {
253 public:
254
259 LifeCycle(
260 sizeof(T),
261 (std::is_standard_layout<T>::value && std::is_trivial<T>::value)
262 ) {
263 }
264
268 void construct(Memory::pointer address) override {
269 new(address)T;
270 }
271
277 ) override {
278 new(lhs)T(*(const T*)rhs);
279 }
280
284 void destroy(Memory::pointer address) override {
285 geo_argused(address); // Silences a MSVC warning.
286 ((T*)address)->~T();
287 }
288
293 *(T*)lhs=*(const T*)rhs;
294 }
295
299 void reset(Memory::pointer address) override {
300 *(T*)address = T();
301 }
306 std::swap(*(T*)a, *(T*)b);
307 }
308
312 void construct_array(Memory::pointer address, index_t nb) override {
313 T* array = (T*)address;
314 for(index_t i=0; i<nb; ++i) {
315 new(&array[i])T;
316 }
317 }
318
324 ) override {
325 T* lhs_array = (T*)lhs;
326 const T* rhs_array = (const T*)rhs;
327 for(index_t i=0; i<nb; ++i) {
328 new(&lhs_array[i])T(rhs_array[i]);
329 }
330 }
331
335 void destroy_array(Memory::pointer address, index_t nb) override {
336 T* array = (T*)address;
337 geo_argused(array); // Silences a MSVC warning.
338 for(index_t i=0; i<nb; ++i) {
339 array[i].~T();
340 }
341 }
342
348 ) override {
349 T* lhs_array = (T*)lhs;
350 const T* rhs_array = (const T*)rhs;
351 for(index_t i=0; i<nb; ++i) {
352 lhs_array[i] = rhs_array[i];
353 }
354 }
355
359 void reset_array(Memory::pointer address, index_t nb) override {
360 T* array = (T*)address;
361 geo_argused(array); // Silences a MSVC warning.
362 for(index_t i=0; i<nb; ++i) {
363 array[i] = T();
364 }
365 }
366
371 return Memory::pointer(new T);
372 }
373
378 return Memory::pointer(new T(*(const T*)rhs));
379 }
380
384 void delete_object(Memory::pointer address) override {
385 delete((T*)address);
386 }
387
392 return Memory::pointer(new T[nb]);
393 }
394
398 void delete_array(Memory::pointer address) override {
399 delete[]((T*)address);
400 }
401
402 };
403
404}
405
406#ifdef GEO_COMPILER_CLANG
407#pragma GCC diagnostic pop
408#endif
409
410#endif
Base class for reference-counted objects.
Definition counted.h:71
Concrete implementation of LifeCycle.
Definition life_cycle.h:252
void construct(Memory::pointer address) override
Calls the constructor of an object.
Definition life_cycle.h:268
void reset(Memory::pointer address) override
Resets an object to its default value.
Definition life_cycle.h:299
void delete_object(Memory::pointer address) override
Deletes an object.
Definition life_cycle.h:384
void assign_array(Memory::pointer lhs, Memory::const_pointer rhs, index_t nb) override
Copies an array of object at a given address.
Definition life_cycle.h:346
void destroy_array(Memory::pointer address, index_t nb) override
Destroys an array of object at a given address.
Definition life_cycle.h:335
void reset_array(Memory::pointer address, index_t nb) override
Resets all objects in an array to their default value.
Definition life_cycle.h:359
Memory::pointer new_object() override
Dynamically allocates a new object.
Definition life_cycle.h:370
GenericLifeCycle()
GenericLifeCycle constructor.
Definition life_cycle.h:258
void construct_array(Memory::pointer address, index_t nb) override
Calls the constructor of objects in an array.
Definition life_cycle.h:312
void destroy(Memory::pointer address) override
Calls the destructor of an object.
Definition life_cycle.h:284
Memory::pointer new_object(Memory::const_pointer rhs) override
Dynamically allocates a new object.
Definition life_cycle.h:377
void assign(Memory::pointer lhs, Memory::const_pointer rhs) override
Calls the assignment operator.
Definition life_cycle.h:292
Memory::pointer new_array(index_t nb) override
Dynamically allocates an array of objects.
Definition life_cycle.h:391
void copy_construct_array(Memory::pointer lhs, Memory::const_pointer rhs, index_t nb) override
Copy-Constructs an array of object at a given address.
Definition life_cycle.h:322
void swap(Memory::pointer a, Memory::pointer b) override
Swaps the objects at two addresses.
Definition life_cycle.h:305
void copy_construct(Memory::pointer lhs, Memory::const_pointer rhs) override
Copy-Constructs an object at a given address.
Definition life_cycle.h:275
void delete_array(Memory::pointer address) override
Deletes an array of objects.
Definition life_cycle.h:398
Manages the life cycle of an object.
Definition life_cycle.h:66
virtual Memory::pointer new_array(index_t nb)=0
Dynamically allocates an array of objects.
virtual void copy_construct_array(Memory::pointer lhs, Memory::const_pointer rhs, index_t nb)=0
Copy-Constructs an array of object at a given address.
virtual void delete_object(Memory::pointer address)=0
Deletes an object.
virtual void delete_array(Memory::pointer address)=0
Deletes an array of objects.
virtual void copy_construct(Memory::pointer lhs, Memory::const_pointer rhs)=0
Copy-Constructs an object at a given address.
virtual void assign_array(Memory::pointer lhs, Memory::const_pointer rhs, index_t nb)=0
Copies an array of object at a given address.
virtual void reset(Memory::pointer address)=0
Resets an object to its default value.
virtual void reset_array(Memory::pointer address, index_t nb)=0
Resets all objects in an array to their default value.
virtual void construct_array(Memory::pointer address, index_t nb)=0
Calls the constructor of objects in an array.
bool is_pod() const
Tests whether object is pod (plain ordinary datatype).
Definition life_cycle.h:98
virtual void destroy_array(Memory::pointer address, index_t nb)=0
Destroys an array of object at a given address.
virtual Memory::pointer new_object(Memory::const_pointer rhs)=0
Dynamically allocates a new object with copy constructor.
virtual void swap(Memory::pointer a, Memory::pointer b)=0
Swaps the objects at two addresses.
virtual void destroy(Memory::pointer address)=0
Calls the destructor of an object.
size_t object_size() const
Gets the size of an object.
Definition life_cycle.h:86
virtual void assign(Memory::pointer lhs, Memory::const_pointer rhs)=0
Calls the assignment operator.
virtual void construct(Memory::pointer address)=0
Calls the constructor of an object.
LifeCycle(size_t object_size, bool is_pod=false)
LifeCycle constructor.
Definition life_cycle.h:73
~LifeCycle() override
LifeCycle destructor.
virtual Memory::pointer new_object()=0
Dynamically allocates a new object.
A smart pointer with reference-counted copy semantics.
Base class of reference-counted objects, to be used with smart pointers.
Common include file, providing basic definitions. Should be included before anything else by all head...
Types and functions for memory manipulation.
byte * pointer
Pointer to unsigned byte(s)
Definition memory.h:104
const byte * const_pointer
Const pointer to unsigned byte(s)
Definition memory.h:107
Global Vorpaline namespace.
Definition basic.h:55
void geo_argused(const T &)
Suppresses compiler warnings about unused parameters.
Definition argused.h:60
SmartPointer< LifeCycle > LifeCycle_var
A reference_counted pointer to a LifeCycle.
Definition life_cycle.h:245
geo_index_t index_t
The type for storing and manipulating indices.
Definition numeric.h:329