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 -------------------------------------------------------------------------------
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 "HashPtrTable.H"
30 #include "Istream.H"
31 #include "Ostream.H"
32 #include "INew.H"
33 #include "dictionary.H"
34 
35 // * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * * //
36 
37 template<class T, class Key, class Hash>
38 template<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  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 
125 template<class T, class Key, class Hash>
126 template<class INew>
128 (
129  const dictionary& dict,
130  const INew& inew
131 )
132 {
133  this->reserve(this->size() + dict.size());
134  for (const entry& e : dict)
135  {
136  this->set(e.keyword(), inew(e.dict()).ptr());
137  }
138 }
139 
140 
141 template<class T, class Key, class Hash>
142 void Foam::HashPtrTable<T, Key, Hash>::write(Ostream& os) const
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  }
152 }
153 
154 
155 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
156 
157 template<class T, class Key, class Hash>
158 template<class INew>
160 {
161  this->readIstream(is, inew);
162 }
163 
164 
165 template<class T, class Key, class Hash>
167 {
168  this->readIstream(is, INew<T>());
169 }
170 
171 
172 template<class T, class Key, class Hash>
174 {
175  this->read(dict, INew<T>());
176 }
177 
178 
179 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
180 
181 template<class T, class Key, class Hash>
182 Foam::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 // ************************************************************************* //
dictionary dict
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:125
A list of keyword definitions, which are a keyword followed by a number of values (eg...
Definition: dictionary.H:129
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:50
A HashTable of pointers to objects of type <T>, with deallocation management of the pointers...
Definition: HashPtrTable.H:51
void write(Ostream &os) const
Invoke write() on each non-null entry.
const dimensionedScalar e
Elementary charge.
Definition: createFields.H:11
Istream & operator>>(Istream &, directionInfo &)
constexpr auto cend(const C &c) -> decltype(c.end())
Return const_iterator to the end of the container c.
Definition: stdFoam.H:223
HashPtrTable()=default
Default construct with default table capacity.
void read(Istream &, label &val, const dictionary &)
In-place read with dictionary lookup.
OBJstream os(runTime.globalPath()/outputName)
#define FUNCTION_NAME
constexpr auto cbegin(const C &c) -> decltype(c.begin())
Return const_iterator to the beginning of the container c.
Definition: stdFoam.H:190
const volScalarField & T
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:627
auto key(const Type &t) -> typename std::enable_if< std::is_enum< Type >::value, typename std::underlying_type< Type >::type >::type
Definition: foamGltfBase.H:103
A helper class when constructing from an Istream or dictionary.
Definition: INew.H:46
triangles reserve(surf.size())
IOerror FatalIOError
Error stream (stdout output on all processes), with additional &#39;FOAM FATAL IO ERROR&#39; header text and ...