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-2022 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
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 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
33 
34 template<class T>
36 {
37  T* p = ptr_;
38  ptr_ = nullptr;
39  return p;
40 }
41 
42 
43 template<class T>
44 inline void Foam::autoPtr<T>::reset(T* p) noexcept
45 {
46  delete ptr_;
47  ptr_ = p;
48 }
49 
50 
51 template<class T>
52 inline 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  }
59 
60  reset(other.release());
61 }
62 
63 
64 template<class T>
65 inline void Foam::autoPtr<T>::swap(autoPtr<T>& other) noexcept
66 {
67  // Swap is just copy/assign for pointer and enum types
68  // Self-swap is effectively ignored
69  T* p = ptr_;
70  ptr_ = other.ptr_;
71  other.ptr_ = p;
72 }
73 
74 
75 template<class T>
76 template<class... Args>
77 inline Foam::autoPtr<T> Foam::autoPtr<T>::clone(Args&&... args) const
78 {
79  if (ptr_)
80  {
81  return autoPtr<T>(ptr_->clone(std::forward<Args>(args)...).ptr());
82  }
83 
84  return autoPtr<T>();
85 }
86 
87 
88 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
89 
90 template<class T>
92 {
93  if (!ptr_)
94  {
96  << "unallocated autoPtr of type " << typeid(T).name()
98  }
99  return *ptr_;
100 }
101 
102 
103 template<class T>
104 inline const T& Foam::autoPtr<T>::operator*() const
105 {
106  return const_cast<autoPtr<T>*>(this)->operator*();
107 }
108 
109 
110 template<class T>
112 {
113  if (!ptr_)
114  {
116  << "unallocated autoPtr of type " << typeid(T).name()
118  }
119  return ptr_;
120 }
121 
122 
123 template<class T>
124 inline const T* Foam::autoPtr<T>::operator->() const
125 {
126  return const_cast<autoPtr<T>*>(this)->operator->();
127 }
128 
129 
130 template<class T>
132 {
133  return operator*();
134 }
135 
136 
137 template<class T>
138 inline const T& Foam::autoPtr<T>::operator()() const
139 {
140  return operator*();
141 }
142 
143 
144 // ************************************************************************* //
void swap(autoPtr< T > &other) noexcept
Swaps the managed object with other autoPtr.
Definition: autoPtrI.H:58
T & operator*()
Return reference to the managed object.
Definition: autoPtrI.H:84
error FatalError
Error stream (stdout output on all processes), with additional &#39;FOAM FATAL ERROR&#39; header text and sta...
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:578
void reset(T *p=nullptr) noexcept
Delete managed object and set to new given pointer.
Definition: autoPtrI.H:37
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:52
tmp< faMatrix< Type > > operator*(const areaScalarField::Internal &, const faMatrix< Type > &)
errorManip< error > abort(error &err)
Definition: errorManip.H:139
T * release() noexcept
Release ownership and return the pointer.
Definition: autoPtrI.H:28
const direction noexcept
Definition: Scalar.H:258
const volScalarField & T
T * operator->()
Dereferences (non-const) pointer to the managed object.
Definition: autoPtrI.H:104
autoPtr< T > clone(Args &&... args) const
Copy construct by invoking clone on underlying managed object.
Pointer management similar to std::unique_ptr, with some additional methods and type checking...
Definition: HashPtrTable.H:48
volScalarField & p
T & operator()()
Return reference to the object data.
Definition: autoPtrI.H:124
Foam::argList args(argc, argv)