DemandDrivenMeshObject.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) 2011-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 
26 #include "DemandDrivenMeshObject.H"
27 #include "meshObjects.H"
28 
29 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
30 
31 template<class Mesh, template<class> class MeshObjectType, class Type>
33 (
34  const IOobject& io,
35  const Mesh& mesh
36 )
37 :
38  regIOobject(io),
39  MeshObjectType<Mesh>(*this),
40  mesh_(mesh)
41 {}
42 
43 
44 template<class Mesh, template<class> class MeshObjectType, class Type>
46 (
47  const word& name,
48  const Mesh& mesh
49 )
50 :
52  (
53  IOobject
54  (
55  name,
56  mesh.thisDb().instance(),
57  mesh.thisDb()
58  )
59  ),
60  MeshObjectType<Mesh>(*this),
61  mesh_(mesh)
62 {}
63 
64 
65 template<class Mesh, template<class> class MeshObjectType, class Type>
67 (
68  const Mesh& mesh
69 )
70 :
71  DemandDrivenMeshObject<Mesh, MeshObjectType, Type>(Type::typeName, mesh)
72 {}
73 
74 
75 // * * * * * * * * * * * * * * * * Selectors * * * * * * * * * * * * * * * * //
76 
77 template<class Mesh, template<class> class MeshObjectType, class Type>
79 (
80  const word& name,
81  const Mesh& mesh
82 )
83 {
84  if (found(name, mesh))
85  {
86  return mesh.thisDb().objectRegistry::template lookupObjectRef<Type>
87  (
88  name
89  );
90  }
91  else
92  {
93  if (meshObjects::debug)
94  {
95  Pout<< "DemandDrivenMeshObject::New(" << Mesh::typeName
96  << "&) : constructing " << name
97  << " of type " << Type::typeName
98  << " for region " << mesh.name() << endl;
99  }
100 
101  Type* objectPtr = new Type(name, mesh);
102 
103  return regIOobject::store(objectPtr);
104  }
105 }
106 
107 
108 template<class Mesh, template<class> class MeshObjectType, class Type>
110 (
111  const Mesh& mesh
112 )
113 {
114  if (found(mesh))
115  {
116  return mesh.thisDb().objectRegistry::template lookupObjectRef<Type>
117  (
118  Type::typeName
119  );
120  }
121  else
122  {
123  if (meshObjects::debug)
124  {
125  Pout<< "DemandDrivenMeshObject::New(" << Mesh::typeName
126  << "&) : constructing " << Type::typeName
127  << " for region " << mesh.name() << endl;
128  }
129 
130  Type* objectPtr = new Type(mesh);
131 
132  return regIOobject::store(objectPtr);
133  }
134 }
135 
136 
137 template<class Mesh, template<class> class MeshObjectType, class Type>
138 template<class... Args>
140 (
141  const word& name,
142  const Mesh& mesh,
143  const Args&... args
144 )
145 {
146  if (found(name, mesh))
147  {
148  return mesh.thisDb().objectRegistry::template lookupObjectRef<Type>
149  (
150  name
151  );
152  }
153  else
154  {
155  if (meshObjects::debug)
156  {
157  Pout<< "DemandDrivenMeshObject::New(" << Mesh::typeName
158  << "&, const Data1&) : constructing " << name
159  << " of type " << Type::typeName
160  << " for region " << mesh.name() << endl;
161  }
162 
163  Type* objectPtr = new Type(name, mesh, args...);
164 
165  return regIOobject::store(objectPtr);
166  }
167 }
168 
169 
170 template<class Mesh, template<class> class MeshObjectType, class Type>
171 template<class... Args>
173 (
174  const Mesh& mesh,
175  const Args&... args
176 )
177 {
178  if (found(mesh))
179  {
180  return mesh.thisDb().objectRegistry::template lookupObjectRef<Type>
181  (
182  Type::typeName
183  );
184  }
185  else
186  {
187  if (meshObjects::debug)
188  {
189  Pout<< "DemandDrivenMeshObject::New(" << Mesh::typeName
190  << "&, const Data1&) : constructing " << Type::typeName
191  << " for region " << mesh.name() << endl;
192  }
193 
194  Type* objectPtr = new Type(mesh, args...);
195 
196  return regIOobject::store(objectPtr);
197  }
198 }
199 
200 
201 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * //
202 
203 template<class Mesh, template<class> class MeshObjectType, class Type>
206 {
208 }
209 
210 
211 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
212 
213 template<class Mesh, template<class> class MeshObjectType, class Type>
214 const Foam::word&
216 {
217  return Type::typeName;
218 }
219 
220 
221 template<class Mesh, template<class> class MeshObjectType, class Type>
223 (
224  const word& name,
225  const Mesh& mesh
226 )
227 {
228  return mesh.thisDb().objectRegistry::template foundObject<Type>(name);
229 }
230 
231 
232 template<class Mesh, template<class> class MeshObjectType, class Type>
234 (
235  const Mesh& mesh
236 )
237 {
238  return found(Type::typeName, mesh);
239 }
240 
241 
242 // ************************************************************************* //
bool found
Templated abstract base-class for demand-driven mesh objects used to automate their allocation to the...
DemandDrivenMeshObject(const IOobject &io, const Mesh &mesh)
Construct from mesh and IOobject.
static bool found(const word &name, const Mesh &mesh)
Return true if the DemandDrivenMeshObject with the given name.
static Type & New(const word &name, const Mesh &mesh)
Construct and return the named DemandDrivenMeshObject.
virtual const word & type() const
Runtime type information.
IOobject defines the attributes of an object for which implicit objectRegistry management is supporte...
Definition: IOobject.H:99
virtual const fileName & name() const
Return the name of the stream.
Definition: OSstream.H:85
regIOobject is an abstract class derived from IOobject to handle automatic object registration with t...
Definition: regIOobject.H:55
void release()
Release ownership of this object from its registry.
Definition: regIOobjectI.H:83
void store()
Transfer ownership of this object to its registry.
Definition: regIOobjectI.H:40
A class for handling words, derived from string.
Definition: word.H:62
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
prefixOSstream Pout(cout, "Pout")
Definition: IOstreams.H:53
Foam::argList args(argc, argv)