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-2014 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  (
180  "objectRegistry::lookupObject<Type>(const word&) const"
181  ) << nl
182  << " lookup of " << name << " from objectRegistry "
183  << this->name()
184  << " successful\n but it is not a " << Type::typeName
185  << ", it is a " << iter()->type()
186  << abort(FatalError);
187  }
188  else
189  {
190  if (this->parentNotTime())
191  {
192  return parent_.lookupObject<Type>(name);
193  }
194 
196  (
197  "objectRegistry::lookupObject<Type>(const word&) const"
198  ) << nl
199  << " request for " << Type::typeName
200  << " " << name << " from objectRegistry " << this->name()
201  << " failed\n available objects of type " << Type::typeName
202  << " are" << nl
203  << names<Type>()
204  << abort(FatalError);
205  }
206 
207  return NullObjectRef<Type>();
208 }
209 
210 
211 // ************************************************************************* //
bool foundObject(const word &name) const
Is the named Type found?
bool insert(const Key &, const T &newElmt)
Insert a new hashedEntry.
Definition: HashTableI.H:80
#define forAllIter(Container, container, iter)
Definition: UList.H:440
bool match(const std::string &, bool literalMatch=false) const
Smart match as regular expression or as a string.
Definition: wordReI.H:202
A class for handling words, derived from string.
Definition: word.H:59
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
friend class const_iterator
Declare friendship with the const_iterator.
Definition: HashTable.H:192
forAllConstIter(PtrDictionary< phaseModel >, mixture.phases(), phase)
Definition: pEqn.H:39
static const char nl
Definition: Ostream.H:260
void setSize(const label)
Reset size of List.
Definition: List.C:318
bool findStrings(const wordReListMatcher &matcher, const std::string &str)
Return true if string matches one of the regular expressions.
Definition: stringListOps.H:52
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.
errorManip< error > abort(error &err)
Definition: errorManip.H:131
const word & name() const
Return name.
Definition: IOobject.H:260
#define FatalErrorIn(functionName)
Report an error message using Foam::FatalError.
Definition: error.H:314
wordList names() const
Return the list of names of the IOobjects.
List< word > wordList
A List of words.
Definition: fileName.H:54
error FatalError
Operations on lists of strings.
static iteratorEnd end()
iteratorEnd set to beyond the end of any HashTable
Definition: HashTable.H:106
HashTable< const Type * > lookupClass(const bool strict=false) const
Lookup and return all objects of the given Type.
iterator find(const word &)
Find and return an iterator set at the hashedEntry.
A wordRe is a word, but can also have a regular expression for matching words.
Definition: wordRe.H:74