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-2019 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 ObjectType>
43 (
44  const word& fieldName
45 ) const
46 {
47  return obr_.lookupObject<ObjectType>(fieldName);
48 }
49 
50 
51 template<class ObjectType>
53 (
54  const word& fieldName
55 )
56 {
57  return obr_.lookupObjectRef<ObjectType>(fieldName);
58 }
59 
60 
61 template<class ObjectType>
63 (
64  const tmp<ObjectType>& tfield
65 )
66 {
67  if (obr_.foundObject<ObjectType>(tfield->name()))
68  {
69  ObjectType& field = obr_.lookupObjectRef<ObjectType>(tfield->name());
70 
71  // If there is a result field already registered assign to the new
72  // result field otherwise transfer ownership of the new result field to
73  // the object registry
74  if (&field != &tfield())
75  {
76  field = tfield;
77  }
78  else
79  {
80  obr_.objectRegistry::store(tfield.ptr());
81  }
82  }
83  else
84  {
85  obr_.objectRegistry::store(tfield.ptr());
86  }
87 
88  return true;
89 }
90 
91 
92 template<class ObjectType>
94 (
95  const word& fieldName,
96  const tmp<ObjectType>& tfield,
97  bool cacheable
98 )
99 {
100  if (cacheable && fieldName == tfield().name())
101  {
103  << "Cannot store cache-able field with the name used in the cache."
104  << nl
105  << " Either choose a different name or cache the field"
106  << " and use the 'writeObjects' functionObject."
107  << endl;
108 
109  return false;
110  }
111 
112  if
113  (
114  fieldName.size()
115  && obr_.foundObject<ObjectType>(fieldName)
116  )
117  {
118  ObjectType& field = obr_.lookupObjectRef<ObjectType>(fieldName);
119 
120  // If there is a result field already registered assign to the new
121  // result field otherwise transfer ownership of the new result field to
122  // the object registry
123  if (&field != &tfield())
124  {
125  field = tfield;
126  }
127  else
128  {
129  obr_.objectRegistry::store(tfield.ptr());
130  }
131  }
132  else
133  {
134  if (fieldName.size() && fieldName != tfield().name())
135  {
136  tfield.ref().rename(fieldName);
137  }
138 
139  obr_.objectRegistry::store(tfield.ptr());
140  }
141 
142  return true;
143 }
144 
145 
146 // ************************************************************************* //
T & ref() const
Return non-const reference or generate a fatal error.
Definition: tmpI.H:174
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
const ObjectType & lookupObject(const word &fieldName) const
Lookup object from the objectRegistry.
A class for handling words, derived from string.
Definition: word.H:59
bool foundObject(const word &fieldName) const
Find field in the objectRegistry.
static const char nl
Definition: Ostream.H:260
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
bool store(const tmp< ObjectType > &tfield)
Store the given field in the objectRegistry.
#define WarningInFunction
Report a warning using Foam::Warning.
ObjectType & lookupObjectRef(const word &fieldName)
Lookup non-const object reference from the objectRegistry.
T * ptr() const
Return tmp pointer for reuse.
Definition: tmpI.H:198
rDeltaTY field()
A class for managing temporary objects.
Definition: PtrList.H:53