objectRegistry.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-2019 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 
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 
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  clear();
96 }
97 
98 
99 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
100 
102 {
104 }
105 
106 
108 {
110 }
111 
112 
114 {
115  wordList objectNames(size());
116 
117  label count=0;
118  for (const_iterator iter = cbegin(); iter != cend(); ++iter)
119  {
120  if (iter()->type() == ClassName)
121  {
122  objectNames[count++] = iter.key();
123  }
124  }
125 
126  objectNames.setSize(count);
127 
128  return objectNames;
129 }
130 
131 
133 {
134  wordList sortedLst = names(ClassName);
135  sort(sortedLst);
136 
137  return sortedLst;
138 }
139 
140 
142 (
143  const word& name,
144  const bool forceCreate
145 ) const
146 {
147  if (forceCreate && !foundObject<objectRegistry>(name))
148  {
149  objectRegistry* fieldsCachePtr = new objectRegistry
150  (
151  IOobject
152  (
153  name,
154  time().constant(),
155  *this,
158  )
159  );
160  fieldsCachePtr->store();
161  }
162  return lookupObject<objectRegistry>(name);
163 }
164 
165 
167 {
168  label curEvent = event_++;
169 
170  if (event_ == labelMax)
171  {
172  if (objectRegistry::debug)
173  {
175  << "Event counter has overflowed. "
176  << "Resetting counter on all dependent objects." << nl
177  << "This might cause extra evaluations." << endl;
178  }
179 
180  // Reset event counter
181  curEvent = 1;
182  event_ = 2;
183 
184  for (const_iterator iter = begin(); iter != end(); ++iter)
185  {
186  const regIOobject& io = *iter();
187 
188  if (objectRegistry::debug)
189  {
190  Pout<< "objectRegistry::getEvent() : "
191  << "resetting count on " << iter.key() << endl;
192  }
193 
194  if (io.eventNo() != 0)
195  {
196  const_cast<regIOobject&>(io).eventNo() = curEvent;
197  }
198  }
199  }
200 
201  return curEvent;
202 }
203 
204 
206 {
207  if (objectRegistry::debug)
208  {
209  Pout<< "objectRegistry::checkIn(regIOobject&) : "
210  << name() << " : checking in " << io.name()
211  << " of type " << io.type()
212  << endl;
213  }
214 
215  return const_cast<objectRegistry&>(*this).insert(io.name(), &io);
216 }
217 
218 
220 {
221  iterator iter = const_cast<objectRegistry&>(*this).find(io.name());
222 
223  if (iter != end())
224  {
225  if (objectRegistry::debug)
226  {
227  Pout<< "objectRegistry::checkOut(regIOobject&) : "
228  << name() << " : checking out " << iter.key()
229  << endl;
230  }
231 
232  if (iter() != &io)
233  {
234  if (objectRegistry::debug)
235  {
237  << name() << " : attempt to checkOut copy of "
238  << iter.key()
239  << endl;
240  }
241 
242  return false;
243  }
244  else
245  {
246  regIOobject* object = iter();
247 
248  bool hasErased = const_cast<objectRegistry&>(*this).erase(iter);
249 
250  if (io.ownedByRegistry())
251  {
252  delete object;
253  }
254 
255  return hasErased;
256  }
257  }
258  else
259  {
260  if (objectRegistry::debug)
261  {
262  Pout<< "objectRegistry::checkOut(regIOobject&) : "
263  << name() << " : could not find " << io.name()
264  << " in registry " << name()
265  << endl;
266  }
267  }
268 
269  return false;
270 }
271 
272 
274 {
275  List<regIOobject*> myObjects(size());
276  label nMyObjects = 0;
277 
278  for (iterator iter = begin(); iter != end(); ++iter)
279  {
280  if (iter()->ownedByRegistry())
281  {
282  myObjects[nMyObjects++] = iter();
283  }
284  }
285 
286  for (label i=0; i < nMyObjects; i++)
287  {
288  checkOut(*myObjects[i]);
289  }
290 }
291 
292 
293 void Foam::objectRegistry::rename(const word& newName)
294 {
295  regIOobject::rename(newName);
296 
297  // adjust dbDir_ as well
298  string::size_type i = dbDir_.rfind('/');
299 
300  if (i == string::npos)
301  {
302  dbDir_ = newName;
303  }
304  else
305  {
306  dbDir_.replace(i+1, string::npos, newName);
307  }
308 }
309 
310 
312 {
314  {
315  if (iter()->modified())
316  {
317  return true;
318  }
319  }
320 
321  return false;
322 }
323 
324 
326 {
327  for (iterator iter = begin(); iter != end(); ++iter)
328  {
329  if (objectRegistry::debug)
330  {
331  Pout<< "objectRegistry::readModifiedObjects() : "
332  << name() << " : Considering reading object "
333  << iter.key() << endl;
334  }
335 
336  iter()->readIfModified();
337  }
338 }
339 
340 
342 {
343  readModifiedObjects();
344  return true;
345 }
346 
347 
349 (
353  const bool write
354 ) const
355 {
356  bool ok = true;
357 
359  {
360  if (objectRegistry::debug)
361  {
362  Pout<< "objectRegistry::write() : "
363  << name() << " : Considering writing object "
364  << iter.key()
365  << " of type " << iter()->type()
366  << " with writeOpt " << iter()->writeOpt()
367  << " to file " << iter()->objectPath()
368  << endl;
369  }
370 
371  if (iter()->writeOpt() != NO_WRITE)
372  {
373  ok = iter()->writeObject(fmt, ver, cmp, write) && ok;
374  }
375  }
376 
377  return ok;
378 }
379 
380 
381 // ************************************************************************* //
fileName path() const
Return path.
Definition: Time.H:266
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:295
void readModifiedObjects()
Read the objects that have been modified.
tUEqn clear()
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:256
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
objectRegistry(const Time &db, const label nIoObjects=128)
Construct the time objectRegistry given an initial estimate.
A class for handling words, derived from string.
Definition: word.H:59
const fileName & caseName() const
Return case name.
Definition: Time.H:260
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:83
static const label labelMax
Definition: label.H:62
virtual bool readIfModified()
Read object if modified.
An STL-conforming hash table.
Definition: HashTable.H:61
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:211
static const char nl
Definition: Ostream.H:265
defineTypeNameAndDebug(combustionModel, 0)
virtual void rename(const word &newName)
Rename.
Definition: regIOobject.C:410
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 occurrence of sub-string oldStr with newStr.
Definition: string.C:56
#define WarningInFunction
Report a warning using Foam::Warning.
const Time & time() const
Return time.
Definition: IOobject.C:360
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
void clear()
Remove all regIOobject owned by the registry.
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:55
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:354
IOobject defines the attributes of an object for which implicit objectRegistry management is supporte...
Definition: IOobject.H:92
virtual bool writeObject(IOstream::streamFormat fmt, IOstream::versionNumber ver, IOstream::compressionType cmp, const bool write) const
Write the objects.
bool checkIn()
Add object to registry.
Definition: regIOobject.C:175
virtual void rename(const word &newName)
Rename.
Namespace for OpenFOAM.