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 -------------------------------------------------------------------------------
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 #include "regionFunctionObject.H"
30 #include "objectRegistry.H"
31 
32 // * * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * //
33 
34 template<class ObjectType>
36 (
37  const word& fieldName
38 ) const
39 {
40  return obr().foundObject<ObjectType>(fieldName);
41 }
42 
43 
44 template<class ObjectType>
46 (
47  const word& fieldName
48 ) const
49 {
50  return obr().cfindObject<ObjectType>(fieldName);
51 }
52 
53 
54 template<class ObjectType>
56 (
57  const word& fieldName
58 ) const
59 {
60  return obr().findObject<ObjectType>(fieldName);
61 }
62 
63 
64 template<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 
75 template<class ObjectType>
77 (
78  const word& fieldName
79 ) const
80 {
81  return obr().getObjectPtr<ObjectType>(fieldName);
82 }
83 
84 
85 template<class ObjectType>
87 (
88  const word& fieldName
89 ) const
90 {
91  return obr().lookupObject<ObjectType>(fieldName);
92 }
93 
94 
95 template<class ObjectType>
97 (
98  const word& fieldName
99 ) const
100 {
101  return obr().lookupObjectRef<ObjectType>(fieldName);
102 }
103 
104 
105 template<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 
163 template<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 // ************************************************************************* //
Foam::objectRegistry::getObjectPtr
Type * getObjectPtr(const word &name, const bool recursive=false) const
Definition: objectRegistryTemplates.C:423
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:65
Foam::functionObjects::regionFunctionObject::lookupObject
const ObjectType & lookupObject(const word &fieldName) const
Lookup and return object (eg, a field) from the (sub) objectRegistry.
Definition: regionFunctionObjectTemplates.C:87
Foam::tmp
A class for managing temporary objects.
Definition: PtrList.H:61
Foam::functionObjects::regionFunctionObject::lookupObjectRef
ObjectType & lookupObjectRef(const word &fieldName) const
Lookup and return object (eg, a field) from the (sub) objectRegistry.
Definition: regionFunctionObjectTemplates.C:97
objectRegistry.H
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:369
Foam::functionObjects::regionFunctionObject::store
bool store(word &fieldName, const tmp< ObjectType > &tfield, bool cacheable=false)
Store the field in the (sub) objectRegistry under the given name.
Definition: regionFunctionObjectTemplates.C:107
Foam::objectRegistry::foundObject
bool foundObject(const word &name, const bool recursive=false) const
Is the named Type found?
Definition: objectRegistryTemplates.C:379
Foam::functionObjects::regionFunctionObject::cfindObject
const ObjectType * cfindObject(const word &fieldName) const
Definition: regionFunctionObjectTemplates.C:46
Foam::objectRegistry
Registry of regIOobjects.
Definition: objectRegistry.H:60
Foam::functionObjects::regionFunctionObject::foundObject
bool foundObject(const word &fieldName) const
Find object (eg, a field) in the (sub) objectRegistry.
Definition: regionFunctionObjectTemplates.C:36
Foam::tmp::ref
T & ref() const
Definition: tmpI.H:227
Foam::objectRegistry::lookupObject
const Type & lookupObject(const word &name, const bool recursive=false) const
Definition: objectRegistryTemplates.C:434
regionFunctionObject.H
Foam::functionObjects::regionFunctionObject::findObject
const ObjectType * findObject(const word &fieldName) const
Definition: regionFunctionObjectTemplates.C:56
Foam::tmp::ptr
T * ptr() const
Return managed pointer for reuse, or clone() the object reference.
Definition: tmpI.H:255
Foam::objectRegistry::lookupObjectRef
Type & lookupObjectRef(const word &name, const bool recursive=false) const
Definition: objectRegistryTemplates.C:478
Foam::functionObjects::regionFunctionObject::getObjectPtr
ObjectType * getObjectPtr(const word &fieldName) const
Definition: regionFunctionObjectTemplates.C:77
Foam::objectRegistry::findObject
const Type * findObject(const word &name, const bool recursive=false) const
Return const pointer to the object of the given Type.
Definition: objectRegistryTemplates.C:401
Foam::objectRegistry::cfindObject
const Type * cfindObject(const word &name, const bool recursive=false) const
Return const pointer to the object of the given Type.
Definition: objectRegistryTemplates.C:390
Foam::nl
constexpr char nl
Definition: Ostream.H:404
Foam::functionObjects::regionFunctionObject::storeInDb
bool storeInDb(const word &fieldName, const tmp< ObjectType > &tfield, const objectRegistry &obr)
Store the field in an optional objectRegistry under the given name.
Definition: regionFunctionObjectTemplates.C:165
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