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