UListIO.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-2019 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 "UList.H"
27 #include "Ostream.H"
28 #include "token.H"
29 #include "SLList.H"
30 #include "contiguous.H"
31 
32 // * * * * * * * * * * * * * * * IOstream Functions * * * * * * * * * * * * //
33 
34 template<class ListType>
35 void Foam::writeListEntry(Ostream& os, const ListType& l)
36 {
37  if
38  (
39  l.size()
40  && token::compound::isCompound
41  (
42  "List<"
44  )
45  )
46  {
47  os << word
48  (
49  "List<"
51  ) << " ";
52  }
53 
54  os << l;
55 }
56 
57 
58 template<class ListType>
59 void Foam::writeListEntries(Ostream& os, const ListType& l)
60 {
61  // Write size and start delimiter
62  os << nl << l.size() << nl << token::BEGIN_LIST;
63 
64  // Write contents
65  forAll(l, i)
66  {
67  writeEntry(os, l[i]);
68  os << nl;
69  }
70 
71  // Write end delimiter
72  os << nl << token::END_LIST << nl;
73 }
74 
75 
76 template<class ListType>
77 void Foam::writeListEntries(Ostream& os, const word& keyword, const ListType& l)
78 {
79  os.writeKeyword(keyword);
80  writeListEntries(os, l);
81  os << token::END_STATEMENT << endl;
82 }
83 
84 
85 template<class T>
86 void Foam::writeEntry(Ostream& os, const UList<T>& l)
87 {
88  writeListEntry(os, l);
89 }
90 
91 
92 // * * * * * * * * * * * * * * * Ostream Operator * * * * * * * * * * * * * //
93 
94 template<class T>
95 Foam::Ostream& Foam::operator<<(Foam::Ostream& os, const Foam::UList<T>& L)
96 {
97  // Write list contents depending on data format
98  if (os.format() == IOstream::ASCII || !contiguous<T>())
99  {
100  bool uniform = false;
101 
102  if (L.size() > 1 && contiguous<T>())
103  {
104  uniform = true;
105 
106  forAll(L, i)
107  {
108  if (L[i] != L[0])
109  {
110  uniform = false;
111  break;
112  }
113  }
114  }
115 
116  if (uniform)
117  {
118  // Write size and start delimiter
119  os << L.size() << token::BEGIN_BLOCK;
120 
121  // Write contents
122  os << L[0];
123 
124  // Write end delimiter
125  os << token::END_BLOCK;
126  }
127  else if (L.size() <= 1 || (L.size() < 11 && contiguous<T>()))
128  {
129  // Write size and start delimiter
130  os << L.size() << token::BEGIN_LIST;
131 
132  // Write contents
133  forAll(L, i)
134  {
135  if (i > 0) os << token::SPACE;
136  os << L[i];
137  }
138 
139  // Write end delimiter
140  os << token::END_LIST;
141  }
142  else
143  {
144  // Write size and start delimiter
145  os << nl << L.size() << nl << token::BEGIN_LIST;
146 
147  // Write contents
148  forAll(L, i)
149  {
150  os << nl << L[i];
151  }
152 
153  // Write end delimiter
154  os << nl << token::END_LIST << nl;
155  }
156  }
157  else
158  {
159  os << nl << L.size() << nl;
160  if (L.size())
161  {
162  os.write(reinterpret_cast<const char*>(L.v_), L.byteSize());
163  }
164  }
165 
166  // Check state of IOstream
167  os.check("Ostream& operator<<(Ostream&, const UList&)");
168 
169  return os;
170 }
171 
172 
173 template<class T>
175 {
176  is.fatalCheck("operator>>(Istream&, UList<T>&)");
177 
178  token firstToken(is);
179 
180  is.fatalCheck("operator>>(Istream&, UList<T>&) : reading first token");
181 
182  if (firstToken.isCompound())
183  {
184  List<T> elems;
185  elems.transfer
186  (
188  (
189  firstToken.transferCompoundToken(is)
190  )
191  );
192  // Check list length
193  label s = elems.size();
194 
195  if (s != L.size())
196  {
198  << "incorrect length for UList. Read " << s
199  << " expected " << L.size()
200  << exit(FatalIOError);
201  }
202  for (label i=0; i<s; i++)
203  {
204  L[i] = elems[i];
205  }
206  }
207  else if (firstToken.isLabel())
208  {
209  label s = firstToken.labelToken();
210 
211  // Set list length to that read
212  if (s != L.size())
213  {
215  << "incorrect length for UList. Read " << s
216  << " expected " << L.size()
217  << exit(FatalIOError);
218  }
219 
220  // Read list contents depending on data format
221 
222  if (is.format() == IOstream::ASCII || !contiguous<T>())
223  {
224  // Read beginning of contents
225  char delimiter = is.readBeginList("List");
226 
227  if (s)
228  {
229  if (delimiter == token::BEGIN_LIST)
230  {
231  for (label i=0; i<s; i++)
232  {
233  is >> L[i];
234 
235  is.fatalCheck
236  (
237  "operator>>(Istream&, UList<T>&) : reading entry"
238  );
239  }
240  }
241  else
242  {
243  T element;
244  is >> element;
245 
246  is.fatalCheck
247  (
248  "operator>>(Istream&, UList<T>&) : "
249  "reading the single entry"
250  );
251 
252  for (label i=0; i<s; i++)
253  {
254  L[i] = element;
255  }
256  }
257  }
258 
259  // Read end of contents
260  is.readEndList("List");
261  }
262  else
263  {
264  if (s)
265  {
266  is.read(reinterpret_cast<char*>(L.data()), s*sizeof(T));
267 
268  is.fatalCheck
269  (
270  "operator>>(Istream&, UList<T>&) : reading the binary block"
271  );
272  }
273  }
274  }
275  else if (firstToken.isPunctuation())
276  {
277  if (firstToken.pToken() != token::BEGIN_LIST)
278  {
280  << "incorrect first token, expected '(', found "
281  << firstToken.info()
282  << exit(FatalIOError);
283  }
284 
285  // Putback the opening bracket
286  is.putBack(firstToken);
287 
288  // Now read as a singly-linked list
289  SLList<T> sll(is);
290 
291  if (sll.size() != L.size())
292  {
294  << "incorrect length for UList. Read " << sll.size()
295  << " expected " << L.size()
296  << exit(FatalIOError);
297  }
298 
299  // Convert the singly-linked list to this list
300  label i = 0;
301  for
302  (
303  typename SLList<T>::const_iterator iter = sll.begin();
304  iter != sll.end();
305  ++iter
306  )
307  {
308  L[i] = iter();
309  }
310 
311  }
312  else
313  {
315  << "incorrect first token, expected <int> or '(', found "
316  << firstToken.info()
317  << exit(FatalIOError);
318  }
319 
320  return is;
321 }
322 
323 
324 // ************************************************************************* //
bool isLabel() const
Definition: tokenI.H:271
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
punctuationToken pToken() const
Definition: tokenI.H:217
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
compound & transferCompoundToken(const Istream &is)
Definition: token.C:93
To & dynamicCast(From &r)
Reference type cast template function,.
Definition: typeInfo.H:85
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
void writeListEntries(Ostream &os, const ListType &l)
Definition: UListIO.C:59
const iterator & end()
Definition: LList.H:286
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: HashTable.H:59
char readEndList(const char *funcName)
Definition: Istream.C:148
InfoProxy< token > info() const
Return info proxy.
Definition: token.H:371
Template class for non-intrusive linked lists.
Definition: LList.H:51
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:163
An STL-conforming const_iterator.
Definition: LList.H:297
A token holds items read from Istream.
Definition: token.H:69
void putBack(const token &)
Put back token.
Definition: Istream.C:30
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:256
Template function to specify if the data of a type are contiguous.
Traits class for primitives.
Definition: pTraits.H:50
bool isCompound() const
Definition: tokenI.H:399
A templated class for holding compound tokens.
Definition: token.H:205
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))
virtual Istream & read(token &)=0
Return next token from stream.
A class for handling words, derived from string.
Definition: word.H:59
Istream & operator>>(Istream &, directionInfo &)
streamFormat format() const
Return current stream format.
Definition: IOstream.H:377
char readBeginList(const char *funcName)
Definition: Istream.C:127
void fatalCheck(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:105
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition: HashTable.H:60
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:53
static const char nl
Definition: Ostream.H:265
Ostream & writeKeyword(const keyType &)
Write the keyword followed by an appropriate indentation.
Definition: Ostream.C:54
iterator begin()
Definition: LList.H:281
void writeEntry(Ostream &os, const HashTable< T, Key, Hash > &ht)
Definition: HashTableIO.C:96
const volScalarField & T
T * data()
Return a pointer to the first data element,.
Definition: UListI.H:149
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:331
std::streamsize byteSize() const
Return the binary size in number of characters of the UList.
Definition: UList.C:100
label labelToken() const
Definition: tokenI.H:276
label size() const
Return the number of elements in the UList.
Definition: UListI.H:299
Non-intrusive singly-linked list.
void writeListEntry(Ostream &os, const ListType &l)
Definition: UListIO.C:35
void transfer(List< T > &)
Transfer the contents of the argument List into this list.
Definition: List.C:342
bool isPunctuation() const
Definition: tokenI.H:212
IOerror FatalIOError