Jpp  18.5.2
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JSinglePointer.hh
Go to the documentation of this file.
1 #ifndef __JLANG__JSINGLEPOINTER__
2 #define __JLANG__JSINGLEPOINTER__
3 
4 #include "JLang/JStorage.hh"
5 
6 
7 /**
8  * \author mdejong
9  */
10 
11 namespace JLANG {}
12 namespace JPP { using namespace JLANG; }
13 
14 namespace JLANG {
15 
16  /**
17  * The template JSinglePointer class can be used to hold a pointer to an object.
18  * The object pointed to is deleted at destruction.
19  * It is possible to create a container with single pointers.
20  * The first template argument refers to the data type pointed to
21  * and the second to the memory management policy.
22  */
23  template<class JClass_t, template<class> class JMemory_t = JNew>
25  public JStorage<JClass_t, JMemory_t>
26  {
27  public:
28 
29  /**
30  * Default constructor.
31  */
33  {}
34 
35 
36  /**
37  * Copy constructor.
38  * The ownership of the object pointed to is transferred to this single pointer.
39  *
40  * \param object single pointer
41  */
43  {
44  this->set(object.get());
45 
46  const_cast<JSinglePointer&>(object).set(NULL);
47  }
48 
49 
50  /**
51  * Constructor.
52  *
53  * \param p pointer to object
54  */
55  JSinglePointer(JClass_t* p)
56  {
57  this->set(p);
58  }
59 
60 
61  /**
62  * Destructor.
63  */
65  {
66  this->reset();
67  }
68 
69 
70  /**
71  * Assignment operator.
72  * The object pointed to previously is deleted.
73  * The ownership of the object pointed to is transferred to this single pointer.
74  *
75  * \param object single pointer
76  * \return this single pointer
77  */
79  {
80  if (this->get() != object.get()) {
81 
82  this->reset();
83 
84  if (object.is_valid()) {
85 
86  this->set(object.get());
87 
88  const_cast<JSinglePointer&>(object).set(NULL);
89  }
90  }
91 
92  return *this;
93  }
94 
95 
96  /**
97  * Assignment operator.
98  * The object pointed to previously is deleted.
99  *
100  * \param p pointer to derived class object
101  * \return this single pointer
102  */
103  template<class JDerived_t>
104  JSinglePointer& operator=(JDerived_t* p)
105  {
106  this->reset(p);
107 
108  return *this;
109  }
110  };
111 }
112 
113 #endif
Template storage class.
Definition: JStorage.hh:26
Memory management class for create/release policy based on new/delete.
Definition: JMemory.hh:23
JSinglePointer & operator=(const JSinglePointer &object)
Assignment operator.
JSinglePointer()
Default constructor.
virtual void reset() override
Reset pointer.
Definition: JStorage.hh:42
bool is_valid() const
Check validity of pointer.
JSinglePointer(const JSinglePointer &object)
Copy constructor.
The template JSinglePointer class can be used to hold a pointer to an object.
JSinglePointer & operator=(JDerived_t *p)
Assignment operator.
JSinglePointer(JClass_t *p)
Constructor.
virtual void set(JClass_t *p) override
Set pointer.
Definition: JPointer.hh:75
~JSinglePointer()
Destructor.