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-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 // * * * * * * * * * * * * * * * 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 
149  ptr.protect(false); // Storing (ie, not cached/protected)
150  p = ptr.ptr();
151 
152  store(p);
153 
154  // Change parameter to access the stored reference
155  ptr.cref(p);
156  }
157  else
158  {
159  // Taking ownership of reference does not make much sense.
160  // - Storing the object won't actually do so, it will be removed
161  // when the original object goes out of scope.
162  // - Storing a clone may not be what we want.
163 
164  p = ptr.get();
165 
167  << "Refuse to store reference: " << p->name()
168  << ". Likely indicates a coding error\n";
169  }
170 
171  return *p;
172 }
173 
174 
175 template<class Type>
177 {
178  // Forward as named reference, which also does a move
179  return store(ptr);
180 }
181 
182 
183 inline void Foam::regIOobject::release(const bool unregister) noexcept
184 {
185  ownedByRegistry_ = false;
186  if (unregister)
187  {
188  registered_ = false;
189  }
190 }
192 
193 inline Foam::label Foam::regIOobject::eventNo() const noexcept
194 {
195  return eventNo_;
196 }
198 inline Foam::label& Foam::regIOobject::eventNo() noexcept
199 {
200  return eventNo_;
201 }
202 
205 {
206  return watchIndices_;
207 }
208 
209 
211 {
212  return watchIndices_;
213 }
214 
215 
216 // ************************************************************************* //
void release(const bool unregister=false) noexcept
Set object as not ownedByRegistry.
Definition: regIOobjectI.H:176
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
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:186
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
const direction noexcept
Definition: Scalar.H:258
const labelList & watchIndices() const noexcept
Read access to file-monitoring handles.
Definition: regIOobjectI.H:197
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