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-2020 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  token::compound::isCompound
40  (
41  "List<"
43  )
44  )
45  {
46  os << word
47  (
48  "List<"
50  ) << " ";
51  }
52 
53  os << l;
54 }
55 
56 
57 template<class ListType>
58 void Foam::writeListEntries(Ostream& os, const ListType& l)
59 {
60  // Write size and start delimiter
61  os << nl << l.size() << nl << token::BEGIN_LIST;
62 
63  // Write contents
64  forAll(l, i)
65  {
66  writeEntry(os, l[i]);
67  os << nl;
68  }
69 
70  // Write end delimiter
71  os << nl << token::END_LIST << nl;
72 }
73 
74 
75 template<class ListType>
76 void Foam::writeListEntries(Ostream& os, const word& keyword, const ListType& l)
77 {
78  writeKeyword(os, keyword);
79  writeListEntries(os, l);
80  os << token::END_STATEMENT << endl;
81 }
82 
83 
84 template<class T>
85 void Foam::writeEntry(Ostream& os, const UList<T>& l)
86 {
87  writeListEntry(os, l);
88 }
89 
90 
91 // * * * * * * * * * * * * * * * Ostream Operator * * * * * * * * * * * * * //
92 
93 template<class T>
94 Foam::Ostream& Foam::operator<<(Foam::Ostream& os, const Foam::UList<T>& L)
95 {
96  // Write list contents depending on data format
97  if (os.format() == IOstream::ASCII || !contiguous<T>())
98  {
99  bool uniform = false;
100 
101  if (L.size() > 1 && contiguous<T>())
102  {
103  uniform = true;
104 
105  forAll(L, i)
106  {
107  if (L[i] != L[0])
108  {
109  uniform = false;
110  break;
111  }
112  }
113  }
114 
115  if (uniform)
116  {
117  // Write size and start delimiter
118  os << L.size() << token::BEGIN_BLOCK;
119 
120  // Write contents
121  os << L[0];
122 
123  // Write end delimiter
124  os << token::END_BLOCK;
125  }
126  else if (L.size() <= 1 || (L.size() < 11 && contiguous<T>()))
127  {
128  // Write size and start delimiter
129  os << L.size() << token::BEGIN_LIST;
130 
131  // Write contents
132  forAll(L, i)
133  {
134  if (i > 0) os << token::SPACE;
135  os << L[i];
136  }
137 
138  // Write end delimiter
139  os << token::END_LIST;
140  }
141  else
142  {
143  // Write size and start delimiter
144  os << nl << L.size() << nl << token::BEGIN_LIST;
145 
146  // Write contents
147  forAll(L, i)
148  {
149  os << nl << L[i];
150  }
151 
152  // Write end delimiter
153  os << nl << token::END_LIST << nl;
154  }
155  }
156  else
157  {
158  os << nl << L.size() << nl;
159  if (L.size())
160  {
161  os.write(reinterpret_cast<const char*>(L.v_), L.byteSize());
162  }
163  }
164 
165  // Check state of IOstream
166  os.check("Ostream& operator<<(Ostream&, const UList&)");
167 
168  return os;
169 }
170 
171 
172 template<class T>
174 {
175  is.fatalCheck("operator>>(Istream&, UList<T>&)");
176 
177  token firstToken(is);
178 
179  is.fatalCheck("operator>>(Istream&, UList<T>&) : reading first token");
180 
181  if (firstToken.isCompound())
182  {
183  List<T> elems;
184  elems.transfer
185  (
187  (
188  firstToken.transferCompoundToken(is)
189  )
190  );
191  // Check list length
192  label s = elems.size();
193 
194  if (s != L.size())
195  {
197  << "incorrect length for UList. Read " << s
198  << " expected " << L.size()
199  << exit(FatalIOError);
200  }
201  for (label i=0; i<s; i++)
202  {
203  L[i] = elems[i];
204  }
205  }
206  else if (firstToken.isLabel())
207  {
208  label s = firstToken.labelToken();
209 
210  // Set list length to that read
211  if (s != L.size())
212  {
214  << "incorrect length for UList. Read " << s
215  << " expected " << L.size()
216  << exit(FatalIOError);
217  }
218 
219  // Read list contents depending on data format
220 
221  if (is.format() == IOstream::ASCII || !contiguous<T>())
222  {
223  // Read beginning of contents
224  char delimiter = is.readBeginList("List");
225 
226  if (s)
227  {
228  if (delimiter == token::BEGIN_LIST)
229  {
230  for (label i=0; i<s; i++)
231  {
232  is >> L[i];
233 
234  is.fatalCheck
235  (
236  "operator>>(Istream&, UList<T>&) : reading entry"
237  );
238  }
239  }
240  else
241  {
242  T element;
243  is >> element;
244 
245  is.fatalCheck
246  (
247  "operator>>(Istream&, UList<T>&) : "
248  "reading the single entry"
249  );
250 
251  for (label i=0; i<s; i++)
252  {
253  L[i] = element;
254  }
255  }
256  }
257 
258  // Read end of contents
259  is.readEndList("List");
260  }
261  else
262  {
263  if (s)
264  {
265  is.read(reinterpret_cast<char*>(L.data()), s*sizeof(T));
266 
267  is.fatalCheck
268  (
269  "operator>>(Istream&, UList<T>&) : reading the binary block"
270  );
271  }
272  }
273  }
274  else if (firstToken.isPunctuation())
275  {
276  if (firstToken.pToken() != token::BEGIN_LIST)
277  {
279  << "incorrect first token, expected '(', found "
280  << firstToken.info()
281  << exit(FatalIOError);
282  }
283 
284  // Putback the opening bracket
285  is.putBack(firstToken);
286 
287  // Now read as a singly-linked list
288  SLList<T> sll(is);
289 
290  if (sll.size() != L.size())
291  {
293  << "incorrect length for UList. Read " << sll.size()
294  << " expected " << L.size()
295  << exit(FatalIOError);
296  }
297 
298  // Convert the singly-linked list to this list
299  label i = 0;
300  for
301  (
302  typename SLList<T>::const_iterator iter = sll.begin();
303  iter != sll.end();
304  ++iter
305  )
306  {
307  L[i] = iter();
308  }
309 
310  }
311  else
312  {
314  << "incorrect first token, expected <int> or '(', found "
315  << firstToken.info()
316  << exit(FatalIOError);
317  }
318 
319  return is;
320 }
321 
322 
323 // ************************************************************************* //
bool isLabel() const
Definition: tokenI.H:392
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
punctuationToken pToken() const
Definition: tokenI.H:248
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
Ostream & writeKeyword(Foam::Ostream &os, const keyType &kw)
Write the keyword to the Ostream with the current level of indentation.
Definition: keyType.C:155
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:58
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:391
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:164
An STL-conforming const_iterator.
Definition: LList.H:297
A token holds items read from Istream.
Definition: token.H:72
void putBack(const token &)
Put back token.
Definition: Istream.C:30
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
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:520
A templated class for holding compound tokens.
Definition: token.H:209
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:374
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:54
static const char nl
Definition: Ostream.H:260
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:335
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:397
label size() const
Return the number of elements in the UList.
Definition: UListI.H:311
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:243
IOerror FatalIOError