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-2020 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(aptr.release());
82 }
83 
84 
85 template<class Type>
87 {
88  // Pass management to objectRegistry
89  return store(aptr.release());
90 }
91 
92 
93 template<class Type>
95 {
96  Type* p = nullptr;
97 
98  if (tptr.isTmp())
99  {
100  // Pass management to objectRegistry
101  p = tptr.ptr();
102 
103  store(p);
104 
105  // Adjust tmp<> access to use the stored reference
106  tptr.cref(*p);
107  }
108  else
109  {
110  // Taking ownership of a const-ref 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 = tptr.get();
116 
118  << "Refuse to store tmp to const-ref: " << p->name()
119  << ". Likely indicates a coding error\n";
120  }
121 
122  return *p;
123 }
124 
125 
126 template<class Type>
128 {
129  // Treat like a normal reference
130  return store(tptr);
131 }
132 
133 
134 inline void Foam::regIOobject::release(const bool unregister)
135 {
136  ownedByRegistry_ = false;
137  if (unregister)
138  {
139  registered_ = false;
140  }
141 }
142 
143 
144 inline Foam::label Foam::regIOobject::eventNo() const
145 {
146  return eventNo_;
147 }
148 
149 inline Foam::label& Foam::regIOobject::eventNo()
150 {
151  return eventNo_;
152 }
153 
154 
156 {
157  return watchIndices_;
158 }
159 
160 
162 {
163  return watchIndices_;
164 }
165 
166 
167 // ************************************************************************* //
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:134
Foam::tmp
A class for managing temporary objects.
Definition: PtrList.H:59
Foam::regIOobject::watchIndices
const labelList & watchIndices() const
Return file-monitoring handles.
Definition: regIOobjectI.H:155
Foam::tmp::isTmp
bool isTmp() const noexcept
True if this is a managed pointer (not a const reference)
Definition: tmpI.H:192
Foam::regIOobject::eventNo
label eventNo() const
Event number at last update.
Definition: regIOobjectI.H:144
Foam::regIOobject::store
bool store()
Definition: regIOobjectI.H:37
Foam::tmp::get
T * get() noexcept
Return pointer without nullptr checking.
Definition: tmpI.H:227
Foam::name
word name(const complex &c)
Return string representation of complex.
Definition: complex.C:76
Foam::autoPtr::release
T * release() noexcept
Return pointer to the managed object and release ownership.
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
Definition: tmpI.H:296
Foam::abort
errorManip< error > abort(error &err)
Definition: errorManip.H:137
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:372
Foam::nl
constexpr char nl
Definition: Ostream.H:385
Foam::List< label >
WarningInFunction
#define WarningInFunction
Report a warning using Foam::Warning.
Definition: messageStream.H:298
Foam::tmp::cref
const T & cref() const
Definition: tmpI.H:241