Graphite  Version 3
An experimental 3D geometry processing program
life_cycle.h
1 /*
2  * OGF/Graphite: Geometry and Graphics Programming Library + Utilities
3  * Copyright (C) 2000 Bruno Levy
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  *
19  * If you modify this software, you should include a notice giving the
20  * name of the person performing the modification, the date of modification,
21  * and the reason for such modification.
22  *
23  * Contact: Bruno Levy
24  *
25  * levy@loria.fr
26  *
27  * ISA Project
28  * LORIA, INRIA Lorraine,
29  * Campus Scientifique, BP 239
30  * 54506 VANDOEUVRE LES NANCY CEDEX
31  * FRANCE
32  *
33  * Note that the GNU General Public License does not permit incorporating
34  * the Software into proprietary programs.
35  */
36 
37 #ifndef H_OGF_BASIC_ERTTI_SERIALIZER_H
38 #define H_OGF_BASIC_ERTTI_SERIALIZER_H
39 
40 #include <OGF/gom/common/common.h>
41 #include <geogram/basic/memory.h>
42 #include <geogram/basic/counted.h>
43 
49 #ifdef GEO_COMPILER_CLANG
50 #pragma GCC diagnostic push
51 #pragma GCC diagnostic ignored "-Wcast-align"
52 #endif
53 
54 namespace OGF {
55 
59  class GOM_API LifeCycle : public Counted {
60  public:
61 
66  LifeCycle(size_t object_size, bool is_pod=false) :
67  object_size_(object_size), is_pod_(is_pod) {
68  }
69 
73  virtual ~LifeCycle();
74 
79  size_t object_size() const {
80  return object_size_;
81  }
82 
91  bool is_pod() const {
92  return is_pod_;
93  }
94 
101  virtual void construct(Memory::pointer address) = 0;
102 
111  virtual void copy_construct(Memory::pointer lhs, Memory::pointer rhs) = 0;
112 
118  virtual void destroy(Memory::pointer address) = 0;
119 
125  virtual void assign(Memory::pointer lhs, Memory::pointer rhs) = 0;
126 
134  virtual void construct_array(Memory::pointer address, index_t nb) = 0;
135 
145 
152  virtual void destroy_array(Memory::pointer address, index_t nb) = 0;
153 
162  virtual void assign_array(Memory::pointer lhs, Memory::pointer rhs, index_t nb) = 0;
163 
169 
177 
182  virtual void delete_object(Memory::pointer address) = 0;
183 
189 
195  virtual void delete_array(Memory::pointer address) = 0;
196 
197  private:
198  size_t object_size_;
199  bool is_pod_;
200  };
201 
206 
207  /*************************************************************************/
208 
212  template <class T> class GenericLifeCycle : public LifeCycle {
213  public:
214 
218  GenericLifeCycle() : LifeCycle(sizeof(T), std::is_pod<T>::value) {
219  }
220 
224  void construct(Memory::pointer address) override {
225  new(address)T;
226  }
227 
232  new(lhs)T(*(T*)rhs);
233  }
234 
238  void destroy(Memory::pointer address) override {
239  geo_argused(address); // Silences a MSVC warning.
240  ((T*)address)->~T();
241  }
242 
246  void assign(Memory::pointer lhs, Memory::pointer rhs) override {
247  *(T*)lhs=*(T*)rhs;
248  }
249 
253  void construct_array(Memory::pointer address, index_t nb) override {
254  T* array = (T*)address;
255  for(index_t i=0; i<nb; ++i) {
256  new(&array[i])T;
257  }
258  }
259 
265  ) override {
266  T* lhs_array = (T*)lhs;
267  T* rhs_array = (T*)rhs;
268  for(index_t i=0; i<nb; ++i) {
269  new(&lhs_array[i])T(rhs_array[i]);
270  }
271  }
272 
276  void destroy_array(Memory::pointer address, index_t nb) override {
277  T* array = (T*)address;
278  geo_argused(array); // Silences a MSVC warning.
279  for(index_t i=0; i<nb; ++i) {
280  array[i].~T();
281  }
282  }
283 
289  ) override {
290  T* lhs_array = (T*)lhs;
291  T* rhs_array = (T*)rhs;
292  for(index_t i=0; i<nb; ++i) {
293  lhs_array[i] = rhs_array[i];
294  }
295  }
296 
301  return Memory::pointer(new T);
302  }
303 
308  return Memory::pointer(new T(*(T*)rhs));
309  }
310 
314  void delete_object(Memory::pointer address) override {
315  delete((T*)address);
316  }
317 
322  return Memory::pointer(new T[nb]);
323  }
324 
328  void delete_array(Memory::pointer address) override {
329  delete[]((T*)address);
330  }
331 
332  };
333 
334 }
335 
336 #ifdef GEO_COMPILER_CLANG
337 #pragma GCC diagnostic pop
338 #endif
339 
340 #endif
Base class for reference-counted objects.
Definition: counted.h:71
Concrete implementation of LifeCycle.
Definition: life_cycle.h:212
Memory::pointer new_object(Memory::pointer rhs) override
Dynamically allocates a new object.
Definition: life_cycle.h:307
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:287
void copy_construct(Memory::pointer lhs, Memory::pointer rhs) override
Copy-Constructs an object at a given address.
Definition: life_cycle.h:231
Memory::pointer new_object() override
Dynamically allocates a new object.
Definition: life_cycle.h:300
void delete_object(Memory::pointer address) override
Deletes an object.
Definition: life_cycle.h:314
void destroy_array(Memory::pointer address, index_t nb) override
Destroys an array of object at a given address.
Definition: life_cycle.h:276
Memory::pointer new_array(index_t nb) override
Dynamically allocates an array of objects.
Definition: life_cycle.h:321
GenericLifeCycle()
GenericLifeCycle constructor.
Definition: life_cycle.h:218
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:263
void construct_array(Memory::pointer address, index_t nb) override
Calls the constructor of objects in an array.
Definition: life_cycle.h:253
void destroy(Memory::pointer address) override
Calls the destructor of an object.
Definition: life_cycle.h:238
void delete_array(Memory::pointer address) override
Deletes an array of objects.
Definition: life_cycle.h:328
void assign(Memory::pointer lhs, Memory::pointer rhs) override
Calls the assignment operator.
Definition: life_cycle.h:246
void construct(Memory::pointer address) override
Calls the constructor of an object.
Definition: life_cycle.h:224
Manages the life cycle of an object.
Definition: life_cycle.h:59
bool is_pod() const
Tests whether object is pod (plain ordinary datatype).
Definition: life_cycle.h:91
virtual void destroy(Memory::pointer address)=0
Calls the destructor of an object.
virtual void construct(Memory::pointer address)=0
Calls the constructor of an object.
virtual void construct_array(Memory::pointer address, index_t nb)=0
Calls the constructor of objects in an array.
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::pointer rhs)=0
Dynamically allocates a new object with copy constructor.
virtual void assign_array(Memory::pointer lhs, Memory::pointer rhs, index_t nb)=0
Copies an array of object at a given address.
virtual Memory::pointer new_object()=0
Dynamically allocates a new object.
virtual void delete_object(Memory::pointer address)=0
Deletes an object.
virtual void copy_construct(Memory::pointer lhs, Memory::pointer rhs)=0
Copy-Constructs an object at a given address.
virtual void delete_array(Memory::pointer address)=0
Deletes an array of objects.
size_t object_size() const
Gets the size of an object.
Definition: life_cycle.h:79
virtual void assign(Memory::pointer lhs, Memory::pointer rhs)=0
Calls the assignment operator.
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.
LifeCycle(size_t object_size, bool is_pod=false)
LifeCycle constructor.
Definition: life_cycle.h:66
virtual ~LifeCycle()
LifeCycle destructor.
virtual Memory::pointer new_array(index_t nb)=0
Dynamically allocates an array of objects.
Base class of reference-counted objects, to be used with smart pointers.
Types and functions for memory manipulation.
byte * pointer
Pointer to unsigned byte(s)
Definition: memory.h:104
void geo_argused(const T &)
Suppresses compiler warnings about unused parameters.
Definition: argused.h:60
geo_index_t index_t
The type for storing and manipulating indices.
Definition: numeric.h:329
Global Graphite namespace.
Definition: common.h:76
SmartPointer< LifeCycle > LifeCycle_var
A reference_counted pointer to a LifeCycle.
Definition: life_cycle.h:205
Definitions common to all include files in the gom library.