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-------------------------------------------------------------------------------
11License
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
31inline bool Foam::regIOobject::registered() const noexcept
32{
33 return registered_;
34}
35
36
38{
39 return ownedByRegistry_;
40}
41
42
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
59template<class Type>
60inline 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
83template<class Type>
85{
86 // Pass management to objectRegistry
87 return store(ptr.release());
88}
89
90
91template<class Type>
93{
94 // Pass management to objectRegistry
95 return store(ptr.release());
96}
97
98
99template<class Type>
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
132template<class Type>
134{
135 // Forward as named reference, which also does a move
136 return store(ptr);
137}
138
139
140template<class Type>
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
173template<class Type>
175{
176 // Forward as named reference, which also does a move
177 return store(ptr);
178}
179
180
181inline void Foam::regIOobject::release(const bool unregister) noexcept
182{
183 ownedByRegistry_ = false;
184 if (unregister)
185 {
186 registered_ = false;
187 }
188}
189
190
191inline Foam::label Foam::regIOobject::eventNo() const noexcept
192{
193 return eventNo_;
194}
195
197{
198 return eventNo_;
199}
200
201
203{
204 return watchIndices_;
205}
206
207
209{
210 return watchIndices_;
211}
212
213
214// ************************************************************************* //
const word & name() const noexcept
Return the object name.
Definition: IOobjectI.H:65
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition: autoPtr.H:66
T * release() noexcept
Release ownership and return the pointer.
Definition: autoPtrI.H:100
std::istream * release() noexcept
Return managed pointer and release ownership.
A class for managing references or pointers (no reference counting)
Definition: refPtr.H:58
bool is_pointer() const noexcept
True if this is a managed pointer (not a reference)
Definition: refPtr.H:160
const T & cref() const
Definition: refPtrI.H:189
T * get() noexcept
Return pointer without nullptr checking.
Definition: refPtr.H:169
T * release() noexcept
Definition: refPtrI.H:246
bool registered() const noexcept
Definition: regIOobjectI.H:31
const labelList & watchIndices() const noexcept
Read access to file-monitoring handles.
Definition: regIOobjectI.H:202
bool ownedByRegistry() const noexcept
Is this object owned by the registry?
Definition: regIOobjectI.H:37
label eventNo() const noexcept
Event number at last update.
Definition: regIOobjectI.H:191
A class for managing temporary objects.
Definition: tmp.H:65
bool is_pointer() const noexcept
True if this is a managed pointer (not a reference)
Definition: tmp.H:175
const T & cref() const
Definition: tmpI.H:213
T * get() noexcept
Return pointer without nullptr checking.
Definition: tmp.H:184
T * ptr() const
Return managed pointer for reuse, or clone() the object reference.
Definition: tmpI.H:255
volScalarField & p
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
#define WarningInFunction
Report a warning using Foam::Warning.
errorManip< error > abort(error &err)
Definition: errorManip.H:144
const direction noexcept
Definition: Scalar.H:223
error FatalError
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59
constexpr char nl
The newline '\n' character (0x0a)
Definition: Ostream.H:53