Jpp test-rotations-new
the software that should make you happy
Loading...
Searching...
No Matches
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
12namespace JLANG {}
13namespace JPP { using namespace JLANG; }
14
15namespace 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
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
Base class for memory management.
bool is_valid() const
Check validity of pointer.
Memory management class for create/release policy based on new/delete.
Definition JMemory.hh:23
Template implementation of class that holds pointer to object(s).
Definition JPointer.hh:24
JPointer()
Default constructor.
Definition JPointer.hh:33
virtual void set(JClass_t *p) override
Set pointer.
Definition JPointer.hh:75
virtual void reset() override
Reset pointer.
Definition JPointer.hh:84
virtual JClass_t * get() const override
Get pointer.
Definition JPointer.hh:64
Template storage class.
Definition JStorage.hh:29
JPointer< JClass_t > pointer_type
Definition JStorage.hh:32
void release()
Release memory.
Definition JStorage.hh:91
virtual void reset() override
Reset pointer.
Definition JStorage.hh:42
void create(const unsigned int size)
Create array of objects in memory.
Definition JStorage.hh:81
JMemory_t< JClass_t > memory_type
Definition JStorage.hh:33
void recreate()
Recreate object in memory.
Definition JStorage.hh:57
void create()
Create object in memory.
Definition JStorage.hh:69
Auxiliary classes and methods for language specific functionality.
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).