Loading...
Searching...
No Matches
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-2023 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\*---------------------------------------------------------------------------*/
29#include "error.H"
30#include <typeinfo>
31
32// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
33
34template<class T>
36{
37 T* p = ptr_;
38 ptr_ = nullptr;
39 return p;
40}
41
42
43template<class T>
44inline void Foam::autoPtr<T>::reset(T* p) noexcept
46 delete ptr_;
47 ptr_ = p;
48}
49
50
51template<class T>
52inline void Foam::autoPtr<T>::reset(autoPtr<T>&& other) noexcept
53{
54 // Could also make Fatal with FULLDEBUG
55 if (&other == this)
56 {
57 return; // No self-assignment
58 }
60 reset(other.release());
61}
62
63
64template<class T>
65template<class... Args>
66inline T& Foam::autoPtr<T>::emplace(Args&&... args)
67{
68 delete ptr_; // delete old entry
69 ptr_ = new T(std::forward<Args>(args)...);
70 return *ptr_;
71}
72
73
74template<class T>
75inline void Foam::autoPtr<T>::swap(autoPtr<T>& other) noexcept
76{
77 // Swap is just copy/assign for pointer and enum types
78 // Self-swap is effectively ignored
79 T* p = ptr_;
80 ptr_ = other.ptr_;
81 other.ptr_ = p;
82}
83
84
85template<class T>
86template<class... Args>
87inline Foam::autoPtr<T> Foam::autoPtr<T>::clone(Args&&... args) const
88{
89 if (ptr_)
90 {
91 return autoPtr<T>(ptr_->clone(std::forward<Args>(args)...).ptr());
92 }
93
94 return autoPtr<T>();
95}
96
97
98// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
99
100template<class T>
102{
103 if (!ptr_)
104 {
106 << "unallocated autoPtr of type " << typeid(T).name()
108 }
109 return *ptr_;
110}
111
112
113template<class T>
114inline const T& Foam::autoPtr<T>::operator*() const
115{
116 return const_cast<autoPtr<T>*>(this)->operator*();
117}
118
119
120template<class T>
122{
123 if (!ptr_)
124 {
126 << "unallocated autoPtr of type " << typeid(T).name()
128 }
129 return ptr_;
130}
131
132
133template<class T>
134inline const T* Foam::autoPtr<T>::operator->() const
135{
136 return const_cast<autoPtr<T>*>(this)->operator->();
137}
138
139
140template<class T>
142{
143 return operator*();
144}
145
146
147template<class T>
148inline const T& Foam::autoPtr<T>::operator()() const
149{
150 return operator*();
151}
152
153
154// ************************************************************************* //
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition autoPtr.H:65
T & emplace(Args &&... args)
Reset with emplace construction. Return reference to the new content.
Definition autoPtrI.H:59
void swap(autoPtr< T > &other) noexcept
Swaps the managed object with other autoPtr.
Definition autoPtrI.H:68
T & operator*()
Return reference to the managed object.
Definition autoPtrI.H:94
T * operator->()
Dereferences (non-const) pointer to the managed object.
Definition autoPtrI.H:114
T & operator()()
Return reference to the object data.
Definition autoPtrI.H:134
T * release() noexcept
Release ownership and return the pointer.
Definition autoPtrI.H:28
constexpr autoPtr() noexcept
Construct with no managed pointer.
Definition autoPtr.H:92
void reset(T *p=nullptr) noexcept
Delete managed object and set to new given pointer.
Definition autoPtrI.H:37
autoPtr< T > clone(Args &&... args) const
Copy construct by invoking clone on underlying managed object.
Function1 * ptr() noexcept
Definition autoPtr.H:248
volScalarField & p
const volScalarField & T
limits reset(1/(limits.max()+VSMALL), 1/(limits.min()+VSMALL))
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition error.H:600
auto & name
tmp< faMatrix< Type > > operator*(const areaScalarField::Internal &, const faMatrix< Type > &)
errorManip< error > abort(error &err)
Definition errorManip.H:139
const direction noexcept
Definition scalarImpl.H:265
error FatalError
Error stream (stdout output on all processes), with additional 'FOAM FATAL ERROR' header text and sta...
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
Foam::argList args(argc, argv)