Geogram Version 1.9.7
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::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
258 GenericLifeCycle() : LifeCycle(sizeof(T), std::is_pod<T>::value) {
259 }
260
264 void construct(Memory::pointer address) override {
265 new(address)T;
266 }
267
272 new(lhs)T(*(T*)rhs);
273 }
274
278 void destroy(Memory::pointer address) override {
279 geo_argused(address); // Silences a MSVC warning.
280 ((T*)address)->~T();
281 }
282
286 void assign(Memory::pointer lhs, Memory::pointer rhs) override {
287 *(T*)lhs=*(T*)rhs;
288 }
289
293 void reset(Memory::pointer address) override {
294 *(T*)address = T();
295 }
300 std::swap(*(T*)a, *(T*)b);
301 }
302
306 void construct_array(Memory::pointer address, index_t nb) override {
307 T* array = (T*)address;
308 for(index_t i=0; i<nb; ++i) {
309 new(&array[i])T;
310 }
311 }
312
318 ) override {
319 T* lhs_array = (T*)lhs;
320 T* rhs_array = (T*)rhs;
321 for(index_t i=0; i<nb; ++i) {
322 new(&lhs_array[i])T(rhs_array[i]);
323 }
324 }
325
329 void destroy_array(Memory::pointer address, index_t nb) override {
330 T* array = (T*)address;
331 geo_argused(array); // Silences a MSVC warning.
332 for(index_t i=0; i<nb; ++i) {
333 array[i].~T();
334 }
335 }
336
342 ) override {
343 T* lhs_array = (T*)lhs;
344 T* rhs_array = (T*)rhs;
345 for(index_t i=0; i<nb; ++i) {
346 lhs_array[i] = rhs_array[i];
347 }
348 }
349
353 void reset_array(Memory::pointer address, index_t nb) override {
354 T* array = (T*)address;
355 geo_argused(array); // Silences a MSVC warning.
356 for(index_t i=0; i<nb; ++i) {
357 array[i] = T();
358 }
359 }
360
365 return Memory::pointer(new T);
366 }
367
372 return Memory::pointer(new T(*(T*)rhs));
373 }
374
378 void delete_object(Memory::pointer address) override {
379 delete((T*)address);
380 }
381
386 return Memory::pointer(new T[nb]);
387 }
388
392 void delete_array(Memory::pointer address) override {
393 delete[]((T*)address);
394 }
395
396 };
397
398}
399
400#ifdef GEO_COMPILER_CLANG
401#pragma GCC diagnostic pop
402#endif
403
404#endif
Base class for reference-counted objects.
Definition counted.h:71
Concrete implementation of LifeCycle.
Definition life_cycle.h:252
void assign_array(Memory::pointer lhs, Memory::pointer rhs, index_t nb) override
Copies an array of object at a given address.
Definition life_cycle.h:340
void construct(Memory::pointer address) override
Calls the constructor of an object.
Definition life_cycle.h:264
void reset(Memory::pointer address) override
Resets an object to its default value.
Definition life_cycle.h:293
Memory::pointer new_object(Memory::pointer rhs) override
Dynamically allocates a new object.
Definition life_cycle.h:371
void delete_object(Memory::pointer address) override
Deletes an object.
Definition life_cycle.h:378
void copy_construct_array(Memory::pointer lhs, Memory::pointer rhs, index_t nb) override
Copy-Constructs an array of object at a given address.
Definition life_cycle.h:316
void destroy_array(Memory::pointer address, index_t nb) override
Destroys an array of object at a given address.
Definition life_cycle.h:329
void reset_array(Memory::pointer address, index_t nb) override
Resets all objects in an array to their default value.
Definition life_cycle.h:353
Memory::pointer new_object() override
Dynamically allocates a new object.
Definition life_cycle.h:364
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:306
void destroy(Memory::pointer address) override
Calls the destructor of an object.
Definition life_cycle.h:278
void assign(Memory::pointer lhs, Memory::pointer rhs) override
Calls the assignment operator.
Definition life_cycle.h:286
void copy_construct(Memory::pointer lhs, Memory::pointer rhs) override
Copy-Constructs an object at a given address.
Definition life_cycle.h:271
Memory::pointer new_array(index_t nb) override
Dynamically allocates an array of objects.
Definition life_cycle.h:385
void swap(Memory::pointer a, Memory::pointer b) override
Swaps the objects at two addresses.
Definition life_cycle.h:299
void delete_array(Memory::pointer address) override
Deletes an array of objects.
Definition life_cycle.h:392
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 assign_array(Memory::pointer lhs, Memory::pointer rhs, index_t nb)=0
Copies 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::pointer rhs)=0
Copy-Constructs an object at a given address.
virtual Memory::pointer new_object(Memory::pointer rhs)=0
Dynamically allocates a new object with copy constructor.
virtual void reset(Memory::pointer address)=0
Resets an object to its default value.
virtual void copy_construct_array(Memory::pointer lhs, Memory::pointer rhs, index_t nb)=0
Copy-Constructs an array of object at a given address.
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 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::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
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