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 -------------------------------------------------------------------------------
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 template<class... Args>
66 inline 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 
74 template<class T>
75 inline 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 
85 template<class T>
86 template<class... Args>
87 inline 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 
100 template<class T>
102 {
103  if (!ptr_)
104  {
106  << "unallocated autoPtr of type " << typeid(T).name()
108  }
109  return *ptr_;
110 }
111 
112 
113 template<class T>
114 inline const T& Foam::autoPtr<T>::operator*() const
115 {
116  return const_cast<autoPtr<T>*>(this)->operator*();
117 }
118 
119 
120 template<class T>
122 {
123  if (!ptr_)
124  {
126  << "unallocated autoPtr of type " << typeid(T).name()
128  }
129  return ptr_;
130 }
131 
132 
133 template<class T>
134 inline const T* Foam::autoPtr<T>::operator->() const
135 {
136  return const_cast<autoPtr<T>*>(this)->operator->();
137 }
138 
139 
140 template<class T>
142 {
143  return operator*();
144 }
145 
146 
147 template<class T>
148 inline const T& Foam::autoPtr<T>::operator()() const
149 {
150  return operator*();
151 }
152 
153 
154 // ************************************************************************* //
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
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:598
T & emplace(Args &&... args)
Reset with emplace construction. Return reference to the new content.
Definition: autoPtrI.H:59
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 expressions::valueTypeCode::INVALID.
Definition: exprTraits.C:127
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:114
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:134
Foam::argList args(argc, argv)