Jpp
 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 "JLang/JException.hh"
5 #include "JLang/JEquals.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  /**
19  * Template interface for pointer to object(s).
20  */
21  template<class JClass_t>
23  public JEquals< JAbstractPointer<JClass_t> >
24  {
25  protected:
26  /**
27  * Default constructor.
28  */
30  {}
31 
32 
33  public:
34  /**
35  * Virtual destructor.
36  */
38  {}
39 
40 
41  /**
42  * Equals.
43  * The equality is evaluated by comparison of the internal pointers.
44  *
45  * \param object abstract pointer
46  * \return true if equals; else false
47  */
48  virtual bool equals(const JAbstractPointer& object) const
49  {
50  return this->get() == object.get();
51  }
52 
53 
54  /**
55  * Get pointer.
56  *
57  * \return pointer to object
58  */
59  virtual JClass_t* get() const = 0;
60 
61 
62  /**
63  * Set pointer.
64  *
65  * \param p pointer to object
66  */
67  virtual void set(JClass_t* p) = 0;
68 
69 
70  /**
71  * Reset pointer.
72  */
73  virtual void reset() = 0;
74 
75 
76  /**
77  * Check validity of pointer.
78  *
79  * \return true if pointer not null; else false
80  */
81  bool is_valid() const
82  {
83  return this->get() != NULL;
84  }
85 
86 
87  /**
88  * Reset pointer.
89  *
90  * \param p pointer to object
91  */
92  void reset(JClass_t* p)
93  {
94  if (this->get() != p) {
95 
96  this->reset();
97 
98  if (p != NULL) {
99  this->set(p);
100  }
101  }
102  }
103 
104 
105  /**
106  * Smart pointer operator.
107  *
108  * \return pointer to object
109  */
110  JClass_t* operator->() const
111  {
112  if (!is_valid())
113  throw JNullPointerException("JAbstractPointer::operator->()");
114  else
115  return this->get();
116  }
117 
118 
119  /**
120  * Type conversion operator.
121  *
122  * \return pointer to object
123  */
124  operator JClass_t*() const
125  {
126  return this->get();
127  }
128  };
129 }
130 
131 #endif
virtual ~JAbstractPointer()
Virtual destructor.
Exceptions.
virtual void reset()=0
Reset pointer.
bool is_valid() const
Check validity of pointer.
Exception for null pointer operation.
Definition: JException.hh:198
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.