objectRegistry.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-2017 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 "objectRegistry.H"
27 #include "Time.H"
28 
29 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
30 
31 namespace Foam
32 {
33 defineTypeNameAndDebug(objectRegistry, 0);
34 }
35 
36 
37 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
38 
39 bool Foam::objectRegistry::parentNotTime() const
40 {
41  return (&parent_ != dynamic_cast<const objectRegistry*>(&time_));
42 }
43 
44 
45 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
46 
47 Foam::objectRegistry::objectRegistry
48 (
49  const Time& t,
50  const label nIoObjects
51 )
52 :
54  (
55  IOobject
56  (
57  string::validate<word>(t.caseName()),
58  t.path(),
59  t,
62  false
63  ),
64  true // to flag that this is the top-level regIOobject
65  ),
66  HashTable<regIOobject*>(nIoObjects),
67  time_(t),
68  parent_(t),
69  dbDir_(name()),
70  event_(1)
71 {}
72 
73 
74 Foam::objectRegistry::objectRegistry
75 (
76  const IOobject& io,
77  const label nIoObjects
78 )
79 :
80  regIOobject(io),
81  HashTable<regIOobject*>(nIoObjects),
82  time_(io.time()),
83  parent_(io.db()),
84  dbDir_(parent_.dbDir()/local()/name()),
85  event_(1)
86 {
87  writeOpt() = IOobject::AUTO_WRITE;
88 }
89 
90 
91 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
92 
94 {
95  List<regIOobject*> myObjects(size());
96  label nMyObjects = 0;
97 
98  for (iterator iter = begin(); iter != end(); ++iter)
99  {
100  if (iter()->ownedByRegistry())
101  {
102  myObjects[nMyObjects++] = iter();
103  }
104  }
105 
106  for (label i=0; i < nMyObjects; i++)
107  {
108  checkOut(*myObjects[i]);
109  }
110 }
111 
112 
113 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
114 
116 {
118 }
119 
120 
122 {
124 }
125 
126 
128 {
129  wordList objectNames(size());
130 
131  label count=0;
132  for (const_iterator iter = cbegin(); iter != cend(); ++iter)
133  {
134  if (iter()->type() == ClassName)
135  {
136  objectNames[count++] = iter.key();
137  }
138  }
139 
140  objectNames.setSize(count);
141 
142  return objectNames;
143 }
144 
145 
147 {
148  wordList sortedLst = names(ClassName);
149  sort(sortedLst);
150 
151  return sortedLst;
152 }
153 
154 
156 (
157  const word& name,
158  const bool forceCreate
159 ) const
160 {
161  if (forceCreate && !foundObject<objectRegistry>(name))
162  {
163  objectRegistry* fieldsCachePtr = new objectRegistry
164  (
165  IOobject
166  (
167  name,
168  time().constant(),
169  *this,
172  )
173  );
174  fieldsCachePtr->store();
175  }
176  return lookupObject<objectRegistry>(name);
177 }
178 
179 
181 {
182  label curEvent = event_++;
183 
184  if (event_ == labelMax)
185  {
186  if (objectRegistry::debug)
187  {
189  << "Event counter has overflowed. "
190  << "Resetting counter on all dependent objects." << nl
191  << "This might cause extra evaluations." << endl;
192  }
193 
194  // Reset event counter
195  curEvent = 1;
196  event_ = 2;
197 
198  for (const_iterator iter = begin(); iter != end(); ++iter)
199  {
200  const regIOobject& io = *iter();
201 
202  if (objectRegistry::debug)
203  {
204  Pout<< "objectRegistry::getEvent() : "
205  << "resetting count on " << iter.key() << endl;
206  }
207 
208  if (io.eventNo() != 0)
209  {
210  const_cast<regIOobject&>(io).eventNo() = curEvent;
211  }
212  }
213  }
214 
215  return curEvent;
216 }
217 
218 
220 {
221  if (objectRegistry::debug)
222  {
223  Pout<< "objectRegistry::checkIn(regIOobject&) : "
224  << name() << " : checking in " << io.name()
225  << " of type " << io.type()
226  << endl;
227  }
228 
229  return const_cast<objectRegistry&>(*this).insert(io.name(), &io);
230 }
231 
232 
234 {
235  iterator iter = const_cast<objectRegistry&>(*this).find(io.name());
236 
237  if (iter != end())
238  {
239  if (objectRegistry::debug)
240  {
241  Pout<< "objectRegistry::checkOut(regIOobject&) : "
242  << name() << " : checking out " << iter.key()
243  << endl;
244  }
245 
246  if (iter() != &io)
247  {
248  if (objectRegistry::debug)
249  {
251  << name() << " : attempt to checkOut copy of "
252  << iter.key()
253  << endl;
254  }
255 
256  return false;
257  }
258  else
259  {
260  regIOobject* object = iter();
261 
262  bool hasErased = const_cast<objectRegistry&>(*this).erase(iter);
263 
264  if (io.ownedByRegistry())
265  {
266  delete object;
267  }
268 
269  return hasErased;
270  }
271  }
272  else
273  {
274  if (objectRegistry::debug)
275  {
276  Pout<< "objectRegistry::checkOut(regIOobject&) : "
277  << name() << " : could not find " << io.name()
278  << " in registry " << name()
279  << endl;
280  }
281  }
282 
283  return false;
284 }
285 
286 
287 void Foam::objectRegistry::rename(const word& newName)
288 {
289  regIOobject::rename(newName);
290 
291  // adjust dbDir_ as well
292  string::size_type i = dbDir_.rfind('/');
293 
294  if (i == string::npos)
295  {
296  dbDir_ = newName;
297  }
298  else
299  {
300  dbDir_.replace(i+1, string::npos, newName);
301  }
302 }
303 
304 
306 {
308  {
309  if (iter()->modified())
310  {
311  return true;
312  }
313  }
314 
315  return false;
316 }
317 
318 
320 {
321  for (iterator iter = begin(); iter != end(); ++iter)
322  {
323  if (objectRegistry::debug)
324  {
325  Pout<< "objectRegistry::readModifiedObjects() : "
326  << name() << " : Considering reading object "
327  << iter.key() << endl;
328  }
329 
330  iter()->readIfModified();
331  }
332 }
333 
334 
336 {
337  readModifiedObjects();
338  return true;
339 }
340 
341 
343 (
347  const bool valid
348 ) const
349 {
350  bool ok = true;
351 
353  {
354  if (objectRegistry::debug)
355  {
356  Pout<< "objectRegistry::write() : "
357  << name() << " : Considering writing object "
358  << iter.key()
359  << " of type " << iter()->type()
360  << " with writeOpt " << iter()->writeOpt()
361  << " to file " << iter()->objectPath()
362  << endl;
363  }
364 
365  if (iter()->writeOpt() != NO_WRITE)
366  {
367  ok = iter()->writeObject(fmt, ver, cmp, valid) && ok;
368  }
369  }
370 
371  return ok;
372 }
373 
374 
375 // ************************************************************************* //
fileName path() const
Return path.
Definition: Time.H:269
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
const word & name() const
Return name.
Definition: IOobject.H:291
virtual bool writeObject(IOstream::streamFormat fmt, IOstream::versionNumber ver, IOstream::compressionType cmp, const bool valid) const
Write the objects.
void readModifiedObjects()
Read the objects that have been modified.
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: HashTable.H:60
wordList names() const
Return the list of names of the IOobjects.
virtual bool modified() const
Return true if any of the object&#39;s files have been modified.
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:253
bool ownedByRegistry() const
Is this object owned by the registry?
Definition: regIOobjectI.H:28
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:68
bool erase(const iterator &)
Erase a hashedEntry specified by given iterator.
Definition: HashTable.C:371
bool insert(const Key &, const T &newElmt)
Insert a new hashedEntry.
Definition: HashTableI.H:80
#define ClassName(TypeNameString)
Add typeName information from argument TypeNameString to a class.
Definition: className.H:65
iterator find(const Key &)
Find and return an iterator set at the hashedEntry.
Definition: HashTable.C:142
A class for handling words, derived from string.
Definition: word.H:59
const fileName & caseName() const
Return case name.
Definition: Time.H:263
wordList sortedNames() const
Return the sorted list of names of the IOobjects.
void sort(UList< T > &)
Definition: UList.C:115
virtual ~objectRegistry()
Destructor.
label getEvent() const
Return new event number.
streamFormat
Enumeration for the format of data in the stream.
Definition: IOstream.H:86
label eventNo() const
Event number at last update.
Definition: regIOobjectI.H:80
static const label labelMax
Definition: label.H:62
virtual bool readIfModified()
Read object if modified.
An STL-conforming hash table.
Definition: HashTable.H:62
graph_traits< Graph >::vertices_size_type size_type
Definition: SloanRenumber.C:73
forAllConstIter(PtrDictionary< phaseModel >, mixture.phases(), phase)
Definition: pEqn.H:29
compressionType
Enumeration for the format of data in the stream.
Definition: IOstream.H:193
void store()
Transfer ownership of this object to its registry.
Definition: regIOobjectI.H:34
bool checkOut()
Remove object from registry.
Definition: regIOobject.C:203
static const char nl
Definition: Ostream.H:262
defineTypeNameAndDebug(combustionModel, 0)
virtual void rename(const word &newName)
Rename.
Definition: regIOobject.C:402
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
void setSize(const label)
Reset size of List.
Definition: List.C:281
string & replace(const string &oldStr, const string &newStr, size_type start=0)
Replace first occurence of sub-string oldStr with newStr.
Definition: string.C:56
#define WarningInFunction
Report a warning using Foam::Warning.
Constant dispersed-phase particle diameter model.
const Time & time() const
Return time.
Definition: IOobject.C:337
prefixOSstream Pout(cout, "Pout")
Definition: IOstreams.H:53
List< Key > sortedToc() const
Return the table of contents as a sorted list.
Definition: HashTable.C:217
Version number type.
Definition: IOstream.H:96
regIOobject is an abstract class derived from IOobject to handle automatic object registration with t...
Definition: regIOobject.H:65
const objectRegistry & subRegistry(const word &name, const bool forceCreate=false) const
Lookup and return a const sub-objectRegistry. Optionally create.
List< Key > toc() const
Return the table of contents.
Definition: HashTable.C:202
Registry of regIOobjects.
const objectRegistry & db() const
Return the local objectRegistry.
Definition: IOobject.C:331
IOobject defines the attributes of an object for which implicit objectRegistry management is supporte...
Definition: IOobject.H:92
bool checkIn()
Add object to registry.
Definition: regIOobject.C:167
virtual void rename(const word &newName)
Rename.
Namespace for OpenFOAM.