UautoPtrI.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2017-2018 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 
44 
45 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
46 
47 template<class T>
48 inline bool Foam::UautoPtr<T>::empty() const
49 {
50  return !ptr_;
51 }
52 
53 
54 template<class T>
55 inline bool Foam::UautoPtr<T>::valid() const
56 {
57  return ptr_;
58 }
59 
60 
61 template<class T>
63 {
64  return ptr_;
65 }
66 
67 
68 template<class T>
69 inline void Foam::UautoPtr<T>::set(T* p)
70 {
71  if (ptr_)
72  {
74  << "object of type " << typeid(T).name()
75  << " already allocated"
76  << abort(FatalError);
77  }
78 
79  ptr_ = p;
80 }
81 
82 
83 template<class T>
85 {
86  ptr_ = p;
87 }
88 
89 
90 template<class T>
92 {
93  reset(nullptr);
94 }
95 
96 
97 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
98 
99 template<class T>
101 {
102  if (!ptr_)
103  {
105  << "object of type " << typeid(T).name()
106  << " is not allocated"
107  << abort(FatalError);
108  }
109 
110  return *ptr_;
111 }
112 
113 
114 template<class T>
115 inline const T& Foam::UautoPtr<T>::operator()() const
116 {
117  if (!ptr_)
118  {
120  << "object of type " << typeid(T).name()
121  << " is not allocated"
122  << abort(FatalError);
123  }
124 
125  return *ptr_;
126 }
127 
128 
129 template<class T>
131 {
132  if (!ptr_)
133  {
135  << "object of type " << typeid(T).name()
136  << " is not allocated"
137  << abort(FatalError);
138  }
139 
140  return *ptr_;
141 }
142 
143 
144 template<class T>
145 inline const T& Foam::UautoPtr<T>::operator*() const
146 {
147  if (!ptr_)
148  {
150  << "object of type " << typeid(T).name()
151  << " is not allocated"
152  << abort(FatalError);
153  }
154 
155  return *ptr_;
156 }
157 
158 
159 template<class T>
160 inline Foam::UautoPtr<T>::operator const T&() const
161 {
162  return operator()();
163 }
164 
165 
166 template<class T>
168 {
169  if (!ptr_)
170  {
172  << "object of type " << typeid(T).name()
173  << " is not allocated"
174  << abort(FatalError);
175  }
176 
177  return ptr_;
178 }
179 
180 
181 template<class T>
182 inline const T* Foam::UautoPtr<T>::operator->() const
183 {
184  return const_cast<UautoPtr<T>&>(*this).operator->();
185 }
186 
187 
188 template<class T>
190 {
191  reset(p);
192 }
193 
194 
195 template<class T>
197 {
198  if (this != &ap)
199  {
200  reset(const_cast<UautoPtr<T>&>(ap).ptr());
201  }
202 }
203 
204 
205 // ************************************************************************* //
UautoPtr(T *=nullptr)
Store object pointer.
Definition: UautoPtrI.H:32
error FatalError
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:323
T * ptr()
Return object pointer for reuse.
Definition: UautoPtrI.H:62
bool empty() const
Return true if the UautoPtr is empty (ie, no pointer set)
Definition: UautoPtrI.H:48
void set(T *)
Set pointer to that given.
Definition: UautoPtrI.H:69
bool valid() const
Return true if the UautoPtr valid (ie, the pointer is set)
Definition: UautoPtrI.H:55
An auto-pointer similar to the STL auto_ptr but with automatic casting to a reference to the type and...
Definition: UautoPtr.H:50
T & operator*()
Return reference to the object data.
Definition: UautoPtrI.H:130
void operator=(T *)
Take over the object pointer from parameter.
Definition: UautoPtrI.H:189
void reset(T *=nullptr)
Set pointer to that given.
Definition: UautoPtrI.H:84
T & operator()()
Return reference to the object data.
Definition: UautoPtrI.H:100
errorManip< error > abort(error &err)
Definition: errorManip.H:131
T * operator->()
Return object pointer.
Definition: UautoPtrI.H:167
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
const volScalarField & T
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
volScalarField & p
void clear()
Set pointer to nullptr.
Definition: UautoPtrI.H:91