Jpp  18.5.0
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JAbstractPointer.hh
Go to the documentation of this file.
1 #ifndef __JLANG__JABSTRACTPOINTER__
2 #define __JLANG__JABSTRACTPOINTER__
3 
4 #include <cstdlib>
5 
6 #include "JLang/JException.hh"
7 #include "JLang/JEquals.hh"
8 
9 
10 /**
11  * \author mdejong
12  */
13 
14 namespace JLANG {}
15 namespace JPP { using namespace JLANG; }
16 
17 namespace JLANG {
18 
19 
20  /**
21  * Template interface for pointer to object(s).
22  */
23  template<class JClass_t>
25  public JEquals< JAbstractPointer<JClass_t> >
26  {
27  protected:
28  /**
29  * Default constructor.
30  */
32  {}
33 
34 
35  public:
36  /**
37  * Virtual destructor.
38  */
40  {}
41 
42 
43  /**
44  * Equals.
45  * The equality is evaluated by comparison of the internal pointers.
46  *
47  * \param object abstract pointer
48  * \return true if equals; else false
49  */
50  virtual bool equals(const JAbstractPointer& object) const
51  {
52  return this->get() == object.get();
53  }
54 
55 
56  /**
57  * Get pointer.
58  *
59  * \return pointer to object
60  */
61  virtual JClass_t* get() const = 0;
62 
63 
64  /**
65  * Set pointer.
66  *
67  * \param p pointer to object
68  */
69  virtual void set(JClass_t* p) = 0;
70 
71 
72  /**
73  * Reset pointer.
74  */
75  virtual void reset() = 0;
76 
77 
78  /**
79  * Check validity of pointer.
80  *
81  * \return true if pointer not null; else false
82  */
83  bool is_valid() const
84  {
85  return this->get() != NULL;
86  }
87 
88 
89  /**
90  * Reset pointer.
91  *
92  * \param p pointer to object
93  */
94  void reset(JClass_t* p)
95  {
96  if (this->get() != p) {
97 
98  this->reset();
99 
100  if (p != NULL) {
101  this->set(p);
102  }
103  }
104  }
105 
106 
107  /**
108  * Smart pointer operator.
109  *
110  * \return pointer to object
111  */
112  JClass_t* operator->() const
113  {
114  if (!is_valid())
115  THROW(JNullPointerException, "JAbstractPointer::operator->()");
116  else
117  return this->get();
118  }
119 
120 
121  /**
122  * Type conversion operator.
123  *
124  * \return pointer to object
125  */
126  operator JClass_t*() const
127  {
128  return this->get();
129  }
130  };
131 }
132 
133 #endif
virtual ~JAbstractPointer()
Virtual destructor.
Exceptions.
virtual void reset()=0
Reset pointer.
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:712
bool is_valid() const
Check validity of pointer.
Exception for null pointer operation.
Definition: JException.hh:232
JAbstractPointer()
Default constructor.
Template definition of auxiliary base class for comparison of data structures.
Definition: JEquals.hh:24
void reset(JClass_t *p)
Reset pointer.
virtual bool equals(const JAbstractPointer &object) const
Equals.
JClass_t * operator->() const
Smart pointer operator.
Template interface for pointer to object(s).
virtual void set(JClass_t *p)=0
Set pointer.