Geogram Version 1.10.2
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
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
60namespace GEO {
61
69 class GEOGRAM_API LifeCycle : public Counted {
70 public:
71
76 LifeCycle(size_t object_size, bool is_pod=false) :
77 object_size_(object_size), is_pod_(is_pod) {
78 }
79
83 ~LifeCycle() override;
84
89 size_t object_size() const {
90 return object_size_;
91 }
92
101 bool is_pod() const {
102 return is_pod_;
103 }
104
111 virtual void construct(Memory::pointer address) = 0;
112
121 virtual void copy_construct(
123 ) = 0;
124
130 virtual void destroy(Memory::pointer address) = 0;
131
137 virtual void assign(Memory::pointer lhs, Memory::const_pointer rhs) = 0;
138
145 virtual void reset(Memory::pointer address) = 0;
146
147
152 virtual void swap(Memory::pointer a, Memory::pointer b) = 0;
153
161 virtual void construct_array(Memory::pointer address, index_t nb) = 0;
162
173 ) = 0;
174
181 virtual void destroy_array(Memory::pointer address, index_t nb) = 0;
182
191 virtual void assign_array(
193 ) = 0;
194
195
204 virtual void reset_array(Memory::pointer address, index_t nb) = 0;
205
211
219
224 virtual void delete_object(Memory::pointer address) = 0;
225
231
237 virtual void delete_array(Memory::pointer address) = 0;
238
239
240 private:
241 size_t object_size_;
242 bool is_pod_;
243 };
244
249
250 /*************************************************************************/
251
255 template <class T> class GenericLifeCycle : public LifeCycle {
256 public:
257
262 LifeCycle(
263 sizeof(T),
264 (std::is_standard_layout<T>::value && std::is_trivial<T>::value)
265 ) {
266 }
267
271 void construct(Memory::pointer address) override {
272 new(address)T;
273 }
274
280 ) override {
281 new(lhs)T(*(const T*)rhs);
282 }
283
287 void destroy(Memory::pointer address) override {
288 geo_argused(address); // Silences a MSVC warning.
289 ((T*)address)->~T();
290 }
291
296 *(T*)lhs=*(const T*)rhs;
297 }
298
302 void reset(Memory::pointer address) override {
303 *(T*)address = T();
304 }
305
309 std::swap(*(T*)a, *(T*)b);
310 }
311
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
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
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
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
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
374 return Memory::pointer(new T);
375 }
376
381 return Memory::pointer(new T(*(const T*)rhs));
382 }
383
387 void delete_object(Memory::pointer address) override {
388 delete((T*)address);
389 }
390
395 return Memory::pointer(new T[nb]);
396 }
397
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
Counted()
Creates a reference counted object.
Definition counted.h:142
void construct(Memory::pointer address) override
Calls the constructor of an object.
Definition life_cycle.h:271
void reset(Memory::pointer address) override
Resets an object to its default value.
Definition life_cycle.h:302
void delete_object(Memory::pointer address) override
Deletes an object.
Definition life_cycle.h:387
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:349
void destroy_array(Memory::pointer address, index_t nb) override
Destroys an array of object at a given address.
Definition life_cycle.h:338
void reset_array(Memory::pointer address, index_t nb) override
Resets all objects in an array to their default value.
Definition life_cycle.h:362
Memory::pointer new_object() override
Dynamically allocates a new object.
Definition life_cycle.h:373
GenericLifeCycle()
GenericLifeCycle constructor.
Definition life_cycle.h:261
void construct_array(Memory::pointer address, index_t nb) override
Calls the constructor of objects in an array.
Definition life_cycle.h:315
void destroy(Memory::pointer address) override
Calls the destructor of an object.
Definition life_cycle.h:287
Memory::pointer new_object(Memory::const_pointer rhs) override
Dynamically allocates a new object.
Definition life_cycle.h:380
void assign(Memory::pointer lhs, Memory::const_pointer rhs) override
Calls the assignment operator.
Definition life_cycle.h:295
Memory::pointer new_array(index_t nb) override
Dynamically allocates an array of objects.
Definition life_cycle.h:394
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:325
void swap(Memory::pointer a, Memory::pointer b) override
Swaps the objects at two addresses.
Definition life_cycle.h:308
void copy_construct(Memory::pointer lhs, Memory::const_pointer rhs) override
Copy-Constructs an object at a given address.
Definition life_cycle.h:278
void delete_array(Memory::pointer address) override
Deletes an array of objects.
Definition life_cycle.h:401
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:101
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:89
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:76
~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:105
const byte * const_pointer
Const pointer to unsigned byte(s).
Definition memory.h:108
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:248
geo_index_t index_t
The type for storing and manipulating indices.
Definition numeric.h:341