objectRegistryTemplates.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-2017 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  << abort(FatalError);
201  }
202 
203  return NullObjectRef<Type>();
204 }
205 
206 
207 template<class Type>
209 {
210  return const_cast<Type&>(lookupObject<Type>(name));
211 }
212 
213 
214 // ************************************************************************* //
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:291
static iteratorEnd end()
iteratorEnd set to beyond the end of any HashTable
Definition: HashTable.H:110
error FatalError
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:319
#define forAllIter(Container, container, iter)
Iterate across all elements in the container object of type.
Definition: UList.H:453
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.
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:62
forAllConstIter(PtrDictionary< phaseModel >, mixture.phases(), phase)
Definition: pEqn.H:29
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
static const char nl
Definition: Ostream.H:262
List< word > wordList
A List of words.
Definition: fileName.H:54
void setSize(const label)
Reset size of List.
Definition: List.C:281
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