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 
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  }
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 // ************************************************************************* //
const Type & lookupObject(const word &name, const bool recursive=false) const
Lookup and return const reference to the object of the given Type. Fatal if not found or the wrong ty...
Type & lookupObjectRef(const word &name, const bool recursive=false) const
Lookup and return non-const reference to the object of the given Type. Fatal if not found or the wron...
const ObjectType * cfindObject(const word &fieldName) const
Return const pointer to the object (eg, a field) in the (sub) objectRegistry.
T & ref() const
Return non-const reference to the contents of a non-null managed pointer.
Definition: tmpI.H:235
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:50
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:531
const Type * findObject(const word &name, const bool recursive=false) const
Return const pointer to the object of the given Type.
const Type * cfindObject(const word &name, const bool recursive=false) const
Return const pointer to the object of the given Type.
const ObjectType & lookupObject(const word &fieldName) const
Lookup and return object (eg, a field) from the (sub) objectRegistry.
ObjectType & lookupObjectRef(const word &fieldName) const
Lookup and return object (eg, a field) from the (sub) objectRegistry.
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for expressions::valueTypeCode::INVALID.
Definition: exprTraits.C:127
A class for handling words, derived from Foam::string.
Definition: word.H:63
Type * getObjectPtr(const word &name, const bool recursive=false) const
Return non-const pointer to the object of the given Type, using a const-cast to have it behave like a...
ObjectType * getObjectPtr(const word &fieldName) const
Return non-const pointer to the object of the given Type, using a const-cast to have it behave like a...
bool store(word &fieldName, const tmp< ObjectType > &tfield, bool cacheable=false)
Store the field in the (sub) objectRegistry under the given name.
bool foundObject(const word &fieldName) const
Find object (eg, a field) in the (sub) objectRegistry.
const ObjectType * findObject(const word &fieldName) const
Return const pointer to the object (eg, a field) in the (sub) objectRegistry.
#define WarningInFunction
Report a warning using Foam::Warning.
bool foundObject(const word &name, const bool recursive=false) const
Is the named Type found?
T * ptr() const
Return managed pointer for reuse, or clone() the object reference.
Definition: tmpI.H:256
A class for managing temporary objects.
Definition: HashPtrTable.H:50
Registry of regIOobjects.
virtual const objectRegistry & obr() const
The region or sub-region registry being used.
bool storeInDb(const word &fieldName, const tmp< ObjectType > &tfield, const objectRegistry &obr)
Store the field in an optional objectRegistry under the given name.