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-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 "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->names<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  << names<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 Object>
228 {
229  readCacheTemporaryObjects();
230 
231  if (cacheTemporaryObjects_.size())
232  {
233  temporaryObjects_.insert(ob.name());
234 
236  (
237  cacheTemporaryObjects_.find(ob.name())
238  );
239 
240  // Cache object ob if is in the cacheTemporaryObjects list
241  // and hasn't been cached yet
242  if (iter != cacheTemporaryObjects_.end() && iter().first() == false)
243  {
244  iter().first() = true;
245  iter().second() = true;
246 
247  if (ob.db().template foundObject<Object>(ob.name()))
248  {
249  Object& cachedOb =
250  ob.db().template lookupObjectRef<Object>(ob.name());
251 
252  // If the object is already cached in the database delete it
253  if (&cachedOb != &ob && cachedOb.ownedByRegistry())
254  {
255  deleteCachedObject(cachedOb);
256  }
257  }
258 
259  if (debug)
260  {
261  Info<< "Caching " << ob.name()
262  << " of type " << ob.type() << endl;
263  }
264 
265  ob.release();
266  ob.checkOut();
267  store(new Object(move(ob)));
268 
269  return true;
270  }
271  else
272  {
273  return false;
274  }
275  }
276  else
277  {
278  return false;
279  }
280 }
281 
282 
283 // ************************************************************************* //
bool cacheTemporaryObject(const word &name) const
Return true if given name is in the cacheTemporaryObjects set.
const word & name() const
Return name.
Definition: IOobject.H:315
static iteratorEnd end()
iteratorEnd set to beyond the end of any HashTable
Definition: HashTable.H:112
error FatalError
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:306
#define forAllConstIter(Container, container, iter)
Iterate across all elements in the container object of type.
Definition: UList.H:477
#define forAllIter(Container, container, iter)
Iterate across all elements in the container object of type.
Definition: UList.H:459
Type & lookupObjectRef(const word &name) const
Lookup and return the object reference of the given Type.
wordList names() const
Return the list of names of the IOobjects.
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
label count(const ListType &l, typename ListType::const_reference x)
Count the number of occurrences of a value in a list.
bool foundObject(const word &name) const
Is the named Type found?
Operations on lists of strings.
const Type & lookupObject(const word &name) const
Lookup and return the object of the given Type.
label size() const
Return number of elements in table.
Definition: HashTableI.H:65
bool findStrings(const wordReListMatcher &matcher, const std::string &str)
Return true if string matches one of the regular expressions.
Definition: stringListOps.H:52
bool insert(const Key &, const T &newElmt)
Insert a new hashedEntry.
Definition: HashTableI.H:80
iterator find(const word &)
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
friend class const_iterator
Declare friendship with the const_iterator.
Definition: HashTable.H:197
An STL-conforming hash table.
Definition: HashTable.H:61
errorManip< error > abort(error &err)
Definition: errorManip.H:131
A wordRe is a word, but can also have a regular expression for matching words.
Definition: wordRe.H:74
void store()
Transfer ownership of this object to its registry.
Definition: regIOobjectI.H:40
static const char nl
Definition: Ostream.H:260
List< word > wordList
A List of words.
Definition: fileName.H:54
void setSize(const label)
Reset size of List.
Definition: List.C:281
messageStream Info
friend class iterator
Declare friendship with the iterator.
Definition: HashTable.H:194
HashTable< const Type * > lookupClass(const bool strict=false) const
Lookup and return all objects of the given Type.
bool match(const std::string &, bool literalMatch=false) const
Smart match as regular expression or as a string.
Definition: wordReI.H:202