FreeFOAM The Cross-Platform CFD Toolkit
DictionaryBase.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) 1991-2010 OpenCFD Ltd.
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 // * * * * * * * * * * * * * Private 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 
51 
52 template<class IDLListType, class T>
54 (
55  const DictionaryBase& dict
56 )
57 :
58  IDLListType(dict)
59 {
60  addEntries();
61 }
62 
63 
64 template<class IDLListType, class T>
65 template<class INew>
67 (
68  Istream& is,
69  const INew& iNew
70 )
71 :
72  IDLListType(is, iNew)
73 {
74  addEntries();
75 }
76 
77 
78 template<class IDLListType, class T>
80 :
81  IDLListType(is)
82 {
83  addEntries();
84 }
85 
86 
87 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
88 
89 // Find and return T
90 template<class IDLListType, class T>
92 {
93  return hashedTs_.found(keyword);
94 }
95 
96 
97 // Find and return T*, return NULL if not found
98 template<class IDLListType, class T>
100 (
101  const word& keyword
102 ) const
103 {
104  typename HashTable<T*>::const_iterator iter = hashedTs_.find(keyword);
105 
106  if (iter != hashedTs_.end())
107  {
108  return *iter;
109  }
110  else
111  {
112  return NULL;
113  }
114 }
115 
116 
117 // Find and return T*, return NULL if not found
118 template<class IDLListType, class T>
120 {
121  typename HashTable<T*>::iterator iter = hashedTs_.find(keyword);
122 
123  if (iter != hashedTs_.end())
124  {
125  return *iter;
126  }
127  else
128  {
129  return NULL;
130  }
131 }
132 
133 
134 // Find and return T*, FatalError if keyword not found
135 template<class IDLListType, class T>
137 {
138  typename HashTable<T*>::const_iterator iter = hashedTs_.find(keyword);
139 
140  if (iter == hashedTs_.end())
141  {
143  (
144  "DictionaryBase<IDLListType, T>::lookup(const word&) const"
145  ) << keyword << " is undefined"
146  << exit(FatalError);
147  }
148 
149  return *iter;
150 }
151 
152 
153 // Find and return T*, FatalError if keyword not found
154 template<class IDLListType, class T>
156 {
157  typename HashTable<T*>::iterator iter = hashedTs_.find(keyword);
158 
159  if (iter == hashedTs_.end())
160  {
162  (
163  "DictionaryBase<IDLListType, T>::lookup(const word&)"
164  ) << keyword << " is undefined"
165  << exit(FatalError);
166  }
167 
168  return *iter;
169 }
170 
171 
172 // Return the table of contents
173 template<class IDLListType, class T>
175 {
176  wordList keywords(this->size());
177 
178  label i = 0;
179  for
180  (
181  typename IDLListType::const_iterator iter = this->begin();
182  iter != this->end();
183  ++iter
184  )
185  {
186  keywords[i++] = iter().keyword();
187  }
188 
189  return keywords;
190 }
191 
192 
193 // Add at head of dictionary
194 template<class IDLListType, class T>
196 {
197  // NOTE: we should probably check that HashTable::insert actually worked
198  hashedTs_.insert(keyword, tPtr);
199  IDLListType::insert(tPtr);
200 }
201 
202 
203 // Add at tail of dictionary
204 template<class IDLListType, class T>
206 {
207  // NOTE: we should probably check that HashTable::insert actually worked
208  hashedTs_.insert(keyword, tPtr);
209  IDLListType::append(tPtr);
210 }
211 
212 
213 template<class IDLListType, class T>
215 {
216  typename HashTable<T*>::iterator iter = hashedTs_.find(keyword);
217 
218  if (iter != hashedTs_.end())
219  {
220  T* tPtr = IDLListType::remove(iter());
221  hashedTs_.erase(iter);
222  return tPtr;
223  }
224  else
225  {
226  return NULL;
227  }
228 }
229 
230 
231 template<class IDLListType, class T>
233 {
235  hashedTs_.clear();
236 }
237 
238 
239 template<class IDLListType, class T>
241 (
243 )
244 {
245  IDLListType::transfer(dict);
246  hashedTs_.transfer(dict.hashedTs_);
247 }
248 
249 
250 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
251 
252 template<class IDLListType, class T>
254 (
256 )
257 {
258  // Check for assignment to self
259  if (this == &dict)
260  {
261  FatalErrorIn("DictionaryBase::operator=(const DictionaryBase&)")
262  << "attempted assignment to self"
263  << abort(FatalError);
264  }
265 
266  IDLListType::operator=(dict);
267  this->hashedTs_.clear();
268  this->addEntries();
269 }
270 
271 
272 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
273 
274 #include "DictionaryBaseIO.C"
275 
276 // ************************ vim: set sw=4 sts=4 et: ************************ //