objectRegistryFunctionObjectTemplates.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-2024 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 
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>
55 const ObjectType&
57 (
58  const word& fieldName
59 ) const
60 {
61  return obr_.lookupObject<ObjectType>(fieldName);
62 }
63 
64 
65 template<class ObjectType>
66 ObjectType&
68 (
69  const word& fieldName
70 )
71 {
72  return obr_.lookupObjectRef<ObjectType>(fieldName);
73 }
74 
75 
76 template<class ObjectType>
78 (
79  const tmp<ObjectType>& tfield
80 )
81 {
82  if (obr_.foundObject<ObjectType>(tfield->name()))
83  {
84  ObjectType& field = obr_.lookupObjectRef<ObjectType>(tfield->name());
85 
86  // If there is a result field already registered assign to the new
87  // result field otherwise transfer ownership of the new result field to
88  // the object registry
89  if (&field != &tfield())
90  {
91  field = tfield;
92  return field;
93  }
94  else
95  {
96  ObjectType* fieldPtr = tfield.ptr();
97  obr_.objectRegistry::store(fieldPtr);
98  return *fieldPtr;
99  }
100  }
101  else
102  {
103  ObjectType* fieldPtr = tfield.ptr();
104  obr_.objectRegistry::store(fieldPtr);
105  return *fieldPtr;
106  }
107 }
108 
109 
110 template<class ObjectType>
112 (
113  const word& fieldName,
114  const tmp<ObjectType>& tfield,
115  bool cacheable
116 )
117 {
118  if (cacheable && fieldName == tfield().name())
119  {
121  << "Cannot store cache-able field with the name used in the cache."
122  << nl
123  << " Either choose a different name or cache the field"
124  << " and use the 'writeObjects' functionObject."
125  << endl;
126 
127  return tfield.ref();
128  }
129 
130  if
131  (
132  fieldName.size()
133  && obr_.foundObject<ObjectType>(fieldName)
134  )
135  {
136  ObjectType& field = obr_.lookupObjectRef<ObjectType>(fieldName);
137 
138  // If there is a result field already registered assign to the new
139  // result field otherwise transfer ownership of the new result field to
140  // the object registry
141  if (&field != &tfield())
142  {
143  field = tfield;
144  }
145  else
146  {
147  obr_.objectRegistry::store(tfield.ptr());
148  }
149 
150  return field;
151  }
152  else
153  {
154  if (fieldName.size() && fieldName != tfield().name())
155  {
156  tfield.ref().rename(fieldName);
157  }
158 
159  ObjectType* fieldPtr = tfield.ptr();
160  obr_.objectRegistry::store(fieldPtr);
161  return *fieldPtr;
162  }
163 }
164 
165 
166 // ************************************************************************* //
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 objectRegistry.
ObjectType & lookupObjectRef(const word &fieldName)
Lookup non-const object reference from the objectRegistry.
ObjectType & 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:221
T & ref() const
Return non-const reference or generate a fatal error.
Definition: tmpI.H:197
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:258
word name(const LagrangianState state)
Return a string representation of a Lagrangian state enumeration.
static const char nl
Definition: Ostream.H:267
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