LongList.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | cfMesh: A library for mesh generation
4  \\ / O peration |
5  \\ / A nd | Author: Franjo Juretic (franjo.juretic@c-fields.com)
6  \\/ M anipulation | Copyright (C) Creative Fields, Ltd.
7 -------------------------------------------------------------------------------
8 License
9  This file is part of cfMesh.
10 
11  cfMesh is free software; you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by the
13  Free Software Foundation; either version 3 of the License, or (at your
14  option) any later version.
15 
16  cfMesh 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 cfMesh. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "LongList.H"
27 #include "Ostream.H"
28 #include "token.H"
29 
30 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
31 
32 template<class T, Foam::label Offset>
34 {
35  if
36  (
37  size() &&
38  token::compound::isCompound
39  (
40  "LongList<" + word(pTraits<T>::typeName) + '>'
41  )
42  )
43  {
44  os << word("LongList<" + word(pTraits<T>::typeName) + '>') << " ";
45  }
46 
47  os << *this;
48 }
49 
50 template<class T, Foam::label Offset>
52 (
53  const word& keyword,
54  Ostream& os
55 ) const
56 {
57  os.writeKeyword(keyword);
58  writeEntry(os);
59  os << token::END_STATEMENT << endl;
60 }
61 
62 template<class T, Foam::label Offset>
63 Foam::Ostream& Foam::operator<<
64 (
65  Foam::Ostream& os,
67 )
68 {
69  if( (os.format() == IOstream::ASCII) || !contiguous<T>() )
70  {
71  if( DL.size() < 15 )
72  {
73  // Write size of list and start contents delimiter
74  os << DL.size() << token::BEGIN_LIST;
75 
76  // Write list contents
77  forAll(DL, i)
78  {
79  if( i != 0 ) os << token::SPACE;
80  os << DL[i];
81  }
82 
83  // Write end of contents delimiter
84  os << token::END_LIST;
85  }
86  else
87  {
88  // Write size of list and start contents delimiter
89  os << nl << DL.size() << nl << token::BEGIN_LIST;
90 
91  // Write list contents
92  forAll(DL, i)
93  {
94  os << nl << DL[i];
95  }
96 
97  // Write end of contents delimiter
98  os << nl << token::END_LIST << nl;
99  }
100  }
101  else
102  {
103  os << nl << DL.nextFree_ << nl;
104  if( DL.nextFree_ )
105  {
106  const label blockSize = 1<<DL.shift_;
107 
108  label currBlock(0);
109  label currPos(0);
110 
111  while( currPos < DL.nextFree_ )
112  {
113  const label bs =
114  Foam::min(DL.nextFree_ - currPos, blockSize);
115 
116  os.write
117  (
118  reinterpret_cast<const char*>(DL.dataPtr_[currBlock]),
119  bs * sizeof(T)
120  );
121 
122  currPos += bs;
123  ++currBlock;
124  }
125  }
126  }
127 
128  // Check state of IOstream
129  os.check("Ostream& operator<<(Ostream&, const LongList&)");
130 
131  return os;
132 }
133 
134 
135 template<class T, Foam::label Offset>
136 Foam::Istream& Foam::operator>>
137 (
138  Foam::Istream& is,
140 )
141 {
142  // Anull list
143  DL.setSize(0);
144 
145  is.fatalCheck("operator>>(Istream&, LongList<T, Offset>&)");
146 
147  token firstToken(is);
148 
149  is.fatalCheck
150  (
151  "operator>>(Istream&, LongList<T, Offset>&) : reading first token"
152  );
153 
154  if( firstToken.isLabel() )
155  {
156  const label size = firstToken.labelToken();
157 
158  // Set list length to that read
159  DL.setSize(size);
160 
161  // Read list contents depending on data format
162  if( (is.format() == IOstream::ASCII) || !contiguous<T>() )
163  {
164  // Read beginning of contents
165  char listDelimiter = is.readBeginList("List");
166 
167  if( size == 0 )
168  {
169  if( listDelimiter != token::BEGIN_LIST )
170  {
171  WarningIn
172  (
173  "template<class T, Foam::label Offset>"
174 
175  "Foam::Istream& Foam::operator>>"
176  "("
177  "Foam::Istream& ,"
178  "Foam::LongList<T, Offset>& DL"
179  ")"
180  ) << "Missing ( after 0" << endl;
181 
182  return is;
183  }
184 
185  listDelimiter = is.readEndList("List");
186  if( listDelimiter != token::END_LIST )
187  {
188  WarningIn
189  (
190  "template<class T, Foam::label Offset>"
191 
192  "Foam::Istream& Foam::operator>>"
193  "("
194  "Foam::Istream& ,"
195  "Foam::LongList<T, Offset>& DL"
196  ")"
197  ) << "Missing ) after 0(" << endl;
198  }
199 
200  return is;
201  }
202 
203  if( listDelimiter == token::BEGIN_LIST )
204  {
205  for(label i=0;i<size;++i)
206  {
207  is >> DL[i];
208 
209  is.fatalCheck
210  (
211  "operator>>(Istream&, List<T>&) : reading entry"
212  );
213  }
214  }
215  else
216  {
217  T element;
218  is >> element;
219 
220  is.fatalCheck
221  (
222  "operator>>(Istream&, List<T>&) : "
223  "reading the single entry"
224  );
225 
226  for(label i=0;i<size;++i)
227  {
228  DL[i] = element;
229  }
230  }
231 
232  // Read end of contents
233  is.readEndList("List");
234  }
235  else
236  {
237  const label blockSize = (1<<DL.shift_);
238 
239  label currBlock(0);
240  label currPos(0);
241 
242  while( currPos < size )
243  {
244  const label bs = Foam::min(size - currPos, blockSize);
245 
246  is.read
247  (
248  reinterpret_cast<char*>(DL.dataPtr_[currBlock]),
249  bs * sizeof(T)
250  );
251 
252  currPos += bs;
253  ++currBlock;
254  }
255 
256  is.fatalCheck
257  (
258  "operator>>(Istream&, LongList<T, Offset>&)"
259  ": reading the binary block"
260  );
261  }
262  }
263  else
264  {
265  FatalIOErrorIn("operator>>(Istream&, LongList<T, Offset>&)", is)
266  << "incorrect first token, expected <int>, found "
267  << firstToken.info()
268  << exit(FatalIOError);
269  }
270 
271  return is;
272 }
273 
274 template<class T, Foam::label Offset>
276 {
277  is.fatalCheck("appendFromStream(Istream& is)");
278 
279  token firstToken(is);
280 
281  is.fatalCheck
282  (
283  "appendFromStream(Istream& is) : reading first token"
284  );
285 
286  if( firstToken.isLabel() )
287  {
288  const label size = firstToken.labelToken();
289 
290  if( size == 0 )
291  {
292  Pout << "Appending empty stream" << endl;
293  return;
294  }
295 
296  label origSize(this->size());
297 
298  // Set list length to that read
299  setSize(origSize+size);
300 
301  // Read list contents depending on data format
302  if( (is.format() == IOstream::ASCII) || !contiguous<T>() )
303  {
304  // Read beginning of contents
305  char listDelimiter = is.readBeginList("List");
306 
307  if( listDelimiter == token::BEGIN_LIST )
308  {
309  for(label i=0;i<size;++i)
310  {
311  is >> this->operator[](origSize);
312  ++origSize;
313 
314  is.fatalCheck
315  (
316  "appendFromStream(Istream& is) : reading entry"
317  );
318  }
319  }
320  else
321  {
322  T element;
323  is >> element;
324 
325  is.fatalCheck
326  (
327  "appendFromStream(Istream& is) : "
328  "reading the single entry"
329  );
330 
331  for(label i=0;i<size;++i)
332  {
333  this->operator[](origSize) = element;
334  ++origSize;
335  }
336  }
337 
338  // Read end of contents
339  is.readEndList("List");
340  }
341  else
342  {
343  List<T> buf(size);
344  is.read(reinterpret_cast<char*>(buf.begin()), size * sizeof(T));
345 
346  forAll(buf, i)
347  this->operator[](origSize++) = buf[i];
348 
349  is.fatalCheck
350  (
351  "appendFromStream(Istream& is)"
352  ": reading the binary block"
353  );
354  }
355  }
356  else
357  {
358  FatalIOErrorIn("appendFromStream(Istream& is)", is)
359  << "incorrect first token, expected <int>, found "
360  << firstToken.info()
361  << exit(FatalIOError);
362  }
363 }
364 
365 
366 // ************************************************************************* //
Foam::LongList::appendFromStream
void appendFromStream(Istream &)
Read from stream and append to the current content.
Definition: LongList.C:275
token.H
Foam::IOstream::format
streamFormat format() const
Return current stream format.
Definition: IOstream.H:377
setSize
points setSize(newPointi)
LongList.H
Foam::IOstream::fatalCheck
void fatalCheck(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:105
Foam::word
A class for handling words, derived from string.
Definition: word.H:59
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:406
Foam::Istream::readBeginList
char readBeginList(const char *funcName)
Definition: Istream.C:131
Foam::Istream::readEndList
char readEndList(const char *funcName)
Definition: Istream.C:152
Foam::FatalIOError
IOerror FatalIOError
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
Foam::token
A token holds items read from Istream.
Definition: token.H:67
Foam::token::isLabel
bool isLabel() const
Definition: tokenI.H:262
Foam::LongList
Definition: LongList.H:55
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::nl
static const char nl
Definition: Ostream.H:260
Foam::LongList::writeEntry
void writeEntry(Ostream &os) const
Write as a dictionary entry.
Definition: LongList.C:33
FatalIOErrorIn
#define FatalIOErrorIn(functionName, ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:324
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::Pout
prefixOSstream Pout(cout, "Pout")
Definition: IOstreams.H:53
Foam::List< T >
Foam::pTraits
Traits class for primitives.
Definition: pTraits.H:50
Foam::Ostream::writeKeyword
Ostream & writeKeyword(const keyType &)
Write the keyword followed by an appropriate indentation.
Definition: Ostream.C:59
WarningIn
#define WarningIn(functionName)
Report a warning using Foam::Warning.
Definition: messageStream.H:254
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:53
Foam::min
dimensioned< Type > min(const dimensioned< Type > &, const dimensioned< Type > &)
Foam::token::labelToken
label labelToken() const
Definition: tokenI.H:267
Foam::Istream::read
virtual Istream & read(token &)=0
Return next token from stream.