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 | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2011-2016 OpenFOAM Foundation
9  Copyright (C) 2019-2022 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 \*---------------------------------------------------------------------------*/
28 
29 #include "DictionaryBase.H"
30 
31 // * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
32 
33 template<class IDLListType, class T>
35 {
36  for (auto iter = this->begin(); iter != this->end(); ++iter)
37  {
38  this->hashedTs_.insert((*iter).keyword(), &(*iter));
39  }
40 }
41 
42 
43 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
44 
45 template<class IDLListType, class T>
47 :
48  hashedTs_(size)
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 template<class IDLListType, class T>
90 bool Foam::DictionaryBase<IDLListType, T>::found(const word& keyword) const
91 {
92  return hashedTs_.found(keyword);
93 }
94 
95 
96 template<class IDLListType, class T>
98 (
99  const word& keyword
100 ) const
101 {
102  const auto iter = hashedTs_.cfind(keyword);
103 
104  if (iter.found())
105  {
106  return *iter;
107  }
108 
109  return nullptr;
110 }
111 
112 
113 template<class IDLListType, class T>
114 T* Foam::DictionaryBase<IDLListType, T>::find(const word& keyword)
115 {
116  auto iter = hashedTs_.find(keyword);
117 
118  if (iter.found())
119  {
120  return *iter;
121  }
122 
123  return nullptr;
124 }
125 
126 
127 template<class IDLListType, class T>
128 const T* Foam::DictionaryBase<IDLListType, T>::lookup(const word& keyword) const
129 {
130  const auto iter = hashedTs_.cfind(keyword);
131 
132  if (!iter.found())
133  {
135  << "'" << keyword << "' not found"
136  << exit(FatalError);
137  }
138 
139  return *iter;
140 }
141 
142 
143 template<class IDLListType, class T>
145 {
146  auto iter = hashedTs_.find(keyword);
147 
148  if (!iter.found())
149  {
151  << "'" << keyword << "' not found"
152  << exit(FatalError);
153  }
154 
155  return *iter;
156 }
157 
158 
159 template<class IDLListType, class T>
161 {
162  // Cannot rely on the items themselves having a keyword() method
163  // so simply return the toc() from the hashed entries
164  // Make it sorted, since anything else would have no meaning.
165  return hashedTs_.sortedToc();
166 }
167 
168 
169 template<class IDLListType, class T>
171 {
172  return hashedTs_.sortedToc();
173 }
174 
175 
176 template<class IDLListType, class T>
177 template<class Compare>
179 (
180  const Compare& comp
181 ) const
182 {
183  return hashedTs_.sortedToc(comp);
184 }
185 
186 
187 template<class IDLListType, class T>
189 (
190  const word& keyword,
191  T* ptr
192 )
193 {
194  // NOTE: we should probably check that HashTable::insert actually worked
195  hashedTs_.insert(keyword, ptr);
196  IDLListType::push_front(ptr);
197 }
198 
199 
200 template<class IDLListType, class T>
202 (
203  const word& keyword,
204  T* ptr
205 )
206 {
207  // NOTE: we should probably check that HashTable::insert actually worked
208  hashedTs_.insert(keyword, ptr);
209  IDLListType::push_back(ptr);
210 }
211 
212 
213 template<class IDLListType, class T>
215 {
216  auto iter = hashedTs_.find(keyword);
217 
218  if (iter.found())
219  {
220  T* ptr = IDLListType::remove(iter());
221  hashedTs_.erase(iter);
222  return ptr;
223  }
224 
225  return nullptr;
226 }
227 
228 
229 template<class IDLListType, class T>
231 {
233  hashedTs_.clear();
234 }
235 
236 
237 template<class IDLListType, class T>
239 (
241 )
242 {
243  if (this == &dict)
244  {
245  return; // Self-assignment is a no-op
246  }
247 
248  IDLListType::transfer(dict);
249  hashedTs_.transfer(dict.hashedTs_);
250 }
251 
252 
253 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
254 
255 template<class IDLListType, class T>
257 (
259 )
260 {
261  if (this == &dict)
262  {
263  return; // Self-assignment is a no-op
264  }
265 
266  IDLListType::operator=(dict);
267  this->hashedTs_.clear();
268  this->addEntries();
269 }
270 
271 
272 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
273 
274 #include "DictionaryBaseIO.C"
275 
276 // ************************************************************************* //
dictionary dict
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:125
Base dictionary class templated on both the form of doubly-linked list it uses as well as the type it...
error FatalError
Error stream (stdout output on all processes), with additional &#39;FOAM FATAL ERROR&#39; header text and sta...
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:578
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.
wordList toc() const
Return the table of contents (as a sorted list)
void transfer(DictionaryBase< IDLListType, T > &dict)
Transfer the contents of the argument into this DictionaryBase.
DictionaryBase(const label size=128)
Construct with given or default (128) table capacity.
T * remove(const word &keyword)
Remove and return entry specified by keyword.
A class for handling words, derived from Foam::string.
Definition: word.H:63
void clear()
Clear the dictionary.
patchWriters clear()
void push_back(const word &keyword, T *ptr)
Add to back of dictionary.
constexpr auto end(C &c) -> decltype(c.end())
Return iterator to the end of the container c.
Definition: stdFoam.H:193
const volScalarField & T
bool found(const word &keyword) const
Search for given keyword.
const T * lookup(const word &keyword) const
Find and return entry, FatalError on failure.
const T * cfind(const word &keyword) const
Find and return an entry, nullptr on failure.
A helper class when constructing from an Istream or dictionary.
Definition: INew.H:46
T * find(const word &keyword)
Find and return an entry, nullptr on failure.
void push_front(const word &keyword, T *ptr)
Add to front of dictionary.
constexpr auto begin(C &c) -> decltype(c.begin())
Return iterator to the beginning of the container c.
Definition: stdFoam.H:160