DictionaryBase.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 "DictionaryBase.H"
27 
28 // * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
29 
30 template<class IDLListType, class T>
32 {
33  for
34  (
35  typename IDLListType::iterator iter = this->begin();
36  iter != this->end();
37  ++iter
38  )
39  {
40  this->hashedTs_.insert((*iter).keyword(), &(*iter));
41  }
42 }
43 
44 
45 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
46 
47 template<class IDLListType, class T>
49 :
50  hashedTs_(size)
51 {}
52 
53 
54 template<class IDLListType, class T>
56 (
57  const DictionaryBase& dict
58 )
59 :
60  IDLListType(dict)
61 {
62  addEntries();
63 }
64 
65 
66 template<class IDLListType, class T>
68 (
69  DictionaryBase&& dict
70 )
71 :
72  IDLListType(move(dict)),
73  hashedTs_(move(dict.hashedTs_))
74 {}
75 
76 
77 template<class IDLListType, class T>
78 template<class INew>
80 (
81  Istream& is,
82  const INew& iNew
83 )
84 :
85  IDLListType(is, iNew)
86 {
87  addEntries();
88 }
89 
90 
91 template<class IDLListType, class T>
93 :
94  IDLListType(is)
95 {
96  addEntries();
97 }
98 
99 
100 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
101 
102 template<class IDLListType, class T>
104 {
105  return hashedTs_.found(keyword);
106 }
107 
108 
109 template<class IDLListType, class T>
111 (
112  const word& keyword
113 ) const
114 {
115  typename HashTable<T*>::const_iterator iter = hashedTs_.find(keyword);
116 
117  if (iter != hashedTs_.end())
118  {
119  return *iter;
120  }
121  else
122  {
123  return nullptr;
124  }
125 }
126 
127 
128 template<class IDLListType, class T>
130 {
131  typename HashTable<T*>::iterator iter = hashedTs_.find(keyword);
132 
133  if (iter != hashedTs_.end())
134  {
135  return *iter;
136  }
137  else
138  {
139  return nullptr;
140  }
141 }
142 
143 
144 template<class IDLListType, class T>
146 {
147  typename HashTable<T*>::const_iterator iter = hashedTs_.find(keyword);
148 
149  if (iter == hashedTs_.end())
150  {
152  << keyword << " is undefined"
153  << exit(FatalError);
154  }
155 
156  return *iter;
157 }
158 
159 
160 template<class IDLListType, class T>
162 {
163  typename HashTable<T*>::iterator iter = hashedTs_.find(keyword);
164 
165  if (iter == hashedTs_.end())
166  {
168  << keyword << " is undefined"
169  << exit(FatalError);
170  }
171 
172  return *iter;
173 }
174 
175 
176 template<class IDLListType, class T>
178 {
179  wordList keywords(this->size());
180 
181  label i = 0;
182  for
183  (
184  typename IDLListType::const_iterator iter = this->begin();
185  iter != this->end();
186  ++iter
187  )
188  {
189  keywords[i++] = iter().keyword();
190  }
191 
192  return keywords;
193 }
194 
195 
196 template<class IDLListType, class T>
198 {
199  return hashedTs_.sortedToc();
200 }
201 
202 
203 template<class IDLListType, class T>
205 {
206  // NOTE: we should probably check that HashTable::insert actually worked
207  hashedTs_.insert(keyword, tPtr);
208  IDLListType::insert(tPtr);
209 }
210 
211 
212 template<class IDLListType, class T>
214 {
215  // NOTE: we should probably check that HashTable::insert actually worked
216  hashedTs_.insert(keyword, tPtr);
217  IDLListType::append(tPtr);
218 }
219 
220 
221 template<class IDLListType, class T>
223 {
224  typename HashTable<T*>::iterator iter = hashedTs_.find(keyword);
225 
226  if (iter != hashedTs_.end())
227  {
228  T* tPtr = IDLListType::remove(iter());
229  hashedTs_.erase(iter);
230  return tPtr;
231  }
232  else
233  {
234  return nullptr;
235  }
236 }
237 
238 
239 template<class IDLListType, class T>
241 {
243  hashedTs_.clear();
244 }
245 
246 
247 template<class IDLListType, class T>
249 (
251 )
252 {
253  IDLListType::transfer(dict);
254  hashedTs_.transfer(dict.hashedTs_);
255 }
256 
257 
258 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
259 
260 template<class IDLListType, class T>
261 void Foam::DictionaryBase<IDLListType, T>::operator=
262 (
264 )
265 {
266  // Check for assignment to self
267  if (this == &dict)
268  {
270  << "attempted assignment to self"
271  << abort(FatalError);
272  }
273 
274  IDLListType::operator=(dict);
275  this->hashedTs_.clear();
276  this->addEntries();
277 }
278 
279 
280 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
281 
282 #include "DictionaryBaseIO.C"
283 
284 // ************************************************************************* //
dictionary dict
tUEqn clear()
bool found(const word &) const
Search DictionaryBase for given keyword.
An STL-conforming const_iterator.
Definition: HashTable.H:481
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
Base dictionary class templated on both the form of doubly-linked list it uses as well as the type it...
error FatalError
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:306
Reads the data description and data portions of a DictionaryBase File.
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
wordList sortedToc() const
Return the table of contents as a sorted list.
const T * lookupPtr(const word &) const
Find and return an entry if present, otherwise return nullptr.
wordList toc() const
Return the table of contents.
void append(const word &, T *)
Add at tail of dictionary.
DictionaryBase(const label size=128)
Construct given initial table size.
void insert(const scalar, DynamicList< floatScalar > &)
Append scalar to given DynamicList.
void insert(const word &, T *)
Add at head of dictionary.
An STL-conforming iterator.
Definition: HashTable.H:426
A class for handling words, derived from string.
Definition: word.H:59
HashTable< T * > hashedTs_
HashTable of the entries held on the IDLListType for quick lookup.
void clear()
Clear the dictionary.
errorManip< error > abort(error &err)
Definition: errorManip.H:131
T * remove(const word &)
Remove and return entry specified by keyword.
const volScalarField & T
void transfer(DictionaryBase< IDLListType, T > &)
Transfer the contents of the argument into this DictionaryBase.
const T * lookup(const word &) const
Find and return entry.
A helper class when constructing from an Istream or dictionary.
Definition: INew.H:49