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-2026 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
32 <
33  class Mesh,
34  template<class> class MeshObjectType,
35  class Type,
36  class RegIOobject
37 >
40 (
41  const IOobject& io,
42  const Mesh& mesh
43 )
44 :
45  RegIOobject(io),
46  MeshObjectType<Mesh>(*this),
47  mesh_(mesh)
48 {}
49 
50 
51 template
52 <
53  class Mesh,
54  template<class> class MeshObjectType,
55  class Type,
56  class RegIOobject
57 >
60 (
61  const word& name,
62  const Mesh& mesh
63 )
64 :
65  RegIOobject
66  (
67  IOobject
68  (
69  name,
70  mesh.db().instance(),
71  mesh.db()
72  )
73  ),
74  MeshObjectType<Mesh>(*this),
75  mesh_(mesh)
76 {}
77 
78 
79 template
80 <
81  class Mesh,
82  template<class> class MeshObjectType,
83  class Type,
84  class RegIOobject
85 >
88 (
89  const Mesh& mesh
90 )
91 :
92  DemandDrivenMeshObject<Mesh, MeshObjectType, Type, RegIOobject>
93  (
94  Type::typeName,
95  mesh
96  )
97 {}
98 
99 
100 // * * * * * * * * * * * * * * * * Selectors * * * * * * * * * * * * * * * * //
101 
102 template
103 <
104  class Mesh,
105  template<class> class MeshObjectType,
106  class Type,
107  class RegIOobject
108 >
110 (
111  const word& name,
112  const Mesh& mesh
113 )
114 {
115  if (found(name, mesh))
116  {
117  return mesh.db().objectRegistry::template lookupObjectRef<Type>
118  (
119  name
120  );
121  }
122  else
123  {
124  if (meshObjects::debug)
125  {
126  Pout<< "DemandDrivenMeshObject::New(" << Mesh::typeName
127  << "&) : constructing " << name
128  << " of type " << Type::typeName
129  << " for region " << mesh.name() << endl;
130  }
131 
132  Type* objectPtr = new Type(name, mesh);
133 
134  return regIOobject::store(objectPtr);
135  }
136 }
137 
138 
139 template
140 <
141  class Mesh,
142  template<class> class MeshObjectType,
143  class Type,
144  class RegIOobject
145 >
147 (
148  const Mesh& mesh
149 )
150 {
151  if (found(mesh))
152  {
153  return mesh.db().objectRegistry::template lookupObjectRef<Type>
154  (
156  );
157  }
158  else
159  {
160  if (meshObjects::debug)
161  {
162  Pout<< "DemandDrivenMeshObject::New(" << Mesh::typeName
163  << "&) : constructing " << Type::typeName
164  << " for region " << mesh.name() << endl;
165  }
166 
167  Type* objectPtr = new Type(mesh);
168 
169  return regIOobject::store(objectPtr);
170  }
171 }
172 
173 
174 template
175 <
176  class Mesh,
177  template<class> class MeshObjectType,
178  class Type,
179  class RegIOobject
180 >
181 template<class... Args>
183 (
184  const word& name,
185  const Mesh& mesh,
186  const Args&... args
187 )
188 {
189  if (found(name, mesh))
190  {
191  return mesh.db().objectRegistry::template lookupObjectRef<Type>
192  (
194  );
195  }
196  else
197  {
198  if (meshObjects::debug)
199  {
200  Pout<< "DemandDrivenMeshObject::New(" << Mesh::typeName
201  << "&, const Data1&) : constructing " << name
202  << " of type " << Type::typeName
203  << " for region " << mesh.name() << endl;
204  }
205 
206  Type* objectPtr = new Type(name, mesh, args...);
207 
208  return regIOobject::store(objectPtr);
209  }
210 }
211 
212 
213 template
214 <
215  class Mesh,
216  template<class> class MeshObjectType,
217  class Type,
218  class RegIOobject
219 >
220 template<class... Args>
222 (
223  const Mesh& mesh,
224  const Args&... args
225 )
226 {
227  if (found(mesh))
228  {
229  return mesh.db().objectRegistry::template lookupObjectRef<Type>
230  (
232  );
233  }
234  else
235  {
236  if (meshObjects::debug)
237  {
238  Pout<< "DemandDrivenMeshObject::New(" << Mesh::typeName
239  << "&, const Data1&) : constructing " << Type::typeName
240  << " for region " << mesh.name() << endl;
241  }
242 
243  Type* objectPtr = new Type(mesh, args...);
244 
245  return regIOobject::store(objectPtr);
246  }
247 }
248 
249 
250 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * //
251 
252 template
253 <
254  class Mesh,
255  template<class> class MeshObjectType,
256  class Type,
257  class RegIOobject
258 >
261 {
263 }
264 
265 
266 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
267 
268 template
269 <
270  class Mesh,
271  template<class> class MeshObjectType,
272  class Type,
273  class RegIOobject
274 >
275 const Foam::word&
277 type() const
278 {
279  return Type::typeName;
280 }
281 
282 
283 template
284 <
285  class Mesh,
286  template<class> class MeshObjectType,
287  class Type,
288  class RegIOobject
289 >
291 found
292 (
293  const word& name,
294  const Mesh& mesh
295 )
296 {
297  return mesh.db().objectRegistry::template foundObject<Type>(name);
298 }
299 
300 
301 template
302 <
303  class Mesh,
304  template<class> class MeshObjectType,
305  class Type,
306  class RegIOobject
307 >
309 found
310 (
311  const Mesh& mesh
312 )
313 {
314  return found(Type::typeName, mesh);
315 }
316 
317 
318 // ************************************************************************* //
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 objectRegistry & db() const
Return the object registry - resolve conflict polyMesh/lduMesh.
Definition: fvMesh.H:439
const word & name() const
Return reference to name.
Definition: fvMesh.H:447
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
Template function which returns the un-mangled name of a given type. Useful for types which do not ha...
A class for handling words, derived from string.
Definition: word.H:63
Foam::fvMesh mesh(Foam::IOobject(regionName, runTime.name(), runTime, Foam::IOobject::MUST_READ), false)
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:288
String typeName(const std::type_info &info)
Return the un-mangled name given the standard type info.
word name(const LagrangianState state)
Return a string representation of a Lagrangian state enumeration.
prefixOSstream Pout(cout, "Pout")
Definition: IOstreams.H:53
Foam::argList args(argc, argv)