OPAL (Object Oriented Parallel Accelerator Library) 2022.1
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
30template <class Object>
31class OwnPtr {
32
33public:
34
36 // Set the pointer to nullptr.
38
40 // Transmits ownership.
41 OwnPtr(const OwnPtr &);
42
44 // Construct pointer to an object just created by "new". Grabs ownership.
46
48 // Delete the pointed-at object, if the pointer is non-null.
50
52 // Transmit ownership to copy.
54
56 // Grab the ownership of an object just created by "new".
58
60 // Return a C-type pointer to the object.
62
64 // Return a reference to the object.
65 Object &operator*() const;
66
68 // Return true, if the pointer is not nullptr.
69 bool isValid() const;
70
72 // Return built-in pointer.
73 // The calling program must take over the ownership.
75
76private:
77
78 // The object pointed at.
79 mutable Object *object;
80};
81
82
83// Inline functions for template class OwnPtr
84// ------------------------------------------------------------------------
85
86template <class Object>
88 object(0)
89{}
90
91
92template <class Object>
94 object(rhs.object) {
95 rhs.object = 0;
96}
97
98
99template <class Object>
101 object(obj)
102{}
103
104
105template <class Object>
107 delete object;
108}
109
110
111template <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
123template <class Object>
125 delete object;
126 object = obj;
127 return *this;
128}
129
130
131template <class Object>
133 return object;
134}
135
136
137template <class Object>
139 return *object;
140}
141
142
143template <class Object>
144inline bool OwnPtr<Object>::isValid() const {
145 return object != 0;
146}
147
148
149template <class Object>
151 Object *temp = object;
152 object = 0;
153 return temp;
154}
155
156#endif // CLASSIC_OwnPtr_HH
The base class for all OPAL objects.
Definition: Object.h:48
A pointer which owns the object pointed at.
Definition: OwnPtr.h:31
Object * operator->() const
Delegation operator.
Definition: OwnPtr.h:132
OwnPtr & operator=(Object *)
Assign.
Definition: OwnPtr.h:124
Object * object
Definition: OwnPtr.h:79
OwnPtr()
Default constructor.
Definition: OwnPtr.h:87
Object * release()
Release ownership.
Definition: OwnPtr.h:150
bool isValid() const
Test for validity.
Definition: OwnPtr.h:144
OwnPtr & operator=(const OwnPtr &)
Assign.
Definition: OwnPtr.h:112
~OwnPtr()
Destructor.
Definition: OwnPtr.h:106
OwnPtr(const OwnPtr &)
Copy constructor.
Definition: OwnPtr.h:93
Object & operator*() const
Dereferencing operator.
Definition: OwnPtr.h:138
OwnPtr(Object *)
Constructor.
Definition: OwnPtr.h:100