OPAL (Object Oriented Parallel Accelerator Library)  2021.1.99
OPAL
Pointer.h
Go to the documentation of this file.
1 #ifndef CLASSIC_Pointer_HH
2 #define CLASSIC_Pointer_HH
3 
4 // ------------------------------------------------------------------------
5 // $RCSfile: Pointer.h,v $
6 // ------------------------------------------------------------------------
7 // $Revision: 1.1.1.1 $
8 // ------------------------------------------------------------------------
9 // Copyright: see Copyright.readme
10 // ------------------------------------------------------------------------
11 //
12 // Template class: Pointer<Object>
13 //
14 // ------------------------------------------------------------------------
15 // Class category: MemoryManagement
16 // ------------------------------------------------------------------------
17 //
18 // $Date: 2000/03/27 09:32:37 $
19 // $Author: fci $
20 //
21 // ------------------------------------------------------------------------
22 
23 
24 // Template class Pointer
25 // ------------------------------------------------------------------------
27 // This template class implements a pointer with reference counting in
28 // collaboration with the class RCObject. It is modelled after the class
29 // RCPtr described in:
30 // {center}
31 // Scott Meyers, More Effective C++, Addison Wesley, 1996, pg. 195.
32 // {/center}
33 // When the pointer goes out of scope, the pointee's reference count is
34 // decremented, and, if it becomes zero, the pointee is deleted.
35 // When a Pointer is assigned, the pointee's reference count is incremented.
36 
37 template < class Object >
38 class Pointer {
39 
40 public:
41 
43  // Set the pointer to NULL.
45 
47  // Increment reference count of pointee.
48  Pointer(const Pointer &);
49 
51  // Grab an object just created by "new" and set its reference count to one.
53 
55  // Decrement reference count and delete object if it reaches zero.
57 
59  // Increment reference count of pointee.
61 
63  // Grab an object just created by "new" and set its reference count to one.
65 
67  // Return a C-type pointer to the object.
68  Object *operator->() const;
69 
71  // Return a reference to the object.
72  Object &operator*() const;
73 
75  bool operator==(const Pointer &) const;
76 
78  bool operator!=(const Pointer &) const;
79 
81  // Return true, if the pointer is not NULL.
82  bool isValid() const;
83 
85  // Copy the object and make *this point to it.
86  void unique();
87 
88 private:
89 
90  // The object pointed at.
92 };
93 
94 
95 // Inline functions for template class Pointer
96 // ------------------------------------------------------------------------
97 
98 template < class Object >
100  object(0)
101 {}
102 
103 
104 template < class Object >
106  object(rhs.object) {
107  if(object) object->addReference();
108 }
109 
110 
111 template < class Object >
113  object(obj) {
114  if(object) object->addReference();
115 }
116 
117 
118 template < class Object >
120  if(object && object->removeReference() <= 0) delete object;
121 }
122 
123 
124 template < class Object >
126  if(object != rhs.object) {
127  if(object != 0 && object->removeReference() <= 0) delete object;
128  object = rhs.object;
129  if(object) object->addReference();
130  }
131 
132  return *this;
133 }
134 
135 
136 template < class Object >
138  if(object != obj) {
139  if(object && object->removeReference() <= 0) delete object;
140  object = obj;
141  if(object) object->addReference();
142  }
143  return *this;
144 }
145 
146 
147 template < class Object >
149  return object;
150 }
151 
152 
153 template < class Object >
155  return *object;
156 }
157 
158 
159 template < class Object >
160 inline bool Pointer<Object>::operator==(const Pointer &rhs) const {
161  return object == rhs.object;
162 }
163 
164 
165 template < class Object >
166 inline bool Pointer<Object>::operator!=(const Pointer &rhs) const {
167  return object != rhs.object;
168 }
169 
170 
171 template < class Object >
172 inline bool Pointer<Object>::isValid() const {
173  return object != 0;
174 }
175 
176 
177 template < class Object >
178 inline void Pointer<Object>::unique() {
179  if(object != 0 && object->isShared()) {
180  // The static cast is safe, since the declaration says
181  // "Object *object;"
182  object->removeReference();
183  object = static_cast<Object *>(object->clone());
184  object->addReference();
185  }
186 }
187 
188 #endif // CLASSIC_Pointer_HH
The base class for all OPAL objects.
Definition: Object.h:48
Reference-counted pointer.
Definition: Pointer.h:38
~Pointer()
Destructor.
Definition: Pointer.h:119
bool isValid() const
Test for validity.
Definition: Pointer.h:172
void unique()
Force unique.
Definition: Pointer.h:178
Pointer & operator=(Object *)
Assign.
Definition: Pointer.h:137
Object & operator*() const
Dereferencing operator.
Definition: Pointer.h:154
Pointer(Object *)
Constructor.
Definition: Pointer.h:112
Pointer()
Default constructor.
Definition: Pointer.h:99
Object * object
Definition: Pointer.h:91
Object * operator->() const
Delegation operator.
Definition: Pointer.h:148
Pointer & operator=(const Pointer &)
Assign.
Definition: Pointer.h:125
bool operator==(const Pointer &) const
Pointer equality.
Definition: Pointer.h:160
Pointer(const Pointer &)
Copy constructor.
Definition: Pointer.h:105
bool operator!=(const Pointer &) const
Pointer inequality.
Definition: Pointer.h:166
int addReference() const
Increment reference count.
Definition: RCObject.h:83