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-2012 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  "",
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  {
188  WarningIn("objectRegistry::getEvent() const")
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  << endl;
226  }
227 
228  return const_cast<objectRegistry&>(*this).insert(io.name(), &io);
229 }
230 
231 
233 {
234  iterator iter = const_cast<objectRegistry&>(*this).find(io.name());
235 
236  if (iter != end())
237  {
238  if (objectRegistry::debug)
239  {
240  Pout<< "objectRegistry::checkOut(regIOobject&) : "
241  << name() << " : checking out " << iter.key()
242  << endl;
243  }
244 
245  if (iter() != &io)
246  {
247  if (objectRegistry::debug)
248  {
249  WarningIn("objectRegistry::checkOut(regIOobject&)")
250  << name() << " : attempt to checkOut copy of "
251  << iter.key()
252  << endl;
253  }
254 
255  return false;
256  }
257  else
258  {
259  regIOobject* object = iter();
260 
261  bool hasErased = const_cast<objectRegistry&>(*this).erase(iter);
262 
263  if (io.ownedByRegistry())
264  {
265  delete object;
266  }
267 
268  return hasErased;
269  }
270  }
271  else
272  {
273  if (objectRegistry::debug)
274  {
275  Pout<< "objectRegistry::checkOut(regIOobject&) : "
276  << name() << " : could not find " << io.name()
277  << " in registry " << name()
278  << endl;
279  }
280  }
281 
282  return false;
283 }
284 
285 
286 void Foam::objectRegistry::rename(const word& newName)
287 {
288  regIOobject::rename(newName);
289 
290  // adjust dbDir_ as well
291  string::size_type i = dbDir_.rfind('/');
292 
293  if (i == string::npos)
294  {
295  dbDir_ = newName;
296  }
297  else
298  {
299  dbDir_.replace(i+1, string::npos, newName);
300  }
301 }
302 
303 
305 {
307  {
308  if (iter()->modified())
309  {
310  return true;
311  }
312  }
313 
314  return false;
315 }
316 
317 
319 {
320  for (iterator iter = begin(); iter != end(); ++iter)
321  {
322  if (objectRegistry::debug)
323  {
324  Pout<< "objectRegistry::readModifiedObjects() : "
325  << name() << " : Considering reading object "
326  << iter.key() << endl;
327  }
328 
329  iter()->readIfModified();
330  }
331 }
332 
333 
335 {
336  readModifiedObjects();
337  return true;
338 }
339 
340 
342 (
346 ) const
347 {
348  bool ok = true;
349 
351  {
352  if (objectRegistry::debug)
353  {
354  Pout<< "objectRegistry::write() : "
355  << name() << " : Considering writing object "
356  << iter.key()
357  << " with writeOpt " << iter()->writeOpt()
358  << " to file " << iter()->objectPath()
359  << endl;
360  }
361 
362  if (iter()->writeOpt() != NO_WRITE)
363  {
364  ok = iter()->writeObject(fmt, ver, cmp) && ok;
365  }
366  }
367 
368  return ok;
369 }
370 
371 
372 // ************************************************************************* //
label eventNo() const
Event number at last update.
Definition: regIOobjectI.H:82
virtual ~objectRegistry()
Destructor.
virtual bool writeObject(IOstream::streamFormat fmt, IOstream::versionNumber ver, IOstream::compressionType cmp) const
Write the objects.
void sort(UList< T > &)
Definition: UList.C:107
bool erase(const iterator &)
Erase a hashedEntry specified by given iterator.
Definition: HashTable.C:379
label getEvent() const
Return new event number.
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
const objectRegistry & subRegistry(const word &name, const bool forceCreate=false) const
Lookup and return a const sub-objectRegistry. Optionally create.
bool insert(const Key &, const T &newElmt)
Insert a new hashedEntry.
Definition: HashTableI.H:80
List< Key > sortedToc() const
Return the table of contents as a sorted list.
Definition: HashTable.C:216
A class for handling words, derived from string.
Definition: word.H:59
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
bool checkOut()
Remove object from registry.
Definition: regIOobject.C:308
virtual bool readIfModified()
Read object if modified.
Namespace for OpenFOAM.
virtual void rename(const word &newName)
Rename.
Definition: regIOobject.C:416
forAllConstIter(PtrDictionary< phaseModel >, mixture.phases(), phase)
Definition: pEqn.H:39
#define ClassName(TypeNameString)
Add typeName information from argument TypeNameString to a class.
Definition: className.H:69
virtual bool modified() const
Return true if any of the object&#39;s files have been modified.
graph_traits< Graph >::vertices_size_type size_type
Definition: SloanRenumber.C:73
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: HashTable.H:59
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:68
static const char nl
Definition: Ostream.H:260
void setSize(const label)
Reset size of List.
Definition: List.C:318
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
IOobject defines the attributes of an object for which implicit objectRegistry management is supporte...
Definition: IOobject.H:91
#define WarningIn(functionName)
Report a warning using Foam::Warning.
wordList sortedNames() const
Return the sorted list of names of the IOobjects.
virtual void rename(const word &newName)
Rename.
streamFormat
Enumeration for the format of data in the stream.
Definition: IOstream.H:86
void store()
Transfer ownership of this object to its registry.
Definition: regIOobjectI.H:34
List< Key > toc() const
Return the table of contents.
Definition: HashTable.C:201
void readModifiedObjects()
Read the objects that have been modified.
const word & name() const
Return name.
Definition: IOobject.H:260
wordList names() const
Return the list of names of the IOobjects.
compressionType
Enumeration for the format of data in the stream.
Definition: IOstream.H:193
bool checkIn()
Add object to registry.
Definition: regIOobject.C:247
regIOobject is an abstract class derived from IOobject to handle automatic object registration with t...
Definition: regIOobject.H:60
Constant dispersed-phase particle diameter model.
bool ownedByRegistry() const
Is this object owned by the registry?
Definition: regIOobjectI.H:28
Registry of regIOobjects.
string & replace(const string &oldStr, const string &newStr, size_type start=0)
Replace first occurence of sub-string oldStr with newStr.
Definition: string.C:58
const Time & time() const
Return time.
Definition: IOobject.C:251
iterator find(const Key &)
Find and return an iterator set at the hashedEntry.
Definition: HashTable.C:139
const fileName & caseName() const
Return case name.
Definition: Time.H:275
Version number type.
Definition: IOstream.H:96
const objectRegistry & db() const
Return the local objectRegistry.
Definition: IOobject.C:245
defineTypeNameAndDebug(combustionModel, 0)
prefixOSstream Pout(cout,"Pout")
Definition: IOstreams.H:53
static const label labelMax
Definition: label.H:62