HashPtrTableIO.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-2015 OpenFOAM Foundation
9 Copyright (C) 2017-2021 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 "HashPtrTable.H"
30#include "Istream.H"
31#include "Ostream.H"
32#include "INew.H"
33#include "dictionary.H"
34
35// * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * * //
36
37template<class T, class Key, class Hash>
38template<class INew>
40(
41 Istream& is,
42 const INew& inew
43)
44{
45 is.fatalCheck(FUNCTION_NAME);
46
47 token tok(is);
48
49 is.fatalCheck
50 (
51 "HashPtrTable::readIstream : "
52 "reading first token"
53 );
54
55 if (tok.isLabel())
56 {
57 const label len = tok.labelToken();
58
59 // Read beginning of contents
60 const char delimiter = is.readBeginList("HashPtrTable");
61
62 if (len)
63 {
64 if (2*len > this->capacity())
65 {
66 this->resize(2*len);
67 }
68
69 if (delimiter == token::BEGIN_LIST)
70 {
71 for (label i=0; i<len; ++i)
72 {
73 Key key;
74 is >> key;
75 this->set(key, inew(key, is).ptr());
76
77 is.fatalCheck
78 (
79 "HashPtrTable::readIstream : "
80 "reading entry"
81 );
82 }
83 }
84 else
85 {
87 << "incorrect first token, '(', found "
88 << tok.info() << nl
89 << exit(FatalIOError);
90 }
91 }
92
93 // Read end of contents
94 is.readEndList("HashPtrTable");
95 }
96 else if (tok.isPunctuation(token::BEGIN_LIST))
97 {
98 is >> tok;
99
100 while (!tok.isPunctuation(token::END_LIST))
101 {
102 is.putBack(tok);
103 Key key;
104 is >> key;
105 this->set(key, inew(key, is).ptr());
106
107 is.fatalCheck
109 "HashPtrTable::readIstream : "
110 "reading entry"
111 );
112
113 is >> tok;
114 }
115 }
116 else
117 {
119 << "incorrect first token, expected <int> or '(', found "
120 << tok.info() << nl
121 << exit(FatalIOError);
122 }
123
124 is.fatalCheck(FUNCTION_NAME);
125}
126
127
128template<class T, class Key, class Hash>
129template<class INew>
131(
132 const dictionary& dict,
133 const INew& inew
134)
135{
136 for (const entry& e : dict)
137 {
138 this->set(e.keyword(), inew(e.dict()).ptr());
139 }
140}
141
142
143template<class T, class Key, class Hash>
145{
146 for (const_iterator iter = this->cbegin(); iter != this->cend(); ++iter)
147 {
148 const T* ptr = iter.val();
149 if (ptr)
150 {
151 ptr->write(os);
152 }
153 }
154}
155
156
157// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
158
159template<class T, class Key, class Hash>
160template<class INew>
162{
163 this->readIstream(is, inew);
164}
165
166
167template<class T, class Key, class Hash>
169{
170 this->readIstream(is, INew<T>());
171}
172
173
174template<class T, class Key, class Hash>
176{
177 this->read(dict, INew<T>());
178}
179
180
181// * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
182
183template<class T, class Key, class Hash>
185{
186 tbl.clear();
187 tbl.readIstream(is, INew<T>());
188
189 return is;
190}
191
192
193// ************************************************************************* //
A HashTable of pointers to objects of type <T>, with deallocation management of the pointers.
Definition: HashPtrTable.H:68
HashPtrTable()=default
Default construct with default table capacity.
typename parent_type::const_iterator const_iterator
Definition: HashPtrTable.H:89
void clear()
Clear all entries from table and delete any allocated pointers.
Definition: HashPtrTable.C:152
A helper class when constructing from an Istream or dictionary.
Definition: INew.H:52
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:64
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:62
virtual bool read()
Re-read model coefficients if they have changed.
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:126
virtual bool write()
Write the output fields.
const volScalarField & T
patchWriters resize(patchIds.size())
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:473
OBJstream os(runTime.globalPath()/outputName)
#define FUNCTION_NAME
void set(List< bool > &bools, const labelUList &locations)
Set the listed locations (assign 'true').
Definition: BitOps.C:38
auto key(const Type &t) -> typename std::enable_if< std::is_enum< Type >::value, typename std::underlying_type< Type >::type >::type
Definition: foamGltfBase.H:108
Istream & operator>>(Istream &, directionInfo &)
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
constexpr char nl
The newline '\n' character (0x0a)
Definition: Ostream.H:53
dictionary dict
volScalarField & e
Definition: createFields.H:11