OPAL (Object Oriented Parallel Accelerator Library)  2.2.0
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.
44  Pointer();
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.
52  Pointer(Object *);
53 
55  // Decrement reference count and delete object if it reaches zero.
56  ~Pointer();
57 
59  // Increment reference count of pointee.
60  Pointer &operator=(const Pointer &);
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 && object->removeReference() <= 0) delete object;
139  object = obj;
140  if(object) object->addReference();
141  return *this;
142 }
143 
144 
145 template < class Object >
147  return object;
148 }
149 
150 
151 template < class Object >
153  return *object;
154 }
155 
156 
157 template < class Object >
158 inline bool Pointer<Object>::operator==(const Pointer &rhs) const {
159  return object == rhs.object;
160 }
161 
162 
163 template < class Object >
164 inline bool Pointer<Object>::operator!=(const Pointer &rhs) const {
165  return object != rhs.object;
166 }
167 
168 
169 template < class Object >
170 inline bool Pointer<Object>::isValid() const {
171  return object != 0;
172 }
173 
174 
175 template < class Object >
176 inline void Pointer<Object>::unique() {
177  if(object != 0 && object->isShared()) {
178  // The static cast is safe, since the declaration says
179  // "Object *object;"
180  object->removeReference();
181  object = static_cast<Object *>(object->clone());
182  object->addReference();
183  }
184 }
185 
186 #endif // CLASSIC_Pointer_HH
Object & operator*() const
Dereferencing operator.
Definition: Pointer.h:152
Object * operator->() const
Delegation operator.
Definition: Pointer.h:146
Pointer & operator=(const Pointer &)
Assign.
Definition: Pointer.h:125
Pointer()
Default constructor.
Definition: Pointer.h:99
~Pointer()
Destructor.
Definition: Pointer.h:119
bool operator!=(const Pointer &) const
Pointer inequality.
Definition: Pointer.h:164
Object * object
Definition: Pointer.h:91
int addReference() const
Increment reference count.
Definition: RCObject.h:85
Reference-counted pointer.
Definition: Pointer.h:38
bool isValid() const
Test for validity.
Definition: Pointer.h:170
void unique()
Force unique.
Definition: Pointer.h:176
The base class for all OPAL objects.
Definition: Object.h:48
bool operator==(const Pointer &) const
Pointer equality.
Definition: Pointer.h:158