Loading...
Searching...
No Matches
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-------------------------------------------------------------------------------
11License
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
33template<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
45template<class IDLListType, class T>
47(
48 const DictionaryBase& dict
49)
50:
51 IDLListType(dict)
53 addEntries();
54}
55
56
57template<class IDLListType, class T>
58template<class INew>
60(
61 Istream& is,
62 const INew& iNew
63)
64:
65 IDLListType(is, iNew)
66{
67 addEntries();
68}
69
70
71template<class IDLListType, class T>
73:
74 IDLListType(is)
75{
77}
78
79
80// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
81
82template<class IDLListType, class T>
84{
85 return hashedTs_.contains(keyword);
86}
87
88
89template<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();
101
102 return nullptr;
103}
104
105
106template<class IDLListType, class T>
108{
109 auto iter = hashedTs_.find(keyword);
110
111 if (iter.good())
112 {
113 return iter.val();
115
116 return nullptr;
117}
118
119
120template<class IDLListType, class T>
121const 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);
131
132 return iter.val();
133}
134
135
136template<class IDLListType, class T>
138{
139 auto iter = hashedTs_.find(keyword);
140
141 if (!iter.good())
144 << "'" << keyword << "' not found"
145 << exit(FatalError);
148 return iter.val();
149}
150
151
152template<class IDLListType, class T>
154(
155 const word& keyword,
156 T* ptr
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
165template<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
178template<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
193template<class IDLListType, class T>
196 IDLListType::clear();
197 hashedTs_.clear();
198}
199
201template<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
219template<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// ************************************************************************* //
void addEntries()
Add the IDLListType entries into the HashTable.
bool contains(const word &keyword) const
Search for given keyword.
DictionaryBase()=default
Default construct: empty without allocation (capacity=0).
void transfer(DictionaryBase< IDLListType, T > &dict)
Transfer the contents of the argument into this DictionaryBase.
const T * lookup(const word &keyword) const
Find and return entry, FatalError on failure.
void push_front(const word &keyword, T *ptr)
Add to front of dictionary.
void push_back(const word &keyword, T *ptr)
Add to back of dictionary.
T * find(const word &keyword)
Find and return an entry, nullptr on failure.
HashTable< T * > hashedTs_
HashTable of the entries held on the IDLListType for quick lookup.
T * remove(const word &keyword)
Remove and return entry specified by keyword.
void clear()
Clear the dictionary.
const T * cfind(const word &keyword) const
Find and return an entry, nullptr on failure.
friend Ostream & operator(Ostream &, const DictionaryBase< IDLListType, T > &)
A helper class when constructing from an Istream or dictionary.
Definition INew.H:47
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition Istream.H:60
A class for handling words, derived from Foam::string.
Definition word.H:66
const volScalarField & T
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition error.H:600
error FatalError
Error stream (stdout output on all processes), with additional 'FOAM FATAL ERROR' header text and sta...
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition errorManip.H:125
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
dictionary dict