Jpp
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
JException.hh
JLANG::JEquals
Template definition of auxiliary base class for comparison of data structures.
Definition: JEquals.hh:24
JLANG::JNullPointerException
Exception for null pointer operation.
Definition: JException.hh:216
JLANG::JAbstractPointer::reset
virtual void reset()=0
Reset pointer.
JEquals.hh
JPP
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Definition: JAAnetToolkit.hh:37
JLANG::JAbstractPointer::operator->
JClass_t * operator->() const
Smart pointer operator.
Definition: JAbstractPointer.hh:112
JLANG::JAbstractPointer::JAbstractPointer
JAbstractPointer()
Default constructor.
Definition: JAbstractPointer.hh:31
JLANG::JAbstractPointer
Template interface for pointer to object(s).
Definition: JAbstractPointer.hh:24
JLANG::JAbstractPointer::reset
void reset(JClass_t *p)
Reset pointer.
Definition: JAbstractPointer.hh:94
JLANG::JAbstractPointer::get
virtual JClass_t * get() const =0
Get pointer.
JLANG::JAbstractPointer::~JAbstractPointer
virtual ~JAbstractPointer()
Virtual destructor.
Definition: JAbstractPointer.hh:39
JLANG::JAbstractPointer::equals
virtual bool equals(const JAbstractPointer &object) const
Equals.
Definition: JAbstractPointer.hh:50
JLANG
Auxiliary classes and methods for language specific functionality.
Definition: JAbstractClass.hh:10
JLANG::JAbstractPointer::is_valid
bool is_valid() const
Check validity of pointer.
Definition: JAbstractPointer.hh:83
JLANG::JAbstractPointer::set
virtual void set(JClass_t *p)=0
Set pointer.