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-2023 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  const DictionaryBase& dict
49 )
50 :
51  IDLListType(dict)
52 {
53  addEntries();
54 }
55 
56 
57 template<class IDLListType, class T>
58 template<class INew>
60 (
61  Istream& is,
62  const INew& iNew
63 )
64 :
65  IDLListType(is, iNew)
66 {
67  addEntries();
68 }
69 
70 
71 template<class IDLListType, class T>
73 :
74  IDLListType(is)
75 {
76  addEntries();
77 }
78 
79 
80 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
81 
82 template<class IDLListType, class T>
84 {
85  return hashedTs_.contains(keyword);
86 }
87 
88 
89 template<class IDLListType, class T>
91 (
92  const word& keyword
93 ) const
94 {
95  const auto iter = hashedTs_.cfind(keyword);
96 
97  if (iter.good())
98  {
99  return iter.val();
100  }
101 
102  return nullptr;
103 }
104 
105 
106 template<class IDLListType, class T>
107 T* Foam::DictionaryBase<IDLListType, T>::find(const word& keyword)
108 {
109  auto iter = hashedTs_.find(keyword);
110 
111  if (iter.good())
112  {
113  return iter.val();
114  }
115 
116  return nullptr;
117 }
118 
119 
120 template<class IDLListType, class T>
121 const T* Foam::DictionaryBase<IDLListType, T>::lookup(const word& keyword) const
122 {
123  const auto iter = hashedTs_.cfind(keyword);
124 
125  if (!iter.good())
126  {
128  << "'" << keyword << "' not found"
129  << exit(FatalError);
130  }
131 
132  return iter.val();
133 }
134 
135 
136 template<class IDLListType, class T>
138 {
139  auto iter = hashedTs_.find(keyword);
140 
141  if (!iter.good())
142  {
144  << "'" << keyword << "' not found"
145  << exit(FatalError);
146  }
148  return iter.val();
149 }
150 
151 
152 template<class IDLListType, class T>
154 (
155  const word& keyword,
156  T* ptr
157 )
158 {
159  IDLListType::push_front(ptr);
160  // NOTE: we should probably check that HashTable::insert actually worked
161  hashedTs_.insert(keyword, ptr);
162 }
163 
164 
165 template<class IDLListType, class T>
167 (
168  const word& keyword,
169  T* ptr
170 )
171 {
172  // NOTE: we should probably check that HashTable::insert actually worked
173  hashedTs_.insert(keyword, ptr);
174  IDLListType::push_back(ptr);
175 }
176 
177 
178 template<class IDLListType, class T>
180 {
181  T* ptr = nullptr;
182  auto iter = hashedTs_.find(keyword);
183 
184  if (iter.good())
185  {
186  ptr = IDLListType::remove(iter.val());
187  hashedTs_.erase(iter);
188  }
189  return ptr;
190 }
191 
192 
193 template<class IDLListType, class T>
195 {
197  hashedTs_.clear();
198 }
199 
200 
201 template<class IDLListType, class T>
203 (
205 )
206 {
207  if (this == &dict)
208  {
209  return; // Self-assignment is a no-op
210  }
211 
212  IDLListType::transfer(dict);
213  hashedTs_.transfer(dict.hashedTs_);
214 }
215 
216 
217 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
218 
219 template<class IDLListType, class T>
221 (
223 )
224 {
225  if (this == &dict)
226  {
227  return; // Self-assignment is a no-op
228  }
229 
230  IDLListType::operator=(dict);
231  this->hashedTs_.clear();
232  this->addEntries();
233 }
234 
235 
236 // ************************************************************************* //
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:598
void addEntries()
Add the IDLListType entries into the HashTable.
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
void transfer(DictionaryBase< IDLListType, T > &dict)
Transfer the contents of the argument into this DictionaryBase.
bool contains(const word &keyword) const
Search for given keyword.
T * remove(const word &keyword)
Remove and return entry specified by keyword.
DictionaryBase()=default
Default construct: empty without allocation (capacity=0).
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:201
const volScalarField & T
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:168