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-2024 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(std::unique_ptr<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(autoPtr<Type>&& ptr)
101 {
102  // Pass management to objectRegistry
103  return store(ptr.release());
104 }
105 
106 
107 template<class Type>
108 inline Type& Foam::regIOobject::store(refPtr<Type>& ptr)
109 {
110  Type* p = nullptr;
111 
112  if (ptr.is_pointer())
113  {
114  // Acquire ownership, pass management to objectRegistry
115  p = ptr.release();
116 
117  store(p);
118 
119  // Change parameter to access the stored reference
120  ptr.cref(p);
121  }
122  else
123  {
124  // Taking ownership of reference does not make much sense.
125  // - Storing the object won't actually do so, it will be removed
126  // when the original object goes out of scope.
127  // - Storing a clone may not be what we want.
128 
129  p = ptr.get();
130 
132  << "Refuse to store reference: " << p->name()
133  << ". Likely indicates a coding error\n";
134  }
135 
136  return *p;
137 }
138 
139 
140 template<class Type>
141 inline Type& Foam::regIOobject::store(refPtr<Type>&& ptr)
142 {
143  // Forward as named reference, which also does a move
144  return store(ptr);
145 }
146 
147 
148 template<class Type>
149 inline Type& Foam::regIOobject::store(tmp<Type>& ptr)
150 {
151  Type* p = nullptr;
152 
153  if (ptr.is_pointer())
154  {
155  // Acquire ownership, pass management to objectRegistry
156 
157  ptr.protect(false); // Storing (ie, not cached/protected)
158  p = ptr.ptr();
159 
160  store(p);
161 
162  // Change parameter to access the stored reference
163  ptr.cref(p);
164  }
165  else
166  {
167  // Taking ownership of reference does not make much sense.
168  // - Storing the object won't actually do so, it will be removed
169  // when the original object goes out of scope.
170  // - Storing a clone may not be what we want.
171 
172  p = ptr.get();
173 
175  << "Refuse to store reference: " << p->name()
176  << ". Likely indicates a coding error\n";
177  }
178 
179  return *p;
180 }
181 
182 
183 template<class Type>
185 {
186  // Forward as named reference, which also does a move
187  return store(ptr);
188 }
189 
190 
191 inline void Foam::regIOobject::release(const bool unregister) noexcept
192 {
193  // Note: could also return the old ownedByRegistry_ value
194  ownedByRegistry_ = false;
195  if (unregister)
196  {
197  registered_ = false;
198  }
199 }
201 
202 inline Foam::label Foam::regIOobject::eventNo() const noexcept
203 {
204  return eventNo_;
205 }
207 inline Foam::label& Foam::regIOobject::eventNo() noexcept
208 {
209  return eventNo_;
210 }
211 
214 {
215  return watchIndices_;
216 }
217 
218 
220 {
221  return watchIndices_;
222 }
223 
224 
225 // ************************************************************************* //
void release(const bool unregister=false) noexcept
Set object as not ownedByRegistry.
Definition: regIOobjectI.H:184
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:608
const word & name() const noexcept
Return the object name.
Definition: IOobjectI.H:195
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:50
label eventNo() const noexcept
Event number at last update.
Definition: regIOobjectI.H:195
const T & cref() const
Return const reference to the object or to the contents of a (non-null) managed pointer.
Definition: tmpI.H:221
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:216
void protect(bool on) noexcept
Use specified protection against moving for the managed pointer. No-op for references.
Definition: tmpI.H:307
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 expressions::valueTypeCode::INVALID.
Definition: exprTraits.C:127
T * get() noexcept
Return pointer without nullptr checking.
Definition: tmp.H:268
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 labelList & watchIndices() const noexcept
Read access to file-monitoring handles.
Definition: regIOobjectI.H:206
T * get() noexcept
Return pointer without nullptr checking.
Definition: refPtr.H:249
T * release() noexcept
Release ownership and return the pointer. A no-op for reference objects (returns nullptr).
Definition: refPtrI.H:266
#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: tmpI.H:198
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:256
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: refPtrI.H:193