regionFunctionObjectTemplates.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2016-2020 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "regionFunctionObject.H"
27 #include "objectRegistry.H"
28 
29 // * * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * //
30 
31 template<class ObjectType>
33 (
34  const word& fieldName
35 ) const
36 {
37  return obr_.foundObject<ObjectType>(fieldName);
38 }
39 
40 
41 template<class Type>
43 (
44  const word& fieldName
45 )
46 {
47  Warning
48  << " functionObjects::" << type() << " " << name()
49  << " cannot find required object " << fieldName << " of type "
50  << Type::typeName << endl;
51 }
52 
53 
54 template<class ObjectType>
56 (
57  const word& fieldName
58 ) const
59 {
60  return obr_.lookupObject<ObjectType>(fieldName);
61 }
62 
63 
64 template<class ObjectType>
66 (
67  const word& fieldName
68 )
69 {
70  return obr_.lookupObjectRef<ObjectType>(fieldName);
71 }
72 
73 
74 template<class ObjectType>
76 (
77  const tmp<ObjectType>& tfield
78 )
79 {
80  if (obr_.foundObject<ObjectType>(tfield->name()))
81  {
82  ObjectType& field = obr_.lookupObjectRef<ObjectType>(tfield->name());
83 
84  // If there is a result field already registered assign to the new
85  // result field otherwise transfer ownership of the new result field to
86  // the object registry
87  if (&field != &tfield())
88  {
89  field = tfield;
90  }
91  else
92  {
93  obr_.objectRegistry::store(tfield.ptr());
94  }
95  }
96  else
97  {
98  obr_.objectRegistry::store(tfield.ptr());
99  }
100 
101  return true;
102 }
103 
104 
105 template<class ObjectType>
107 (
108  const 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  if
126  (
127  fieldName.size()
128  && obr_.foundObject<ObjectType>(fieldName)
129  )
130  {
131  ObjectType& field = obr_.lookupObjectRef<ObjectType>(fieldName);
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 (&field != &tfield())
137  {
138  field = 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 
152  obr_.objectRegistry::store(tfield.ptr());
153  }
154 
155  return true;
156 }
157 
158 
159 // ************************************************************************* //
const ObjectType & lookupObject(const word &fieldName) const
Lookup object from the objectRegistry.
bool foundObject(const word &fieldName) const
Find field in the objectRegistry.
const objectRegistry & obr_
Reference to the region objectRegistry.
ObjectType & lookupObjectRef(const word &fieldName)
Lookup non-const object reference from the objectRegistry.
bool store(const tmp< ObjectType > &tfield)
Store the given field in the objectRegistry.
void cannotFindObject(const word &fieldName)
Prints a warning message that fieldName cannot be found.
bool foundObject(const word &name) const
Is the named Type in registry.
A class for managing temporary objects.
Definition: tmp.H:55
T * ptr() const
Return tmp pointer for reuse.
Definition: tmpI.H:205
T & ref() const
Return non-const reference or generate a fatal error.
Definition: tmpI.H:181
A class for handling words, derived from string.
Definition: word.H:62
#define WarningInFunction
Report a warning using Foam::Warning.
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:257
word name(const bool)
Return a word representation of a bool.
Definition: boolIO.C:39
static const char nl
Definition: Ostream.H:266
fileType type(const fileName &, const bool checkVariants=true, const bool followLink=true)
Return the file type: directory or file.
Definition: POSIX.C:488
messageStream Warning