OPAL (Object Oriented Parallel Accelerator Library)  2.2.0
OPAL
OwnPtr.h
Go to the documentation of this file.
1 #ifndef CLASSIC_OwnPtr_HH
2 #define CLASSIC_OwnPtr_HH
3 
4 // ------------------------------------------------------------------------
5 // $RCSfile: OwnPtr.h,v $
6 // ------------------------------------------------------------------------
7 // $Revision: 1.1.1.1 $
8 // ------------------------------------------------------------------------
9 // Copyright: see Copyright.readme
10 // ------------------------------------------------------------------------
11 //
12 // Template class: OwnPtr<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 OwnPtr
25 // ------------------------------------------------------------------------
27 // When the pointer goes out of scope, the pointee is deleted.
28 // When an OwnPtr is assigned the ownership is transferred to the target.
29 
30 template <class Object>
31 class OwnPtr {
32 
33 public:
34 
36  // Set the pointer to NULL.
37  OwnPtr();
38 
40  // Transmits ownership.
41  OwnPtr(const OwnPtr &);
42 
44  // Construct pointer to an object just created by "new". Grabs ownership.
45  OwnPtr(Object *);
46 
48  // Delete the pointed-at object, if the pointer is non-null.
49  ~OwnPtr();
50 
52  // Transmit ownership to copy.
53  OwnPtr &operator=(const OwnPtr &);
54 
56  // Grab the ownership of an object just created by "new".
58 
60  // Return a C-type pointer to the object.
61  Object *operator->() const;
62 
64  // Return a reference to the object.
65  Object &operator*() const;
66 
68  // Return true, if the pointer is not NULL.
69  bool isValid() const;
70 
72  // Return built-in pointer.
73  // The calling program must take over the ownership.
74  Object *release();
75 
76 private:
77 
78  // The object pointed at.
79  mutable Object *object;
80 };
81 
82 
83 // Inline functions for template class OwnPtr
84 // ------------------------------------------------------------------------
85 
86 template <class Object>
88  object(0)
89 {}
90 
91 
92 template <class Object>
94  object(rhs.object) {
95  rhs.object = 0;
96 }
97 
98 
99 template <class Object>
101  object(obj)
102 {}
103 
104 
105 template <class Object>
107  delete object;
108 }
109 
110 
111 template <class Object>
113  if(object != rhs.object) {
114  delete object;
115  object = rhs.object;
116  rhs.object = 0;
117  }
118 
119  return *this;
120 }
121 
122 
123 template <class Object>
125  delete object;
126  object = obj;
127  return *this;
128 }
129 
130 
131 template <class Object>
133  return object;
134 }
135 
136 
137 template <class Object>
139  return *object;
140 }
141 
142 
143 template <class Object>
144 inline bool OwnPtr<Object>::isValid() const {
145  return object != 0;
146 }
147 
148 
149 template <class Object>
151  Object *temp = object;
152  object = 0;
153  return temp;
154 }
155 
156 #endif // CLASSIC_OwnPtr_HH
OwnPtr & operator=(const OwnPtr &)
Assign.
Definition: OwnPtr.h:112
~OwnPtr()
Destructor.
Definition: OwnPtr.h:106
Object * operator->() const
Delegation operator.
Definition: OwnPtr.h:132
A pointer which owns the object pointed at.
Definition: OwnPtr.h:31
Object * release()
Release ownership.
Definition: OwnPtr.h:150
bool isValid() const
Test for validity.
Definition: OwnPtr.h:144
OwnPtr()
Default constructor.
Definition: OwnPtr.h:87
The base class for all OPAL objects.
Definition: Object.h:48
Object * object
Definition: OwnPtr.h:79
Object & operator*() const
Dereferencing operator.
Definition: OwnPtr.h:138