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 | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2011-2017 OpenFOAM Foundation
9  Copyright (C) 2015-2021 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 \*---------------------------------------------------------------------------*/
28 
29 #include "objectRegistry.H"
30 #include "Time.H"
31 #include "predicates.H"
32 
33 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
34 
35 namespace Foam
36 {
37  defineTypeNameAndDebug(objectRegistry, 0);
38 }
39 
40 
41 // * * * * * * * * * * * * * * * Local Functions * * * * * * * * * * * * * * //
42 
43 namespace Foam
44 {
45 
46 // Templated implementation for erase() with iterator range.
47 // Prefer not to expose directly.
48 template<class InputIter>
49 static label eraseImpl(objectRegistry& obr, InputIter first, InputIter last)
50 {
51  label changed = 0;
52 
53  for
54  (
55  const label nTotal = obr.size();
56  changed < nTotal && first != last; // Terminate early
57  ++first
58  )
59  {
60  if (obr.erase(*first))
61  {
62  ++changed;
63  }
64  }
65 
66  return changed;
67 }
68 
69 } // End namespace Foam
70 
71 
72 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
73 
74 bool Foam::objectRegistry::parentNotTime() const noexcept
75 {
76  return (&parent_ != static_cast<const objectRegistry*>(&time_));
77 }
78 
79 
80 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
81 
82 Foam::objectRegistry::objectRegistry(const Time& t, const label nObjects)
83 :
85  (
86  IOobject
87  (
88  word::validate(t.caseName()),
89  t.path(),
90  t,
91  IOobject::NO_READ,
92  IOobject::AUTO_WRITE,
93  false
94  ),
95  true // to flag that this is the top-level regIOobject
96  ),
97  HashTable<regIOobject*>(nObjects),
98  time_(t),
99  parent_(t),
100  dbDir_(name()),
101  event_(1)
102 {}
103 
104 
105 Foam::objectRegistry::objectRegistry(const IOobject& io, const label nObjects)
106 :
107  regIOobject(io),
108  HashTable<regIOobject*>(nObjects),
109  time_(io.time()),
110  parent_(io.db()),
111  dbDir_(parent_.dbDir()/local()/name()),
112  event_(1)
113 {
115 }
116 
117 
118 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
119 
121 {
123 }
124 
125 
126 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
127 
128 bool Foam::objectRegistry::isTimeDb() const noexcept
129 {
130  return (this == &static_cast<const objectRegistry&>(time_));
131 }
132 
133 
135 {
136  return classesImpl(*this, predicates::always());
137 }
138 
139 
140 Foam::label Foam::objectRegistry::count(const char* clsName) const
141 {
142  // No nullptr check - only called with string literals
143  return count(static_cast<word>(clsName));
144 }
145 
146 
148 {
150 }
151 
152 
154 {
156 }
157 
158 
159 Foam::wordList Foam::objectRegistry::names(const char* clsName) const
160 {
161  // No nullptr check - only called with string literals
162  return names(static_cast<word>(clsName));
163 }
164 
165 
167 {
168  // No nullptr check - only called with string literals
169  return sortedNames(static_cast<word>(clsName));
170 }
171 
172 
174 (
175  const word& name,
176  const bool forceCreate,
177  const bool recursive
178 ) const
179 {
180  if (forceCreate && !foundObject<objectRegistry>(name, recursive))
181  {
182  objectRegistry* subObr = new objectRegistry
183  (
184  IOobject
185  (
186  name,
187  time().constant(),
188  *this,
191  )
192  );
193  subObr->store();
194  }
195 
196  return lookupObject<objectRegistry>(name, recursive);
197 }
198 
199 
201 {
202  label curEvent = event_++;
203 
204  if (event_ == labelMax)
205  {
207  {
209  << "Event counter has overflowed. "
210  << "Resetting counter on all dependent objects." << nl
211  << "This might cause extra evaluations." << endl;
212  }
213 
214  // Reset event counter
215  curEvent = 1;
216  event_ = 2;
217 
218  // No need to reset dependent objects; overflow is now handled
219  // in regIOobject::upToDate
220  }
221 
222  return curEvent;
223 }
224 
225 
227 {
228  if (!io) return false;
229 
231  {
232  Pout<< "objectRegistry::checkIn : "
233  << name() << " : checking in " << io->name()
234  << " of type " << io->type()
235  << endl;
236  }
237 
238  objectRegistry& obr = const_cast<objectRegistry&>(*this);
239 
240  bool ok = obr.insert(io->name(), io);
241 
242  if (!ok && objectRegistry::debug)
243  {
245  << name() << " : Attempt to checkIn object with name "
246  << io->name() << " which was already checked in"
247  << endl;
248  }
249 
250  return ok;
251 }
252 
253 
255 {
256  if (!io) return false;
257 
258  objectRegistry& obr = const_cast<objectRegistry&>(*this);
259 
260  iterator iter = obr.find(io->name());
261 
262  if (iter.found())
263  {
265  {
266  Pout<< "objectRegistry::checkOut : "
267  << name() << " : checking out " << io->name()
268  << " of type " << io->type()
269  << endl;
270  }
271 
272  if (iter.val() != io)
273  {
275  {
277  << name() << " : Attempt to checkOut copy of "
278  << iter.key()
279  << endl;
280  }
281 
282  return false;
283  }
284 
285  return obr.erase(iter);
286  }
287 
288 
290  {
291  Pout<< "objectRegistry::checkOut : "
292  << name() << " : could not find " << io->name() << " in registry"
293  << endl;
294  }
295 
296  return false;
297 }
298 
299 
301 {
302  return checkIn(&io);
303 }
304 
305 
307 {
308  return checkOut(&io);
309 }
310 
311 
313 {
314  return const_cast<objectRegistry&>(*this).erase(key);
315 }
316 
317 
319 {
320  // Free anything owned by the registry, but first unset both
321  // 'ownedByRegistry' and 'registered' flags to ensure that the
322  // regIOobject destructor will not affect the registry
323 
324  for (iterator iter = begin(); iter != end(); ++iter)
325  {
326  regIOobject* ptr = iter.val();
327 
328  if (ptr && ptr->ownedByRegistry())
329  {
331  {
332  Pout<< "objectRegistry::clear : " << ptr->name() << nl;
333  }
334 
335  ptr->release(true); // Relinquish ownership and registration
336  delete ptr; // Delete also clears fileHandler watches
337  }
338  }
339 
341 }
342 
343 
345 {
348 }
349 
350 
351 bool Foam::objectRegistry::erase(const iterator& iter)
352 {
353  // Remove from registry - see notes in objectRegistry::clear()
354 
355  if (iter.found())
356  {
357  regIOobject* ptr = const_cast<iterator&>(iter).val();
358 
359  const bool ok = HashTable<regIOobject*>::erase(iter);
360 
361  if (ptr && ptr->ownedByRegistry())
362  {
363  ptr->release(true); // Relinquish ownership and registration
364  delete ptr; // Delete also clears fileHandler watches
365  }
366 
367  return ok;
368  }
369 
370  return false;
371 }
372 
373 
375 {
376  return erase(find(key));
377 }
378 
379 
380 Foam::label Foam::objectRegistry::erase(std::initializer_list<word> keys)
381 {
382  return eraseImpl(*this, keys.begin(), keys.end());
383 }
384 
385 
386 Foam::label Foam::objectRegistry::erase(const UList<word>& keys)
387 {
388  return eraseImpl(*this, keys.begin(), keys.end());
389 }
390 
391 
392 void Foam::objectRegistry::rename(const word& newName)
393 {
394  regIOobject::rename(newName);
395 
396  // Adjust dbDir_ as well
397  const auto i = dbDir_.rfind('/');
398 
399  if (i == string::npos)
400  {
401  dbDir_ = newName;
402  }
403  else
404  {
405  dbDir_.replace(i+1, string::npos, newName);
406  }
407 }
408 
409 
411 (
412  const word& name,
413  const bool recursive
414 ) const
415 {
416  const_iterator iter = cfind(name);
417 
418  if (iter.found())
419  {
420  return iter.val();
421  }
422  else if (recursive && this->parentNotTime())
423  {
424  return parent_.cfindIOobject(name, recursive);
425  }
426 
427  return nullptr;
428 }
429 
430 
432 (
433  const word& name,
434  const bool recursive
435 ) const
436 {
437  return cfindIOobject(name, recursive);
438 }
439 
440 
442 {
443  for (const_iterator iter = cbegin(); iter != cend(); ++iter)
444  {
445  if ((*iter)->modified())
446  {
447  return true;
448  }
449  }
450 
451  return false;
452 }
453 
454 
456 {
457  for (iterator iter = begin(); iter != end(); ++iter)
458  {
460  {
461  Pout<< "objectRegistry::readModifiedObjects() : "
462  << name() << " : Considering reading object "
463  << iter.key() << endl;
464  }
465 
466  (*iter)->readIfModified();
467  }
468 }
469 
470 
472 {
473  readModifiedObjects();
474  return true;
475 }
476 
477 
479 (
480  IOstreamOption streamOpt,
481  const bool valid
482 ) const
483 {
484  bool ok = true;
485 
486  for (const_iterator iter = cbegin(); iter != cend(); ++iter)
487  {
489  {
490  Pout<< "objectRegistry::write() : "
491  << name() << " : Considering writing object "
492  << iter.key()
493  << " of type " << (*iter)->type()
494  << " with writeOpt " << static_cast<int>((*iter)->writeOpt())
495  << " to file " << (*iter)->objectPath()
496  << endl;
497  }
498 
499  if ((*iter)->writeOpt() != NO_WRITE)
500  {
501  ok = (*iter)->writeObject(streamOpt, valid) && ok;
502  }
503  }
504 
505  return ok;
506 }
507 
508 
509 // ************************************************************************* //
Foam::string::replace
string & replace(const std::string &s1, const std::string &s2, size_type pos=0)
Definition: string.C:108
Foam::expressions::patchExpr::debug
int debug
Static debugging option.
Foam::IOobject::NO_WRITE
Definition: IOobject.H:195
Foam::HashTable::size
label size() const noexcept
The number of elements in table.
Definition: HashTableI.H:52
Foam::objectRegistry::readIfModified
virtual bool readIfModified()
Read object if modified.
Definition: objectRegistry.C:471
Foam::objectRegistry::sortedNames
wordList sortedNames() const
The sorted names of all objects.
Definition: objectRegistry.C:153
Foam::IOobject
Defines the attributes of an object for which implicit objectRegistry management is supported,...
Definition: IOobject.H:169
Foam::objectRegistry::~objectRegistry
virtual ~objectRegistry()
Destructor, with checkOut() for all objects that are ownedByRegistry.
Definition: objectRegistry.C:120
Foam::IOobject::AUTO_WRITE
Definition: IOobject.H:194
Foam::objectRegistry::getEvent
label getEvent() const
Return new event number.
Definition: objectRegistry.C:200
Foam::Time
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:73
Foam::labelMax
constexpr label labelMax
Definition: label.H:61
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:65
stdFoam::begin
constexpr auto begin(C &c) -> decltype(c.begin())
Return iterator to the beginning of the container c.
Definition: stdFoam.H:97
Foam::UList::end
iterator end() noexcept
Return an iterator to end traversing the UList.
Definition: UListI.H:350
Foam::HashTable::toc
List< Key > toc() const
The table of contents (the keys) in unsorted order.
Definition: HashTable.C:121
Foam::regIOobject::release
void release(const bool unregister=false)
Release ownership of this object from its registry.
Definition: regIOobjectI.H:175
Foam::objectRegistry::isTimeDb
bool isTimeDb() const noexcept
True if the registry is Time.
Definition: objectRegistry.C:128
Foam::objectRegistry::clearStorage
void clearStorage()
Clear all entries from the registry and the table itself.
Definition: objectRegistry.C:344
Foam::regIOobject::checkIn
bool checkIn()
Add object to registry, if not already registered.
Definition: regIOobject.C:188
Foam::glTF::key
auto key(const Type &t) -> typename std::enable_if< std::is_enum< Type >::value, typename std::underlying_type< Type >::type >::type
Definition: foamGltfBase.H:108
objectRegistry.H
Foam::HashTable::insert
bool insert(const Key &key, const T &obj)
Copy insert a new entry, not overwriting existing entries.
Definition: HashTableI.H:180
Foam::objectRegistry::clear
void clear()
Clear all entries from the registry.
Definition: objectRegistry.C:318
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:369
erase
srcOptions erase("case")
Foam::Pout
prefixOSstream Pout
OSstream wrapped stdout (std::cout) with parallel prefix.
Foam::IOobject::writeOpt
writeOption writeOpt() const noexcept
The write option.
Definition: IOobjectI.H:179
Foam::regIOobject::store
bool store()
Definition: regIOobjectI.H:37
Foam::predicates::always
Unary and binary predicates that always return true, useful for templating.
Definition: predicates.H:56
Foam::objectRegistry
Registry of regIOobjects.
Definition: objectRegistry.H:60
Foam::objectRegistry::rename
virtual void rename(const word &newName)
Rename.
Definition: objectRegistry.C:392
Foam::UList::begin
iterator begin() noexcept
Return an iterator to begin traversing the UList.
Definition: UListI.H:329
Foam::HashTable::erase
bool erase(const iterator &iter)
Erase an entry specified by given iterator.
Definition: HashTable.C:392
Foam::objectRegistry::classes
HashTable< wordHashSet > classes() const
A summary hash of classes used and their associated object names.
Definition: objectRegistry.C:134
Foam::IOstreamOption
The IOstreamOption is a simple container for options an IOstream can normally have.
Definition: IOstreamOption.H:63
Foam::objectRegistry::cfindIOobject
const regIOobject * cfindIOobject(const word &name, const bool recursive=false) const
Return const pointer to the regIOobject.
Definition: objectRegistry.C:411
Foam::objectRegistry::writeObject
virtual bool writeObject(IOstreamOption streamOpt, const bool valid) const
Write the objects using stream options.
Definition: objectRegistry.C:479
Foam::objectRegistry::found
bool found(const word &name, const bool recursive=false) const
Can the regIOobject object be found (by name).
Definition: objectRegistry.C:432
stdFoam::end
constexpr auto end(C &c) -> decltype(c.end())
Return iterator to the end of the container c.
Definition: stdFoam.H:121
Foam::ListOps::find
label find(const ListType &input, const UnaryPredicate &pred, const label start=0)
Find index of the first occurrence that satisfies the predicate.
Foam::regIOobject::ownedByRegistry
bool ownedByRegistry() const
Is this object owned by the registry?
Definition: regIOobjectI.H:31
Foam::eraseImpl
static label eraseImpl(objectRegistry &obr, InputIter first, InputIter last)
Definition: objectRegistry.C:49
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
stdFoam::cend
constexpr auto cend(const C &c) -> decltype(c.end())
Return const_iterator to the end of the container c.
Definition: stdFoam.H:137
Foam::HashTable::sortedToc
List< Key > sortedToc() const
The table of contents (the keys) in sorted order.
Definition: HashTable.C:136
Foam::HashTable::find
iterator find(const Key &key)
Find and return an iterator set at the hashed entry.
Definition: HashTableI.H:114
Foam::HashTable
A HashTable similar to std::unordered_map.
Definition: HashTable.H:105
Foam::objectRegistry::modified
virtual bool modified() const
Return true if any of the object's files have been modified.
Definition: objectRegistry.C:441
Foam::objectRegistry::readModifiedObjects
void readModifiedObjects()
Read the objects that have been modified.
Definition: objectRegistry.C:455
Foam::IOobject::name
const word & name() const noexcept
Return name.
Definition: IOobjectI.H:65
Time.H
Foam::regIOobject
regIOobject is an abstract class derived from IOobject to handle automatic object registration with t...
Definition: regIOobject.H:73
Foam::objectRegistry::erase
bool erase(const iterator &iter)
Erase an entry specified by the given iterator.
Definition: objectRegistry.C:351
Foam::HashTable::clearStorage
void clearStorage()
Clear the table entries and the table itself.
Definition: HashTable.C:649
Foam::regIOobject::checkOut
bool checkOut()
Remove all file watches and remove object from registry.
Definition: regIOobject.C:224
Foam::nl
constexpr char nl
Definition: Ostream.H:404
stdFoam::cbegin
constexpr auto cbegin(const C &c) -> decltype(c.begin())
Return const_iterator to the beginning of the container c.
Definition: stdFoam.H:113
Foam::HashTable::clear
void clear()
Clear all entries from table.
Definition: HashTable.C:630
Foam::objectRegistry::names
wordList names() const
The names of all objects.
Definition: objectRegistry.C:147
Foam::objectRegistry::subRegistry
const objectRegistry & subRegistry(const word &name, const bool forceCreate=false, const bool recursive=false) const
Lookup and return a const sub-objectRegistry.
Definition: objectRegistry.C:174
Foam::BitOps::count
unsigned int count(const UList< bool > &bools, const bool val=true)
Count number of 'true' entries.
Definition: BitOps.H:77
Foam::regIOobject::rename
virtual void rename(const word &newName)
Rename.
Definition: regIOobject.C:415
Foam::List< word >
Foam::UList
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition: HashTable.H:103
path
fileName path(UMean.rootPath()/UMean.caseName()/"graphs"/UMean.instance())
Foam::name
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59
predicates.H
Foam::stringOps::validate
StringType validate(const std::string &str, const UnaryPredicate &accept, const bool invert=false)
Return a copy of the input string with validated characters.
Definition: stringOpsTemplates.C:71
Foam::IOobject::NO_READ
Definition: IOobject.H:188
Foam::PtrListOps::names
List< word > names(const UPtrList< T > &list, const UnaryMatchPredicate &matcher)
Foam::objectRegistry::count
label count(const char *clsName) const
The number of objects of the given class name.
Definition: objectRegistry.C:140
constant
constant condensation/saturation model.
Foam::defineTypeNameAndDebug
defineTypeNameAndDebug(combustionModel, 0)
WarningInFunction
#define WarningInFunction
Report a warning using Foam::Warning.
Definition: messageStream.H:328