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 | Copyright (C) 2011-2015 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "HashPtrTable.H"
27 #include "Istream.H"
28 #include "Ostream.H"
29 #include "INew.H"
30 #include "dictionary.H"
31 
32 // * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * * //
33 
34 template<class T, class Key, class Hash>
35 template<class INew>
37 {
38  is.fatalCheck("HashPtrTable<T, Key, Hash>::read(Istream&, const INew&)");
39 
40  token firstToken(is);
41 
42  is.fatalCheck
43  (
44  "HashPtrTable<T, Key, Hash>::read(Istream&, const INew&) : "
45  "reading first token"
46  );
47 
48  if (firstToken.isLabel())
49  {
50  label s = firstToken.labelToken();
51 
52  // Read beginning of contents
53  char delimiter = is.readBeginList("HashPtrTable<T, Key, Hash>");
54 
55  if (s)
56  {
57  if (2*s > this->tableSize_)
58  {
59  this->resize(2*s);
60  }
61 
62  if (delimiter == token::BEGIN_LIST)
63  {
64  for (label i=0; i<s; i++)
65  {
66  Key key;
67  is >> key;
68  this->insert(key, inewt(key, is).ptr());
69 
70  is.fatalCheck
71  (
72  "HashPtrTable<T, Key, Hash>::"
73  "read(Istream&, const INew&) : reading entry"
74  );
75  }
76  }
77  else
78  {
80  (
81  is
82  ) << "incorrect first token, '(', found " << firstToken.info()
83  << exit(FatalIOError);
84  }
85  }
86 
87  // Read end of contents
88  is.readEndList("HashPtrTable");
89  }
90  else if (firstToken.isPunctuation())
91  {
92  if (firstToken.pToken() != token::BEGIN_LIST)
93  {
95  (
96  is
97  ) << "incorrect first token, '(', found " << firstToken.info()
98  << exit(FatalIOError);
99  }
100 
101  token lastToken(is);
102  while
103  (
104  !(
105  lastToken.isPunctuation()
106  && lastToken.pToken() == token::END_LIST
107  )
108  )
109  {
110  is.putBack(lastToken);
111  Key key;
112  is >> key;
113  this->insert(key, inewt(key, is).ptr());
114 
115  is.fatalCheck
116  (
117  "HashPtrTable<T, Key, Hash>::read(Istream&, const INew&) : "
118  "reading entry"
119  );
120 
121  is >> lastToken;
122  }
123  }
124  else
125  {
127  (
128  is
129  ) << "incorrect first token, expected <int> or '(', found "
130  << firstToken.info()
131  << exit(FatalIOError);
132  }
133 
134  is.fatalCheck("HashPtrTable<T, Key, Hash>::read(Istream&, const INew&)");
135 }
136 
137 
138 template<class T, class Key, class Hash>
139 template<class INew>
141 (
142  const dictionary& dict,
143  const INew& inewt
144 )
145 {
147  {
148  this->insert
149  (
150  iter().keyword(),
151  inewt(dict.subDict(iter().keyword())).ptr()
152  );
153  }
154 }
155 
156 
157 template<class T, class Key, class Hash>
159 {
160 
161  for
162  (
164  iter = this->begin();
165  iter != this->end();
166  ++iter
167  )
168  {
169  const T* ptr = iter();
170  ptr->write(os);
171  }
172 }
173 
174 
175 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
176 
177 template<class T, class Key, class Hash>
178 template<class INew>
180 {
181  this->read(is, inewt);
182 }
183 
184 
185 template<class T, class Key, class Hash>
187 {
188  this->read(is, INew<T>());
189 }
190 
191 
192 template<class T, class Key, class Hash>
194 {
195  this->read(dict, INew<T>());
196 }
197 
198 
199 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
200 
201 template<class T, class Key, class Hash>
202 Foam::Istream& Foam::operator>>(Istream& is, HashPtrTable<T, Key, Hash>& L)
203 {
204  L.clear();
205  L.read(is, INew<T>());
206 
207  return is;
208 }
209 
210 
211 template<class T, class Key, class Hash>
212 Foam::Ostream& Foam::operator<<
213 (
214  Ostream& os,
215  const HashPtrTable<T, Key, Hash>& L
216 )
217 {
218  // Write size and start delimiter
219  os << nl << L.size() << nl << token::BEGIN_LIST << nl;
220 
221  // Write contents
222  for
223  (
224  typename HashPtrTable<T, Key, Hash>::const_iterator iter = L.begin();
225  iter != L.end();
226  ++iter
227  )
228  {
229  os << iter.key() << token::SPACE << *iter() << nl;
230  }
231 
232  // Write end delimiter
233  os << token::END_LIST;
234 
235  // Check state of IOstream
236  os.check("Ostream& operator<<(Ostream&, const HashPtrTable&)");
237 
238  return os;
239 }
240 
241 
242 // ************************************************************************* //
Foam::IOstream::fatalCheck
void fatalCheck(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:105
INew.H
Foam::Istream::readBeginList
char readBeginList(const char *funcName)
Definition: Istream.C:131
Foam::read
bool read(const char *, int32_t &)
Definition: int32IO.C:87
Foam::HashPtrTable::HashPtrTable
HashPtrTable(const label size=128)
Construct given initial table size.
resize
triSurfaceToAgglom resize(surfacesMesh.size())
Foam::HashTable::const_iterator
An STL-conforming const_iterator.
Definition: HashTable.H:470
Foam::Istream::readEndList
char readEndList(const char *funcName)
Definition: Istream.C:152
Foam::FatalIOError
IOerror FatalIOError
Foam::token
A token holds items read from Istream.
Definition: token.H:67
Foam::token::isLabel
bool isLabel() const
Definition: tokenI.H:262
forAllConstIter
forAllConstIter(PtrDictionary< phaseModel >, mixture.phases(), phase)
Definition: pEqn.H:39
Foam::INew
A helper class when constructing from an Istream or dictionary.
Definition: INew.H:49
Foam::label
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
Foam::token::info
InfoProxy< token > info() const
Return info proxy.
Definition: token.H:372
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:57
Foam::HashPtrTable::read
void read(Istream &, const INew &inewt)
Read from Istream using given Istream constructor class.
Definition: HashPtrTableIO.C:36
Foam::nl
static const char nl
Definition: Ostream.H:260
Istream.H
Foam::HashPtrTable::write
void write(Ostream &os) const
Write.
Definition: HashPtrTableIO.C:158
Foam::token::pToken
punctuationToken pToken() const
Definition: tokenI.H:208
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::dictionary
A list of keyword definitions, which are a keyword followed by any number of values (e....
Definition: dictionary.H:137
s
gmvFile<< "tracers "<< particles.size()<< nl;forAllConstIter(Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().x()<< " ";}gmvFile<< nl;forAllConstIter(Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().y()<< " ";}gmvFile<< nl;forAllConstIter(Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().z()<< " ";}gmvFile<< nl;forAll(lagrangianScalarNames, i){ word name=lagrangianScalarNames[i];IOField< scalar > s(IOobject(name, runTime.timeName(), cloud::prefix, mesh, IOobject::MUST_READ, IOobject::NO_WRITE))
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
Ostream.H
T
const volScalarField & T
Definition: createFields.H:25
Foam::Istream::putBack
void putBack(const token &)
Put back token.
Definition: Istream.C:30
HashPtrTable.H
dictionary.H
Foam::operator>>
Istream & operator>>(Istream &, edgeMesh &)
Definition: edgeMeshIO.C:141
FatalIOErrorInFunction
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:330
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:53
Foam::token::isPunctuation
bool isPunctuation() const
Definition: tokenI.H:203
Foam::token::labelToken
label labelToken() const
Definition: tokenI.H:267
insert
timeIndices insert(timeIndex, timeDirs[timeI].value())
Foam::Istream::read
virtual Istream & read(token &)=0
Return next token from stream.