Loading...
Searching...
No Matches
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-------------------------------------------------------------------------------
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 * * * * * * * * * * * * * //
32{
33 return registered_;
34}
35
38{
39 return ownedByRegistry_;
40}
41
42
43inline 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
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>
84inline Type& Foam::regIOobject::store(std::unique_ptr<Type>&& ptr)
86 // Pass management to objectRegistry
87 return store(ptr.release());
88}
89
90
91template<class Type>
94 // Pass management to objectRegistry
95 return store(ptr.release());
96}
97
98
99template<class Type>
100inline Type& Foam::regIOobject::store(autoPtr<Type>&& ptr)
102 // Pass management to objectRegistry
103 return store(ptr.release());
104}
105
106
107template<class Type>
108inline 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";
135
136 return *p;
137}
138
139
140template<class Type>
141inline Type& Foam::regIOobject::store(refPtr<Type>&& ptr)
143 // Forward as named reference, which also does a move
144 return store(ptr);
145}
146
147
148template<class Type>
149inline 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";
178
179 return *p;
180}
181
182
183template<class Type>
185{
186 // Forward as named reference, which also does a move
187 return store(ptr);
188}
189
190
191inline 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
202inline Foam::label Foam::regIOobject::eventNo() const noexcept
203{
204 return eventNo_;
205}
207inline 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// ************************************************************************* //
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition autoPtr.H:65
T * release() noexcept
Release ownership and return the pointer.
Definition autoPtrI.H:28
A class for managing references or pointers (no reference counting).
Definition refPtr.H:54
bool is_pointer() const noexcept
True if this is a managed pointer (not a reference).
Definition refPtrI.H:193
const T & cref() const
Return const reference to the object or to the contents of a (non-null) managed pointer.
Definition refPtrI.H:216
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
bool registered() const noexcept
Query the registered state (ie, has been checked in). This is not necessarily the same as registerObj...
const labelList & watchIndices() const noexcept
Read access to file-monitoring handles.
bool ownedByRegistry() const noexcept
Is this object owned by the registry?
bool store()
Register object with its registry and transfer ownership to the registry.
label eventNo() const noexcept
Event number at last update.
void release(const bool unregister=false) noexcept
Set object as not ownedByRegistry.
bool checkIn()
Add object to registry, if not already registered.
A class for managing temporary objects.
Definition tmp.H:75
bool is_pointer() const noexcept
True if this is a managed pointer (not a reference).
Definition tmpI.H:198
void protect(bool on) noexcept
Use specified protection against moving for the managed pointer. No-op for references.
Definition tmpI.H:307
const T & cref() const
Return const reference to the object or to the contents of a (non-null) managed pointer.
Definition tmpI.H:221
T * get() noexcept
Return pointer without nullptr checking.
Definition tmp.H:277
T * ptr() const
Return managed pointer for reuse, or clone() the object reference.
Definition tmpI.H:256
volScalarField & p
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition error.H:600
auto & name
#define WarningInFunction
Report a warning using Foam::Warning.
List< label > labelList
A List of labels.
Definition List.H:62
errorManip< error > abort(error &err)
Definition errorManip.H:139
const direction noexcept
Definition scalarImpl.H:265
error FatalError
Error stream (stdout output on all processes), with additional 'FOAM FATAL ERROR' header text and sta...
constexpr char nl
The newline '\n' character (0x0a).
Definition Ostream.H:50