regionFunctionObjectTemplates.C
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) 2016 OpenFOAM Foundation
9 Copyright (C) 2016-2021 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
30#include "objectRegistry.H"
31
32// * * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * //
33
34template<class ObjectType>
36(
37 const word& fieldName
38) const
39{
40 return obr().foundObject<ObjectType>(fieldName);
41}
42
43
44template<class ObjectType>
46(
47 const word& fieldName
48) const
49{
50 return obr().cfindObject<ObjectType>(fieldName);
51}
52
53
54template<class ObjectType>
56(
57 const word& fieldName
58) const
59{
60 return obr().findObject<ObjectType>(fieldName);
61}
62
63
64template<class ObjectType>
66(
67 const word& fieldName
68)
69{
70 // Need getObjectPtr to bypass const access on the objectRegistry
71 return obr().getObjectPtr<ObjectType>(fieldName);
72}
73
74
75template<class ObjectType>
77(
78 const word& fieldName
79) const
80{
81 return obr().getObjectPtr<ObjectType>(fieldName);
82}
83
84
85template<class ObjectType>
87(
88 const word& fieldName
89) const
90{
91 return obr().lookupObject<ObjectType>(fieldName);
92}
93
94
95template<class ObjectType>
97(
98 const word& fieldName
99) const
100{
101 return obr().lookupObjectRef<ObjectType>(fieldName);
102}
103
104
105template<class ObjectType>
107(
108 word& fieldName,
109 const tmp<ObjectType>& tfield,
110 bool cacheable
111)
112{
113 if (cacheable && fieldName == tfield().name())
114 {
116 << "Cannot store cache-able field with the name used in the cache."
117 << nl
118 << " Either choose a different name or cache the field"
119 << " and use the 'writeObjects' functionObject."
120 << endl;
121
122 return false;
123 }
124
125 ObjectType* fieldptr;
126
127 if
128 (
129 !fieldName.empty()
130 && (fieldptr = getObjectPtr<ObjectType>(fieldName)) != nullptr
131 )
132 {
133 // If there is a result field already registered, assign to the new
134 // result field. Otherwise transfer ownership of the new result field to
135 // the object registry
136 if (fieldptr != &tfield())
137 {
138 (*fieldptr) = tfield;
139 }
140 else
141 {
142 obr().objectRegistry::store(tfield.ptr());
143 }
144 }
145 else
146 {
147 if (fieldName.size() && fieldName != tfield().name())
148 {
149 tfield.ref().rename(fieldName);
150 }
151 else
152 {
153 fieldName = tfield().name();
154 }
155
156 obr().objectRegistry::store(tfield.ptr());
157 }
158
159 return true;
160}
161
162
163template<class ObjectType>
165(
166 const word& fieldName,
167 const tmp<ObjectType>& tfield,
168 const objectRegistry& obr
169)
170{
171 ObjectType* fieldptr;
172 if
173 (
174 !fieldName.empty()
175 && (fieldptr = obr.getObjectPtr<ObjectType>(fieldName)) != nullptr
176 )
177 {
178 (*fieldptr) = tfield;
179 }
180 else
181 {
182 tfield.ref().rename(fieldName);
183 obr.objectRegistry::store(tfield.ptr());
184 }
185
186 return true;
187}
188
189
190// ************************************************************************* //
const ObjectType & lookupObject(const word &fieldName) const
Lookup and return object (eg, a field) from the (sub) objectRegistry.
const ObjectType * cfindObject(const word &fieldName) const
bool foundObject(const word &fieldName) const
Find object (eg, a field) in the (sub) objectRegistry.
ObjectType * getObjectPtr(const word &fieldName) const
virtual const objectRegistry & obr() const
The region or sub-region registry being used.
const ObjectType * findObject(const word &fieldName) const
bool storeInDb(const word &fieldName, const tmp< ObjectType > &tfield, const objectRegistry &obr)
Store the field in an optional objectRegistry under the given name.
ObjectType & lookupObjectRef(const word &fieldName) const
Lookup and return object (eg, a field) from the (sub) objectRegistry.
Registry of regIOobjects.
bool foundObject(const word &name, const bool recursive=false) const
Is the named Type found?
Type * getObjectPtr(const word &name, const bool recursive=false) const
A class for managing temporary objects.
Definition: tmp.H:65
T * ptr() const
Return managed pointer for reuse, or clone() the object reference.
Definition: tmpI.H:255
T & ref() const
Definition: tmpI.H:227
A class for handling words, derived from Foam::string.
Definition: word.H:68
#define WarningInFunction
Report a warning using Foam::Warning.
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:372
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