Jpp  18.0.0-rc.4
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JStorage.hh
Go to the documentation of this file.
1 #ifndef __JLANG__JSTORAGE__
2 #define __JLANG__JSTORAGE__
3 
4 #include "JLang/JPointer.hh"
5 #include "JLang/JMemory.hh"
6 
7 
8 /**
9  * \author mdejong
10  */
11 
12 namespace JLANG {}
13 namespace JPP { using namespace JLANG; }
14 
15 namespace JLANG {
16 
17  /**
18  * Template storage class.
19  * This class extends the JPointer class with storage capabilities.
20  * The first template argument refers to the data type pointed to
21  * and the second (optional argument) to the memory management policy (see e.g.\ JMemory.hh).
22  * The method create() can be used to allocate memory;
23  * the method reset() releases the allocated memory.
24  */
25  template<class JClass_t, template<class> class JMemory_t = JNew>
26  class JStorage :
27  public JPointer <JClass_t>,
28  public JMemory_t<JClass_t>
29  {
30  public:
31 
33  typedef JMemory_t<JClass_t> memory_type;
34 
35  using pointer_type::reset;
36 
37 
38  /**
39  * Reset pointer.
40  * The allocated memory is released.
41  */
42  virtual void reset() override
43  {
44  if (this->is_valid()) {
45  this->release();
46  }
47 
49  }
50 
51 
52  /**
53  * Recreate object in memory.
54  * A new object is created if no memory is allocated yet,
55  * else the previously created object is maintained.
56  */
57  void recreate()
58  {
59  if (!this->is_valid()) {
60  this->set(memory_type::create());
61  }
62  }
63 
64 
65  /**
66  * Create object in memory.
67  * The memory allocated by a previously created object will be released.
68  */
69  void create()
70  {
71  this->reset(memory_type::create());
72  }
73 
74 
75  /**
76  * Create array of objects in memory.
77  * The memory allocated by previously created objects will be released.
78  *
79  * \param size number of elements
80  */
81  void create(const unsigned int size)
82  {
83  this->reset(memory_type::create(size));
84  }
85 
86 
87  protected:
88  /**
89  * Release memory.
90  */
91  void release()
92  {
93  memory_type::release(this->get());
94  }
95  };
96 }
97 
98 #endif
Template storage class.
Definition: JStorage.hh:26
Memory management class for create/release policy based on new/delete.
Definition: JMemory.hh:23
void create()
Create object in memory.
Definition: JStorage.hh:69
virtual void reset() override
Reset pointer.
Definition: JStorage.hh:42
JMemory_t< JClass_t > memory_type
Definition: JStorage.hh:33
bool is_valid() const
Check validity of pointer.
JPointer< JClass_t > pointer_type
Definition: JStorage.hh:32
void recreate()
Recreate object in memory.
Definition: JStorage.hh:57
Template implementation of class that holds pointer to object(s).
Definition: JPointer.hh:22
void create(const unsigned int size)
Create array of objects in memory.
Definition: JStorage.hh:81
void release()
Release memory.
Definition: JStorage.hh:91
Base class for memory management.
virtual void set(JClass_t *p) override
Set pointer.
Definition: JPointer.hh:75
virtual void reset() override
Reset pointer.
Definition: JPointer.hh:84