Loading...
Searching...
No Matches
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-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 "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>
39void Foam::HashPtrTable<T, Key, Hash>::readIstream
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 this->reserve(this->size() + len);
65
66 if (delimiter == token::BEGIN_LIST)
67 {
68 for (label i=0; i<len; ++i)
69 {
70 Key key;
71 is >> key;
72 this->set(key, inew(key, is).ptr());
73
74 is.fatalCheck
75 (
76 "HashPtrTable::readIstream : "
77 "reading entry"
78 );
79 }
80 }
81 else
82 {
84 << "incorrect first token, '(', found "
85 << tok.info() << nl
86 << exit(FatalIOError);
87 }
88 }
89
90 // Read end of contents
91 is.readEndList("HashPtrTable");
92 }
93 else if (tok.isPunctuation(token::BEGIN_LIST))
94 {
95 is >> tok;
96
97 while (!tok.isPunctuation(token::END_LIST))
98 {
99 is.putBack(tok);
100 Key key;
101 is >> key;
102 this->set(key, inew(key, is).ptr());
103
104 is.fatalCheck
105 (
106 "HashPtrTable::readIstream : "
107 "reading entry"
108 );
109
110 is >> tok;
111 }
112 }
113 else
114 {
116 << "incorrect first token, expected <int> or '(', found "
117 << tok.info() << nl
118 << exit(FatalIOError);
119 }
120
121 is.fatalCheck(FUNCTION_NAME);
122}
123
124
125template<class T, class Key, class Hash>
126template<class INew>
127void Foam::HashPtrTable<T, Key, Hash>::read
128(
129 const dictionary& dict,
130 const INew& inew
131)
132{
133 this->reserve(this->size() + dict.size());
134 for (const entry& e : dict)
136 this->set(e.keyword(), inew(e.dict()).ptr());
137 }
138}
139
140
141template<class T, class Key, class Hash>
143{
144 for (const_iterator iter = this->cbegin(); iter != this->cend(); ++iter)
145 {
146 const T* ptr = iter.val();
147 if (ptr)
148 {
149 ptr->write(os);
150 }
151 }
153
154
155// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
156
157template<class T, class Key, class Hash>
158template<class INew>
160{
161 this->readIstream(is, inew);
162}
163
164
165template<class T, class Key, class Hash>
167{
168 this->readIstream(is, INew<T>());
169}
170
171
172template<class T, class Key, class Hash>
174{
175 this->read(dict, INew<T>());
176}
177
178
179// * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
180
181template<class T, class Key, class Hash>
182Foam::Istream& Foam::operator>>(Istream& is, HashPtrTable<T, Key, Hash>& tbl)
183{
184 tbl.clear();
185 tbl.readIstream(is, INew<T>());
186
187 return is;
188}
189
190
191// ************************************************************************* //
A HashTable of pointers to objects of type <T>, with deallocation management of the pointers.
constexpr HashPtrTable() noexcept=default
Default construct: empty without allocation (capacity=0).
void write(Ostream &os) const
Invoke write() on each non-null entry.
bool set(const Key &key, T *ptr)
Assign a new entry, overwrites existing.
typename parent_type::const_iterator const_iterator
void clear()
Clear all entries from table and delete any allocated pointers.
const_iterator cbegin() const
const_iterator set to the beginning of the HashTable
constexpr const_iterator cend() const noexcept
const_iterator to signal the end (for any HashTable)
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
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition Ostream.H:59
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition dictionary.H:133
const volScalarField & T
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition error.H:629
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:35
constexpr auto key(const Type &t) noexcept
Helper function to return the enum value.
bool read(const char *buf, int32_t &val)
Same as readInt32.
Definition int32.H:127
Istream & operator>>(Istream &, directionInfo &)
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)
constexpr char nl
The newline '\n' character (0x0a).
Definition Ostream.H:50
dictionary dict
triangles reserve(surf.size())
volScalarField & e