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-2015 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 template<class Type>
208  (
209  const word& name
210  ) const
211  {
212  const Type& ref = this->lookupObject<Type>(name);
213  // The above will already fail if things didn't work
214 
215  return const_cast<Type&>(ref);
216  }
217 // ************************************************************************* //
Foam::word
A class for handling words, derived from string.
Definition: word.H:59
forAllIter
#define forAllIter(Container, container, iter)
Iterate across all elements in the container object of type.
Definition: UList.H:431
objectRegistry.H
Foam::HashTable::insert
bool insert(const Key &, const T &newElmt)
Insert a new hashedEntry.
Definition: HashTableI.H:80
Foam::wordRe
A wordRe is a word, but can also have a regular expression for matching words.
Definition: wordRe.H:74
Foam::string::match
bool match(const std::string &) const
True when strings match literally.
Definition: stringI.H:179
forAllConstIter
forAllConstIter(PtrDictionary< phaseModel >, mixture.phases(), phase)
Definition: pEqn.H:39
Foam::wordList
List< word > wordList
A List of words.
Definition: fileName.H:54
Foam::objectRegistry::lookupClass
HashTable< const Type * > lookupClass(const bool strict=false) const
Lookup and return all objects of the given Type.
Foam::label
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
Foam::nl
static const char nl
Definition: Ostream.H:260
Foam::FatalError
error FatalError
Foam::HashTable< regIOobject * >::size
label size() const
Return number of elements in table.
Definition: HashTableI.H:65
Foam::abort
errorManip< error > abort(error &err)
Definition: errorManip.H:131
Foam::objectRegistry::lookupObjectRef
Type & lookupObjectRef(const word &name) const
Definition: objectRegistryTemplates.C:208
Foam::HashTable< regIOobject * >
Foam::List::setSize
void setSize(const label)
Reset size of List.
Foam::findStrings
bool findStrings(const wordReListMatcher &matcher, const std::string &str)
Return true if string matches one of the regular expressions.
Definition: stringListOps.H:52
Foam::objectRegistry::foundObject
bool foundObject(const word &name) const
Is the named Type found?
Definition: objectRegistryTemplates.C:142
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:318
Foam::objectRegistry::names
wordList names() const
Return the list of names of the IOobjects.
Definition: objectRegistry.C:115
Foam::List
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: HashTable.H:59
stringListOps.H
Operations on lists of strings.
Foam::objectRegistry::lookupObject
const Type & lookupObject(const word &name) const
Lookup and return the object of the given Type.
Definition: objectRegistryTemplates.C:165
Foam::name
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47