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-2023 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 Mesh& mesh
35 )
36 :
38  (
39  IOobject
40  (
41  Type::typeName,
42  mesh.thisDb().instance(),
43  mesh.thisDb()
44  )
45  ),
46  MeshObjectType<Mesh>(*this, mesh),
47  mesh_(mesh)
48 {}
49 
50 
51 template<class Mesh, template<class> class MeshObjectType, class Type>
53 (
54  const Mesh& mesh,
55  const IOobject& io
56 )
57 :
58  regIOobject(io),
59  MeshObjectType<Mesh>(*this, mesh),
60  mesh_(mesh)
61 {}
62 
63 
64 // * * * * * * * * * * * * * * * * Selectors * * * * * * * * * * * * * * * * //
65 
66 template<class Mesh, template<class> class MeshObjectType, class Type>
68 (
69  const Mesh& mesh
70 )
71 {
72  if (found(mesh))
73  {
74  return mesh.thisDb().objectRegistry::template lookupObjectRef<Type>
75  (
76  Type::typeName
77  );
78  }
79  else
80  {
81  if (meshObjects::debug)
82  {
83  Pout<< "DemandDrivenMeshObject::New(" << Mesh::typeName
84  << "&) : constructing " << Type::typeName
85  << " for region " << mesh.name() << endl;
86  }
87 
88  Type* objectPtr = new Type(mesh);
89 
90  return regIOobject::store(objectPtr);
91  }
92 }
93 
94 
95 template<class Mesh, template<class> class MeshObjectType, class Type>
96 template<class... Args>
98 (
99  const Mesh& mesh,
100  const Args&... args
101 )
102 {
103  if (found(mesh))
104  {
105  return mesh.thisDb().objectRegistry::template lookupObjectRef<Type>
106  (
107  Type::typeName
108  );
109  }
110  else
111  {
112  if (meshObjects::debug)
113  {
114  Pout<< "DemandDrivenMeshObject::New(" << Mesh::typeName
115  << "&, const Data1&) : constructing " << Type::typeName
116  << " for region " << mesh.name() << endl;
117  }
118 
119  Type* objectPtr = new Type(mesh, args...);
120 
121  return regIOobject::store(objectPtr);
122  }
123 }
124 
125 
126 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * //
127 
128 template<class Mesh, template<class> class MeshObjectType, class Type>
130 (
131  const Mesh& mesh
132 )
133 {
134  if (found(mesh))
135  {
136  if (meshObjects::debug)
137  {
138  Pout<< "DemandDrivenMeshObject::Delete(const Mesh&) : deleting "
139  << Type::typeName << endl;
140  }
141 
142  return mesh.thisDb().checkOut
143  (
144  const_cast<Type&>
145  (
146  mesh.thisDb().objectRegistry::template lookupObject<Type>
147  (
148  Type::typeName
149  )
150  )
151  );
152  }
153  else
154  {
155  return false;
156  }
157 }
158 
159 
160 template<class Mesh, template<class> class MeshObjectType, class Type>
163 {
165 }
166 
167 
168 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
169 
170 template<class Mesh, template<class> class MeshObjectType, class Type>
172 (
173  const Mesh& mesh
174 )
175 {
176  return mesh.thisDb().objectRegistry::template foundObject<Type>
177  (
178  Type::typeName
179  );
180 }
181 
182 
183 // ************************************************************************* //
bool found
static bool Delete(const Mesh &mesh)
Lookup DemandDrivenMeshObject and delete.
static Type & New(const Mesh &mesh)
DemandDrivenMeshObject(const Mesh &mesh)
Construct from mesh.
static bool found(const Mesh &mesh)
Return true if this DemandDrivenMeshObject is found.
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
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
prefixOSstream Pout(cout, "Pout")
Definition: IOstreams.H:53
Foam::argList args(argc, argv)