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 | Copyright (C) 2016 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  word& fieldName,
55  const tmp<ObjectType>& tfield,
56  bool cacheable
57 )
58 {
59  if (cacheable && fieldName == tfield().name())
60  {
62  << "Cannot store cache-able field with the named used in the cache."
63  << nl
64  << " Either choose a different name or cache the field"
65  << " and use the 'writeObjects' functionObject."
66  << endl;
67 
68  return false;
69  }
70 
71  if
72  (
73  fieldName.size()
74  && obr_.foundObject<ObjectType>(fieldName)
75  )
76  {
77  const ObjectType& field =
78  (
79  obr_.lookupObject<ObjectType>(fieldName)
80  );
81 
82  // If there is a result field already registered assign to the new
83  // result field otherwise transfer ownership of the new result field to
84  // the object registry
85  if (&field != &tfield())
86  {
87  const_cast<ObjectType&>(field) = tfield;
88  }
89  else
90  {
91  obr_.objectRegistry::store(tfield.ptr());
92  }
93  }
94  else
95  {
96  if (fieldName.size() && fieldName != tfield().name())
97  {
98  tfield.ref().rename(fieldName);
99  }
100  else
101  {
102  fieldName = tfield().name();
103  }
104 
105  obr_.objectRegistry::store(tfield.ptr());
106  }
107 
108  return true;
109 }
110 
111 
112 // ************************************************************************* //
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:253
bool foundObject(const word &fieldName) const
Find field in the objectRegistry.
A class for handling words, derived from string.
Definition: word.H:59
bool store(word &fieldName, const tmp< ObjectType > &tfield, bool cacheable=false)
Store the given field in the objectRegistry under the given name.
T * ptr() const
Return tmp pointer for reuse.
Definition: tmpI.H:198
static const char nl
Definition: Ostream.H:262
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
#define WarningInFunction
Report a warning using Foam::Warning.
A class for managing temporary objects.
Definition: PtrList.H:54
T & ref() const
Return non-const reference or generate a fatal error.
Definition: tmpI.H:174
const ObjectType & lookupObject(const word &fieldName) const
Lookup field from the objectRegistry.