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 | www.openfoam.com
6 \\/ M anipulation |
7-------------------------------------------------------------------------------
8 Copyright (C) 2011-2017 OpenFOAM Foundation
9 Copyright (C) 2016-2020 OpenCFD Ltd.
10-------------------------------------------------------------------------------
11License
12 This file is part of OpenFOAM.
13
14 OpenFOAM is free software: you can redistribute it and/or modify it
15 under the terms of the GNU General Public License as published by
16 the Free Software Foundation, either version 3 of the License, or
17 (at your option) any later version.
18
19 OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26
27\*---------------------------------------------------------------------------*/
28
29#include "error.H"
30#include <typeinfo>
31
32// * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
33
34template<class T>
35template<class... Args>
37{
38 return autoPtr<T>(new T(std::forward<Args>(args)...));
39}
40
41
42template<class T>
43template<class U, class... Args>
45{
46 return autoPtr<T>(new U(std::forward<Args>(args)...));
47}
48
49
50// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
51
52template<class T>
53inline constexpr Foam::autoPtr<T>::autoPtr() noexcept
54:
55 ptr_(nullptr)
56{}
57
58
59template<class T>
60inline constexpr Foam::autoPtr<T>::autoPtr(std::nullptr_t) noexcept
61:
62 ptr_(nullptr)
63{}
64
65
66template<class T>
68:
69 ptr_(p)
70{}
71
72
73template<class T>
75:
76 ptr_(ap.release())
77{}
78
79
80template<class T>
81template<class U>
83:
84 ptr_(ap.release())
85{}
86
87
88// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
89
90template<class T>
92{
93 reset(nullptr);
94}
95
96
97// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
98
99template<class T>
101{
102 T* p = ptr_;
103 ptr_ = nullptr;
104 return p;
105}
107
108template<class T>
109inline void Foam::autoPtr<T>::reset(T* p) noexcept
110{
111 if (ptr_) delete ptr_;
112 ptr_ = p;
113}
114
116template<class T>
117inline void Foam::autoPtr<T>::reset(autoPtr<T>&& other) noexcept
118{
119 if (&other != this)
120 {
121 // Ignore self-assignment
122 reset(other.release());
123 }
124}
125
126
127template<class T>
128inline void Foam::autoPtr<T>::swap(autoPtr<T>& other) noexcept
129{
130 // Swap is just copy/assign for pointer and enum types
131 // Self-swap is effectively ignored
132 T* p = ptr_;
133 ptr_ = other.ptr_;
134 other.ptr_ = p;
135}
136
138template<class T>
139template<class... Args>
141{
142 if (ptr_)
143 {
144 return autoPtr<T>(ptr_->clone(std::forward<Args>(args)...).ptr());
145 }
146
147 return autoPtr<T>();
148}
149
150
151// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
152
153template<class T>
155{
156 if (!ptr_)
157 {
159 << "unallocated autoPtr of type " << typeid(T).name()
160 << abort(FatalError);
161 }
162 return *ptr_;
163}
164
165
166template<class T>
167inline const T& Foam::autoPtr<T>::operator*() const
169 return const_cast<autoPtr<T>*>(this)->operator*();
170}
171
172
173template<class T>
175{
176 if (!ptr_)
177 {
179 << "unallocated autoPtr of type " << typeid(T).name()
180 << abort(FatalError);
182 return ptr_;
183}
185
186template<class T>
187inline const T* Foam::autoPtr<T>::operator->() const
188{
189 return const_cast<autoPtr<T>*>(this)->operator->();
190}
191
192
193template<class T>
195{
196 return operator*();
197}
198
199
200template<class T>
201inline const T& Foam::autoPtr<T>::operator()() const
202{
203 return operator*();
204}
205
206
207template<class T>
208inline void Foam::autoPtr<T>::operator=(autoPtr<T>&& ap) noexcept
209{
210 if (this != &ap)
212 // Ignore self-assignment
213 reset(ap.release());
214 }
216
217
218template<class T>
219template<class U>
220inline void Foam::autoPtr<T>::operator=(autoPtr<U>&& ap) noexcept
221{
222 if (this != &ap)
224 // Ignore self-assignment
225 reset(ap.release());
226 }
227}
228
229
230template<class T>
231inline void Foam::autoPtr<T>::operator=(std::nullptr_t) noexcept
232{
233 reset(nullptr);
234}
235
236
237// ************************************************************************* //
static autoPtr< Time > New()
Construct (dummy) Time - no functionObjects or libraries.
Definition: Time.C:717
virtual tmp< fvPatchScalarField > clone() const
Construct and return a clone.
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition: autoPtr.H:66
void swap(autoPtr< T > &other) noexcept
Swaps the managed object with other autoPtr.
Definition: autoPtrI.H:128
T & operator*()
Return reference to the managed object.
Definition: autoPtrI.H:154
T * operator->()
Dereferences (non-const) pointer to the managed object.
Definition: autoPtrI.H:174
T & operator()()
Return reference to the object data.
Definition: autoPtrI.H:194
T * release() noexcept
Release ownership and return the pointer.
Definition: autoPtrI.H:100
void operator=(T *p)=delete
No copy assignment from plain pointer (uncontrolled access)
static autoPtr< T > NewFrom(Args &&... args)
Construct autoPtr from derived type with forwarding arguments.
constexpr autoPtr() noexcept
Construct with no managed pointer.
Definition: autoPtrI.H:53
~autoPtr() noexcept
Deletes the managed pointer.
Definition: autoPtrI.H:91
void reset()
Reset to defaults.
U
Definition: pEqn.H:72
volScalarField & p
const volScalarField & T
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
tmp< faMatrix< Type > > operator*(const areaScalarField::Internal &, const faMatrix< Type > &)
errorManip< error > abort(error &err)
Definition: errorManip.H:144
const direction noexcept
Definition: Scalar.H:223
error FatalError
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59
Foam::argList args(argc, argv)