objectRegistryTemplates.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-2024 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 "stringListOps.H"
28 
29 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
30 
31 template<class Type>
33 {
34  wordList objectNames(size());
35 
36  label count=0;
38  {
39  if (isA<Type>(*iter()))
40  {
41  objectNames[count++] = iter()->name();
42  }
43  }
44 
45  objectNames.setSize(count);
46 
47  return objectNames;
48 }
49 
50 
51 template<class Type>
53 {
54  wordList objectNames(size());
55 
56  label count = 0;
58  {
59  if (isA<Type>(*iter()))
60  {
61  const word& objectName = iter()->name();
62 
63  if (name.match(objectName))
64  {
65  objectNames[count++] = objectName;
66  }
67  }
68  }
69 
70  objectNames.setSize(count);
71 
72  return objectNames;
73 }
74 
75 
76 template<class Type>
78 {
79  wordList names(this->toc<Type>());
80 
81  return wordList(names, findStrings(patterns, names));
82 }
83 
84 
85 template<class Type>
87 (
88  const bool strict
89 ) const
90 {
91  HashTable<const Type*> objectsOfClass(size());
92 
94  {
95  if
96  (
97  (strict && isType<Type>(*iter()))
98  || (!strict && isA<Type>(*iter()))
99  )
100  {
101  objectsOfClass.insert
102  (
103  iter()->name(),
104  dynamic_cast<const Type*>(iter())
105  );
106  }
107  }
108 
109  return objectsOfClass;
110 }
111 
112 
113 template<class Type>
115 (
116  const bool strict
117 )
118 {
119  HashTable<Type*> objectsOfClass(size());
120 
121  forAllIter(HashTable<regIOobject*>, *this, iter)
122  {
123  if
124  (
125  (strict && isType<Type>(*iter()))
126  || (!strict && isA<Type>(*iter()))
127  )
128  {
129  objectsOfClass.insert
130  (
131  iter()->name(),
132  dynamic_cast<Type*>(iter())
133  );
134  }
135  }
136 
137  return objectsOfClass;
138 }
139 
140 
141 template<class Type>
143 {
144  const_iterator iter = find(name);
145 
146  if (iter != end())
147  {
148  const Type* vpsiPtr_ = dynamic_cast<const Type*>(iter());
149 
150  if (vpsiPtr_)
151  {
152  return true;
153  }
154  }
155  else if (this->parentNotTime())
156  {
157  return parent_.foundObject<Type>(name);
158  }
159 
160  return false;
161 }
162 
163 
164 template<class Type>
166 {
167  const_iterator iter = find(name);
168 
169  if (iter != end())
170  {
171  const Type* vpsiPtr_ = dynamic_cast<const Type*>(iter());
172 
173  if (vpsiPtr_)
174  {
175  return *vpsiPtr_;
176  }
177 
179  << nl
180  << " lookup of " << name << " from objectRegistry "
181  << this->name()
182  << " successful\n but it is not a " << Type::typeName
183  << ", it is a " << iter()->type()
184  << abort(FatalError);
185  }
186  else
187  {
188  if (this->parentNotTime())
189  {
190  return parent_.lookupObject<Type>(name);
191  }
192 
194  << nl
195  << " request for " << Type::typeName
196  << " " << name << " from objectRegistry " << this->name()
197  << " failed\n available objects of type " << Type::typeName
198  << " are" << nl
199  << toc<Type>();
200 
201  if (cacheTemporaryObject(name))
202  {
204  << nl
205  << " request for " << name << " from objectRegistry "
206  << this->name() << " to be cached failed" << nl
207  << " available temporary objects are" << nl
208  << temporaryObjects_;
209  }
210 
212  << abort(FatalError);
213  }
214 
215  return NullObjectRef<Type>();
216 }
217 
218 
219 template<class Type>
221 {
222  return const_cast<Type&>(lookupObject<Type>(name));
223 }
224 
225 
226 template<class Type>
228 {
229  return foundObject<Type>(IOobject::groupName(Type::typeName, group));
230 }
231 
232 
233 template<class Type>
234 const Type& Foam::objectRegistry::lookupType(const word& group) const
235 {
236  return lookupObject<Type>(IOobject::groupName(Type::typeName, group));
237 }
238 
239 
240 template<class Object>
242 {
243  readCacheTemporaryObjects();
244 
245  if (cacheTemporaryObjects_.size())
246  {
247  temporaryObjects_.insert(ob.name());
248 
249  HashTable<Pair<bool>>::iterator iter
250  (
251  cacheTemporaryObjects_.find(ob.name())
252  );
253 
254  // Cache object ob if is in the cacheTemporaryObjects list
255  // and hasn't been cached yet
256  if (iter != cacheTemporaryObjects_.end() && iter().first() == false)
257  {
258  iter().first() = true;
259  iter().second() = true;
260 
261  if (ob.db().template foundObject<Object>(ob.name()))
262  {
263  Object& cachedOb =
264  ob.db().template lookupObjectRef<Object>(ob.name());
265 
266  // If the object is already cached in the database delete it
267  if (&cachedOb != &ob && cachedOb.ownedByRegistry())
268  {
269  deleteCachedObject(cachedOb);
270  }
271  }
272 
273  if (debug)
274  {
275  Info<< "Caching " << ob.name()
276  << " of type " << ob.type() << endl;
277  }
278 
279  ob.release();
280  ob.checkOut();
281  store(new Object(move(ob)));
282 
283  return true;
284  }
285  else
286  {
287  return false;
288  }
289  }
290  else
291  {
292  return false;
293  }
294 }
295 
296 
297 // ************************************************************************* //
#define forAllIter(Container, container, iter)
Iterate across all elements in the container object of type.
Definition: UList.H:459
#define forAllConstIter(Container, container, iter)
Iterate across all elements in the container object of type.
Definition: UList.H:477
label size() const
Return number of elements in table.
Definition: HashTableI.H:65
bool insert(const Key &, const T &newElmt)
Insert a new hashedEntry.
Definition: HashTableI.H:80
static word groupName(Name name, const word &group)
void setSize(const label)
Reset size of List.
Definition: List.C:281
wordList toc() const
Return the list of names of the IOobjects of given type.
Type & lookupObjectRef(const word &name) const
Lookup and return the object reference of the given Type.
const Type & lookupObject(const word &name) const
Lookup and return the object of the given Type and name.
bool foundType(const word &group=word::null) const
Is the Type in registry.
bool cacheTemporaryObject(const word &name) const
Return true if given name is in the cacheTemporaryObjects set.
HashTable< const Type * > lookupClass(const bool strict=false) const
Lookup and return all objects of the given Type.
const Type & lookupType(const word &group=word::null) const
Lookup and return the object of the given Type.
bool foundObject(const word &name) const
Is the named Type in registry.
bool match(const std::string &) const
True when strings match literally.
Definition: stringI.H:194
A wordRe is a word, but can also have a regular expression for matching words.
Definition: wordRe.H:77
A class for handling words, derived from string.
Definition: word.H:62
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:334
const char *const group
Group name for atomic constants.
List< word > wordList
A List of words.
Definition: fileName.H:54
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 findStrings(const wordReListMatcher &matcher, const std::string &str)
Return true if string matches one of the regular expressions.
Definition: stringListOps.H:52
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:257
word name(const bool)
Return a word representation of a bool.
Definition: boolIO.C:39
errorManip< error > abort(error &err)
Definition: errorManip.H:131
messageStream Info
labelList first(const UList< labelPair > &p)
Definition: patchToPatch.C:39
error FatalError
label count(const ListType &l, typename ListType::const_reference x)
Count the number of occurrences of a value in a list.
static const char nl
Definition: Ostream.H:266
Operations on lists of strings.
static iteratorEnd end()
iteratorEnd set to beyond the end of any HashTable
Definition: HashTable.H:112