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-2019 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 firstToken(is);
48 
49  is.fatalCheck
50  (
51  "HashPtrTable::readIstream : "
52  "reading first token"
53  );
54 
55  if (firstToken.isLabel())
56  {
57  const label len = firstToken.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 " << firstToken.info()
88  << exit(FatalIOError);
89  }
90  }
91 
92  // Read end of contents
93  is.readEndList("HashPtrTable");
94  }
95  else if (firstToken.isPunctuation())
96  {
97  if (firstToken.pToken() != token::BEGIN_LIST)
98  {
100  << "incorrect first token, '(', found " << firstToken.info()
101  << exit(FatalIOError);
102  }
103 
104  token lastToken(is);
105  while
106  (
107  !(
108  lastToken.isPunctuation()
109  && lastToken.pToken() == token::END_LIST
110  )
111  )
112  {
113  is.putBack(lastToken);
114  Key key;
115  is >> key;
116  this->set(key, inew(key, is).ptr());
117 
118  is.fatalCheck
119  (
120  "HashPtrTable::readIstream : "
121  "reading entry"
122  );
123 
124  is >> lastToken;
125  }
126  }
127  else
128  {
130  << "incorrect first token, expected <int> or '(', found "
131  << firstToken.info()
132  << exit(FatalIOError);
133  }
134 
135  is.fatalCheck(FUNCTION_NAME);
136 }
137 
138 
139 template<class T, class Key, class Hash>
140 template<class INew>
142 (
143  const dictionary& dict,
144  const INew& inew
145 )
146 {
147  for (const entry& e : dict)
148  {
149  this->set(e.keyword(), inew(e.dict()).ptr());
150  }
151 }
152 
153 
154 template<class T, class Key, class Hash>
156 {
157  for (const_iterator iter = this->cbegin(); iter != this->cend(); ++iter)
158  {
159  const T* ptr = iter.val();
160  if (ptr)
161  {
162  ptr->write(os);
163  }
164  }
165 }
166 
167 
168 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
169 
170 template<class T, class Key, class Hash>
171 template<class INew>
173 {
174  this->readIstream(is, inew);
175 }
176 
177 
178 template<class T, class Key, class Hash>
180 {
181  this->readIstream(is, INew<T>());
182 }
183 
184 
185 template<class T, class Key, class Hash>
187 {
188  this->read(dict, INew<T>());
189 }
190 
191 
192 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
193 
194 template<class T, class Key, class Hash>
196 {
197  tbl.clear();
198  tbl.readIstream(is, INew<T>());
199 
200  return is;
201 }
202 
203 
204 // ************************************************************************* //
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::FatalIOError
IOerror FatalIOError
Foam::operator>>
Istream & operator>>(Istream &, directionInfo &)
Definition: directionInfo.C:230
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:129
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::write
void write(Ostream &os) const
Write.
Definition: HashPtrTableIO.C:155
Foam::blockMeshTools::read
void read(Istream &, label &, const dictionary &)
In-place read with dictionary lookup.
Definition: blockMeshTools.C:33
Foam::HashPtrTable::HashPtrTable
HashPtrTable()
Default construct with default table capacity.
Definition: HashPtrTableI.H:33
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:121
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
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:270
FatalIOErrorInFunction
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:401
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56