HashPtrTableIO.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2018 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>
36 void Foam::HashPtrTable<T, Key, Hash>::read(Istream& is, const INew& inewt)
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 {
146  forAllConstIter(dictionary, dict, iter)
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>
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 // ************************************************************************* //
#define forAllConstIter(Container, container, iter)
Iterate across all elements in the container object of type.
Definition: UList.H:477
A HashTable specialisation for hashing pointers.
Definition: HashPtrTable.H:67
void write(Ostream &os) const
Write.
void clear()
Clear all entries from table.
Definition: HashPtrTable.C:105
An STL-conforming const_iterator.
Definition: HashTable.H:498
iterator begin()
Iterator set to the beginning of the HashTable.
Definition: HashTableI.H:411
label size() const
Return number of elements in table.
Definition: HashTableI.H:65
friend class HashPtrTable
Declare friendship with the HashPtrTable class.
Definition: HashTable.H:184
A helper class when constructing from an Istream or dictionary.
Definition: INew.H:50
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:57
A list of keyword definitions, which are a keyword followed by any number of values (e....
Definition: dictionary.H:162
A token holds items read from Istream.
Definition: token.H:73
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:346
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.name(), cloud::prefix, mesh, IOobject::MUST_READ, IOobject::NO_WRITE))
void read(Istream &, label &, const dictionary &)
In-place read with dictionary lookup.
void insert(const scalar, DynamicList< floatScalar > &)
Append scalar to given DynamicList.
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
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
Istream & operator>>(Istream &, pistonPointEdgeData &)
IOerror FatalIOError
static const char nl
Definition: Ostream.H:266
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
dictionary dict
triSurfaceToAgglom resize(surfacesMesh.size())
static iteratorEnd end()
iteratorEnd set to beyond the end of any HashTable
Definition: HashTable.H:112