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 -------------------------------------------------------------------------------
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  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
108  (
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 
128 template<class T, class Key, class Hash>
129 template<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 
143 template<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 
159 template<class T, class Key, class Hash>
160 template<class INew>
162 {
163  this->readIstream(is, inew);
164 }
165 
166 
167 template<class T, class Key, class Hash>
169 {
170  this->readIstream(is, INew<T>());
171 }
172 
173 
174 template<class T, class Key, class Hash>
176 {
177  this->read(dict, INew<T>());
178 }
179 
180 
181 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
182 
183 template<class T, class Key, class Hash>
185 {
186  tbl.clear();
187  tbl.readIstream(is, INew<T>());
188 
189  return is;
190 }
191 
192 
193 // ************************************************************************* //
Foam::BitOps::set
void set(List< bool > &bools, const labelRange &range)
Set the specified range 'on' in a boolList.
Definition: BitOps.C:37
INew.H
Foam::glTF::key
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
Foam::FatalIOError
IOerror FatalIOError
Foam::operator>>
Istream & operator>>(Istream &, directionInfo &)
Definition: directionInfo.C:230
Foam::blockMeshTools::read
void read(Istream &, label &val, const dictionary &)
In-place read with dictionary lookup.
Definition: blockMeshTools.C:57
Foam::INew
A helper class when constructing from an Istream or dictionary.
Definition: INew.H:51
Foam::HashPtrTable::clear
void clear()
Clear all entries from table and delete any allocated pointers.
Definition: HashPtrTable.C:152
resize
patchWriters resize(patchIds.size())
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:61
Istream.H
Foam::HashPtrTable::HashPtrTable
HashPtrTable()=default
Default construct with default table capacity.
Foam::HashPtrTable::write
void write(Ostream &os) const
Invoke write() on each non-null entry.
Definition: HashPtrTableIO.C:144
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:123
os
OBJstream os(runTime.globalPath()/outputName)
T
const volScalarField & T
Definition: createFieldRefs.H:2
stdFoam::cend
constexpr auto cend(const C &c) -> decltype(c.end())
Return const_iterator to the end of the container c.
Definition: stdFoam.H:137
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
Ostream.H
Foam::HashPtrTable< exprResult >::const_iterator
typename parent_type::const_iterator const_iterator
Definition: HashPtrTable.H:89
Foam::nl
constexpr char nl
Definition: Ostream.H:404
stdFoam::cbegin
constexpr auto cbegin(const C &c) -> decltype(c.begin())
Return const_iterator to the beginning of the container c.
Definition: stdFoam.H:113
Foam::HashPtrTable
A HashTable of pointers to objects of type <T>, with deallocation management of the pointers.
Definition: HashPtrTable.H:54
HashPtrTable.H
Foam::constant::electromagnetic::e
const dimensionedScalar e
Elementary charge.
Definition: createFields.H:11
dictionary.H
FUNCTION_NAME
#define FUNCTION_NAME
Definition: messageStream.H:295
FatalIOErrorInFunction
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:473
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56