regIOobjectI.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-2015 OpenFOAM Foundation
9  Copyright (C) 2018-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 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
30 
31 inline bool Foam::regIOobject::registered() const noexcept
32 {
33  return registered_;
34 }
35 
36 
38 {
39  return ownedByRegistry_;
40 }
41 
42 
43 inline bool Foam::regIOobject::store()
44 {
45  if (checkIn())
46  {
47  ownedByRegistry_ = true;
48  }
49  else
50  {
52  << "Refuse to store unregistered object: " << this->name() << nl;
53  }
54 
55  return ownedByRegistry_;
56 }
57 
58 
59 template<class Type>
60 inline Type& Foam::regIOobject::store(Type* p)
61 {
62  if (!p)
63  {
65  << "Object deallocated\n"
66  << abort(FatalError);
67  }
68 
69  const bool ok = p->regIOobject::store();
70 
71  if (!ok)
72  {
74  << "Failed to store pointer: " << p->regIOobject::name()
75  << ". Risk of memory leakage\n"
76  << abort(FatalError);
77  }
78 
79  return *p;
80 }
81 
82 
83 template<class Type>
84 inline Type& Foam::regIOobject::store(autoPtr<Type>& ptr)
85 {
86  // Pass management to objectRegistry
87  return store(ptr.release());
88 }
89 
90 
91 template<class Type>
92 inline Type& Foam::regIOobject::store(autoPtr<Type>&& ptr)
93 {
94  // Pass management to objectRegistry
95  return store(ptr.release());
96 }
97 
98 
99 template<class Type>
100 inline Type& Foam::regIOobject::store(refPtr<Type>& ptr)
101 {
102  Type* p = nullptr;
103 
104  if (ptr.is_pointer())
105  {
106  // Acquire ownership, pass management to objectRegistry
107  p = ptr.release();
108 
109  store(p);
110 
111  // Change parameter to access the stored reference
112  ptr.cref(p);
113  }
114  else
115  {
116  // Taking ownership of reference does not make much sense.
117  // - Storing the object won't actually do so, it will be removed
118  // when the original object goes out of scope.
119  // - Storing a clone may not be what we want.
120 
121  p = ptr.get();
122 
124  << "Refuse to store reference: " << p->name()
125  << ". Likely indicates a coding error\n";
126  }
127 
128  return *p;
129 }
130 
131 
132 template<class Type>
133 inline Type& Foam::regIOobject::store(refPtr<Type>&& ptr)
134 {
135  // Forward as named reference, which also does a move
136  return store(ptr);
137 }
138 
139 
140 template<class Type>
141 inline Type& Foam::regIOobject::store(tmp<Type>& ptr)
142 {
143  Type* p = nullptr;
144 
145  if (ptr.is_pointer())
146  {
147  // Acquire ownership, pass management to objectRegistry
148  p = ptr.ptr();
149 
150  store(p);
151 
152  // Change parameter to access the stored reference
153  ptr.cref(p);
154  }
155  else
156  {
157  // Taking ownership of reference does not make much sense.
158  // - Storing the object won't actually do so, it will be removed
159  // when the original object goes out of scope.
160  // - Storing a clone may not be what we want.
161 
162  p = ptr.get();
163 
165  << "Refuse to store reference: " << p->name()
166  << ". Likely indicates a coding error\n";
167  }
168 
169  return *p;
170 }
171 
172 
173 template<class Type>
175 {
176  // Forward as named reference, which also does a move
177  return store(ptr);
178 }
179 
180 
181 inline void Foam::regIOobject::release(const bool unregister) noexcept
182 {
183  ownedByRegistry_ = false;
184  if (unregister)
185  {
186  registered_ = false;
187  }
188 }
190 
191 inline Foam::label Foam::regIOobject::eventNo() const noexcept
192 {
193  return eventNo_;
194 }
196 inline Foam::label& Foam::regIOobject::eventNo() noexcept
197 {
198  return eventNo_;
199 }
200 
203 {
204  return watchIndices_;
205 }
206 
207 
209 {
210  return watchIndices_;
211 }
212 
213 
214 // ************************************************************************* //
void release(const bool unregister=false) noexcept
Set object as not ownedByRegistry.
Definition: regIOobjectI.H:174
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
const word & name() const noexcept
Return the object name.
Definition: IOobjectI.H:150
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:49
label eventNo() const noexcept
Event number at last update.
Definition: regIOobjectI.H:184
const T & cref() const
Return const reference to the object or to the contents of a (non-null) managed pointer.
Definition: tmpI.H:196
bool store()
Register object with its registry and transfer ownership to the registry.
Definition: regIOobjectI.H:36
const T & cref() const
Return const reference to the object or to the contents of a (non-null) managed pointer.
Definition: refPtrI.H:194
A class for managing references or pointers (no reference counting)
Definition: HashPtrTable.H:49
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:52
T * get() noexcept
Return pointer without nullptr checking.
Definition: tmp.H:265
errorManip< error > abort(error &err)
Definition: errorManip.H:139
const direction noexcept
Definition: Scalar.H:258
const labelList & watchIndices() const noexcept
Read access to file-monitoring handles.
Definition: regIOobjectI.H:195
T * get() noexcept
Return pointer without nullptr checking.
Definition: refPtr.H:248
T * release() noexcept
Release ownership and return the pointer. A no-op for reference objects (returns nullptr).
Definition: refPtrI.H:244
#define WarningInFunction
Report a warning using Foam::Warning.
bool ownedByRegistry() const noexcept
Is this object owned by the registry?
Definition: regIOobjectI.H:30
bool is_pointer() const noexcept
True if this is a managed pointer (not a reference)
Definition: tmp.H:247
Pointer management similar to std::unique_ptr, with some additional methods and type checking...
Definition: HashPtrTable.H:48
T * ptr() const
Return managed pointer for reuse, or clone() the object reference.
Definition: tmpI.H:231
volScalarField & p
A class for managing temporary objects.
Definition: HashPtrTable.H:50
bool registered() const noexcept
Query the registered state (ie, has been checked in). This is not necessarily the same as registerObj...
Definition: regIOobjectI.H:24
bool is_pointer() const noexcept
True if this is a managed pointer (not a reference)
Definition: refPtr.H:230