autoPtrI.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | Copyright (C) 2011-2013 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "error.H"
27 #include <typeinfo>
28 
29 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
30 
31 template<class T>
33 :
34  ptr_(p)
35 {}
36 
37 
38 template<class T>
40 :
41  ptr_(ap.ptr_)
42 {
43  ap.ptr_ = 0;
44 }
45 
46 
47 template<class T>
48 inline Foam::autoPtr<T>::autoPtr(const autoPtr<T>& ap, const bool reUse)
49 {
50  if (reUse)
51  {
52  ptr_ = ap.ptr_;
53  ap.ptr_ = 0;
54  }
55  else if (ap.valid())
56  {
57  ptr_ = ap().clone().ptr();
58  }
59  else
60  {
61  ptr_ = NULL;
62  }
63 }
64 
65 
66 template<class T>
68 {
69  clear();
70 }
71 
72 
73 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
74 
75 template<class T>
76 inline bool Foam::autoPtr<T>::empty() const
77 {
78  return !ptr_;
79 }
80 
81 
82 template<class T>
83 inline bool Foam::autoPtr<T>::valid() const
84 {
85  return ptr_;
86 }
87 
88 
89 template<class T>
91 {
92  T* ptr = ptr_;
93  ptr_ = 0;
94  return ptr;
95 }
96 
97 
98 template<class T>
99 inline void Foam::autoPtr<T>::set(T* p)
100 {
101  if (ptr_)
102  {
103  FatalErrorIn("void Foam::autoPtr<T>::set(T*)")
104  << "object of type " << typeid(T).name()
105  << " already allocated"
106  << abort(FatalError);
107  }
108 
109  ptr_ = p;
110 }
111 
112 
113 template<class T>
115 {
116  if (ptr_)
117  {
118  delete ptr_;
119  }
120 
121  ptr_ = p;
122 }
123 
124 
125 template<class T>
127 {
128  reset(0);
129 }
130 
131 
132 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
133 
134 template<class T>
136 {
137  if (!ptr_)
138  {
139  FatalErrorIn("T& Foam::autoPtr<T>::operator()()")
140  << "object of type " << typeid(T).name()
141  << " is not allocated"
142  << abort(FatalError);
143  }
144 
145  return *ptr_;
146 }
147 
148 
149 template<class T>
150 inline const T& Foam::autoPtr<T>::operator()() const
151 {
152  if (!ptr_)
153  {
154  FatalErrorIn("const T& Foam::autoPtr<T>::operator()() const")
155  << "object of type " << typeid(T).name()
156  << " is not allocated"
157  << abort(FatalError);
158  }
159 
160  return *ptr_;
161 }
162 
163 
164 template<class T>
165 inline Foam::autoPtr<T>::operator const T&() const
166 {
167  return operator()();
168 }
169 
170 
171 template<class T>
173 {
174  if (!ptr_)
175  {
176  FatalErrorIn("Foam::autoPtr<T>::operator->()")
177  << "object of type " << typeid(T).name()
178  << " is not allocated"
179  << abort(FatalError);
180  }
181 
182  return ptr_;
183 }
184 
185 
186 template<class T>
187 inline const T* Foam::autoPtr<T>::operator->() const
188 {
189  return const_cast<autoPtr<T>&>(*this).operator->();
190 }
191 
192 
193 template<class T>
195 {
196  if (this != &ap)
197  {
198  reset(const_cast<autoPtr<T>&>(ap).ptr());
199  }
200 }
201 
202 
203 // ************************************************************************* //
T * ptr()
Return object pointer for reuse.
Definition: autoPtrI.H:90
~autoPtr()
Destructor, delete object if pointer is not NULL.
Definition: autoPtrI.H:67
void clear()
Delete object (if the pointer is valid) and set pointer to NULL.
Definition: autoPtrI.H:126
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
void reset(T *=0)
If object pointer already set, delete object and set to given.
Definition: autoPtrI.H:114
bool empty() const
Return true if the autoPtr is empty (ie, no pointer set).
Definition: autoPtrI.H:76
bool valid() const
Return true if the autoPtr valid (ie, the pointer is set).
Definition: autoPtrI.H:83
autoPtr(T *=0)
Store object pointer.
Definition: autoPtrI.H:32
const volScalarField & T
Definition: createFields.H:25
volScalarField & p
Definition: createFields.H:51
T * operator->()
Return object pointer.
Definition: autoPtrI.H:172
errorManip< error > abort(error &err)
Definition: errorManip.H:131
void operator=(const autoPtr< T > &)
Take over the object pointer from parameter.
Definition: autoPtrI.H:194
#define FatalErrorIn(functionName)
Report an error message using Foam::FatalError.
Definition: error.H:314
error FatalError
void set(T *)
Set pointer to that given.
Definition: autoPtrI.H:99
An auto-pointer similar to the STL auto_ptr but with automatic casting to a reference to the type and...
Definition: PtrList.H:117
T & operator()()
Return reference to the object data.
Definition: autoPtrI.H:135