MeshObject.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) 2011-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 "MeshObject.H"
27 #include "objectRegistry.H"
28 #include "IOstreams.H"
29 
30 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
31 
32 template<class Mesh, template<class> class MeshObjectType, class Type>
34 :
35  MeshObjectType<Mesh>(Type::typeName, mesh.thisDb()),
36  mesh_(mesh)
37 {}
38 
39 
40 // * * * * * * * * * * * * * * * * Selectors * * * * * * * * * * * * * * * * //
41 
42 template<class Mesh, template<class> class MeshObjectType, class Type>
44 (
45  const Mesh& mesh
46 )
47 {
48  if
49  (
50  mesh.thisDb().objectRegistry::template foundObject<Type>
51  (
52  Type::typeName
53  )
54  )
55  {
56  return mesh.thisDb().objectRegistry::template lookupObject<Type>
57  (
58  Type::typeName
59  );
60  }
61  else
62  {
63  if (meshObject::debug)
64  {
65  Pout<< "MeshObject::New(const " << Mesh::typeName
66  << "&) : constructing " << Type::typeName
67  << " for region " << mesh.name() << endl;
68  }
69 
70  Type* objectPtr = new Type(mesh);
71 
72  regIOobject::store(static_cast<MeshObjectType<Mesh>*>(objectPtr));
73 
74  return *objectPtr;
75  }
76 }
77 
78 
79 template<class Mesh, template<class> class MeshObjectType, class Type>
80 template<class Data1>
82 (
83  const Mesh& mesh,
84  const Data1& d
85 )
86 {
87  if
88  (
89  mesh.thisDb().objectRegistry::template foundObject<Type>
90  (
91  Type::typeName
92  )
93  )
94  {
95  return mesh.thisDb().objectRegistry::template lookupObject<Type>
96  (
97  Type::typeName
98  );
99  }
100  else
101  {
102  if (meshObject::debug)
103  {
104  Pout<< "MeshObject::New(const " << Mesh::typeName
105  << "&, const Data1&) : constructing " << Type::typeName
106  << " for region " << mesh.name() << endl;
107  }
108 
109  Type* objectPtr = new Type(mesh, d);
110 
111  regIOobject::store(static_cast<MeshObjectType<Mesh>*>(objectPtr));
112 
113  return *objectPtr;
114  }
115 }
116 
117 
118 template<class Mesh, template<class> class MeshObjectType, class Type>
119 template<class Data1, class Data2>
121 (
122  const Mesh& mesh,
123  const Data1& d1,
124  const Data2& d2
125 )
126 {
127  if
128  (
129  mesh.thisDb().objectRegistry::template foundObject<Type>
130  (
131  Type::typeName
132  )
133  )
134  {
135  return mesh.thisDb().objectRegistry::template lookupObject<Type>
136  (
137  Type::typeName
138  );
139  }
140  else
141  {
142  if (meshObject::debug)
143  {
144  Pout<< "MeshObject::New(const " << Mesh::typeName
145  << "&, const Data[1-2]&) : constructing " << Type::typeName
146  << " for region " << mesh.name() << endl;
147  }
148 
149  Type* objectPtr = new Type(mesh, d1, d2);
150 
151  // Make sure to register the top level regIOobject for if Type itself
152  // is a regIOobject
153  regIOobject::store(static_cast<MeshObjectType<Mesh>*>(objectPtr));
154 
155  return *objectPtr;
156  }
157 }
158 
159 
160 template<class Mesh, template<class> class MeshObjectType, class Type>
161 template<class Data1, class Data2, class Data3>
163 (
164  const Mesh& mesh,
165  const Data1& d1,
166  const Data2& d2,
167  const Data3& d3
168 )
169 {
170  if
171  (
172  mesh.thisDb().objectRegistry::template foundObject<Type>
173  (
174  Type::typeName
175  )
176  )
177  {
178  return mesh.thisDb().objectRegistry::template lookupObject<Type>
179  (
180  Type::typeName
181  );
182  }
183  else
184  {
185  if (meshObject::debug)
186  {
187  Pout<< "MeshObject::New(const " << Mesh::typeName
188  << "&, const Data[1-3]&) : constructing " << Type::typeName
189  << " for region " << mesh.name() << endl;
190  }
191  Type* objectPtr = new Type(mesh, d1, d2, d3);
192 
193  regIOobject::store(static_cast<MeshObjectType<Mesh>*>(objectPtr));
194 
195  return *objectPtr;
196  }
197 }
198 
199 
200 template<class Mesh, template<class> class MeshObjectType, class Type>
201 template<class Data1, class Data2, class Data3, class Data4>
203 (
204  const Mesh& mesh,
205  const Data1& d1,
206  const Data2& d2,
207  const Data3& d3,
208  const Data4& d4
209 )
210 {
211  if
212  (
213  mesh.thisDb().objectRegistry::template foundObject<Type>
214  (
215  Type::typeName
216  )
217  )
218  {
219  return mesh.thisDb().objectRegistry::template lookupObject<Type>
220  (
221  Type::typeName
222  );
223  }
224  else
225  {
226  if (meshObject::debug)
227  {
228  Pout<< "MeshObject::New(const " << Mesh::typeName
229  << "&, const Data[1-4]&) : constructing " << Type::typeName
230  << " for region " << mesh.name() << endl;
231  }
232  Type* objectPtr = new Type(mesh, d1, d2, d3, d4);
233 
234  regIOobject::store(static_cast<MeshObjectType<Mesh>*>(objectPtr));
235 
236  return *objectPtr;
237  }
238 }
239 
240 
241 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * //
242 
243 template<class Mesh, template<class> class MeshObjectType, class Type>
245 {
246  if
247  (
248  mesh.thisDb().objectRegistry::template foundObject<Type>
249  (
250  Type::typeName
251  )
252  )
253  {
254  if (meshObject::debug)
255  {
256  Pout<< "MeshObject::Delete(const Mesh&) : deleting "
257  << Type::typeName << endl;
258  }
259 
260  return mesh.thisDb().checkOut
261  (
262  const_cast<Type&>
263  (
264  mesh.thisDb().objectRegistry::template lookupObject<Type>
265  (
266  Type::typeName
267  )
268  )
269  );
270  }
271  else
272  {
273  return false;
274  }
275 }
276 
277 
278 template<class Mesh, template<class> class MeshObjectType, class Type>
280 {
281  MeshObjectType<Mesh>::release();
282 }
283 
284 
285 template<class Mesh>
287 {
289  (
291  );
292 
293  if (meshObject::debug)
294  {
295  Pout<< "meshObject::movePoints(objectRegistry&) :"
296  << " moving " << Mesh::typeName
297  << " meshObjects for region " << obr.name() << endl;
298  }
299 
300  forAllIter
301  (
303  meshObjects,
304  iter
305  )
306  {
307  if (isA<MoveableMeshObject<Mesh>>(*iter()))
308  {
309  if (meshObject::debug)
310  {
311  Pout<< " Moving " << iter()->name() << endl;
312  }
313  dynamic_cast<MoveableMeshObject<Mesh>*>(iter())->movePoints();
314  }
315  else
316  {
317  if (meshObject::debug)
318  {
319  Pout<< " Destroying " << iter()->name() << endl;
320  }
321  obr.checkOut(*iter());
322  }
323  }
324 }
325 
326 
327 template<class Mesh>
329 {
331  (
333  );
334 
335  if (meshObject::debug)
336  {
337  Pout<< "meshObject::updateMesh(objectRegistry&, "
338  "const mapPolyMesh& mpm) : updating " << Mesh::typeName
339  << " meshObjects for region " << obr.name() << endl;
340  }
341 
342  forAllIter
343  (
345  meshObjects,
346  iter
347  )
348  {
349  if (isA<UpdateableMeshObject<Mesh>>(*iter()))
350  {
351  if (meshObject::debug)
352  {
353  Pout<< " Updating " << iter()->name() << endl;
354  }
355  dynamic_cast<UpdateableMeshObject<Mesh>*>(iter())->updateMesh(mpm);
356  }
357  else
358  {
359  if (meshObject::debug)
360  {
361  Pout<< " Destroying " << iter()->name() << endl;
362  }
363  obr.checkOut(*iter());
364  }
365  }
366 }
367 
368 
369 template<class Mesh, template<class> class MeshObjectType>
371 {
372  HashTable<MeshObjectType<Mesh>*> meshObjects
373  (
374  obr.lookupClass<MeshObjectType<Mesh>>()
375  );
376 
377  if (meshObject::debug)
378  {
379  Pout<< "meshObject::clear(objectRegistry&) :"
380  << " clearing " << Mesh::typeName
381  << " meshObjects for region " << obr.name() << endl;
382  }
383 
384  forAllIter(typename HashTable<MeshObjectType<Mesh>*>, meshObjects, iter)
385  {
386  if (meshObject::debug)
387  {
388  Pout<< " Destroying " << iter()->name() << endl;
389  }
390  obr.checkOut(*iter());
391  }
392 }
393 
394 
395 template
396 <
397  class Mesh,
398  template<class> class FromType,
399  template<class> class ToType
400 >
402 {
403  HashTable<FromType<Mesh>*> meshObjects
404  (
405  obr.lookupClass<FromType<Mesh>>()
406  );
407 
408  if (meshObject::debug)
409  {
410  Pout<< "meshObject::clearUpto(objectRegistry&) :"
411  << " clearing " << Mesh::typeName
412  << " meshObjects for region " << obr.name() << endl;
413  }
414 
415  forAllIter(typename HashTable<FromType<Mesh>*>, meshObjects, iter)
416  {
417  if (!isA<ToType<Mesh>>(*iter()))
418  {
419  if (meshObject::debug)
420  {
421  Pout<< " Destroying " << iter()->name() << endl;
422  }
423  obr.checkOut(*iter());
424  }
425  }
426 }
427 
428 
429 // ************************************************************************* //
bool isA(const Type &t)
Check if a dynamic_cast to typeid is possible.
Definition: typeInfo.H:134
virtual const fileName & name() const
Return the name of the stream.
Definition: OSstream.H:88
const word & name() const
Return name.
Definition: IOobject.H:291
static bool Delete(const Mesh &mesh)
Definition: MeshObject.C:244
#define forAllIter(Container, container, iter)
Iterate across all elements in the container object of type.
Definition: UList.H:453
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:253
bool checkOut(regIOobject &) const
Remove an regIOobject from registry.
static void movePoints(objectRegistry &)
Definition: MeshObject.C:286
Class containing mesh-to-mesh mapping information after a change in polyMesh topology.
Definition: mapPolyMesh.H:158
static const Type & New(const Mesh &mesh)
Definition: MeshObject.C:44
Useful combination of include files which define Sin, Sout and Serr and the use of IO streams general...
virtual ~MeshObject()
Definition: MeshObject.C:279
static void clearUpto(objectRegistry &)
Clear all meshObject derived from FromType up to (but not including)
Definition: MeshObject.C:401
static void updateMesh(objectRegistry &, const mapPolyMesh &)
Definition: MeshObject.C:328
An STL-conforming hash table.
Definition: HashTable.H:62
prefixOSstream Pout(cout, "Pout")
Definition: IOstreams.H:53
Registry of regIOobjects.
MeshObject(const Mesh &mesh)
Definition: MeshObject.C:33
static void clear(objectRegistry &)
Definition: MeshObject.C:370
HashTable< const Type * > lookupClass(const bool strict=false) const
Lookup and return all objects of the given Type.