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-2026 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 {
38  (
39  "List<"
41  );
42 
43  os << l;
44 }
45 
46 
47 template<class ListType>
48 void Foam::writeListEntries(Ostream& os, const ListType& l)
49 {
50  // Write size and start delimiter
51  os << nl << l.size() << nl << token::BEGIN_LIST;
52 
53  // Write contents
54  forAll(l, i)
55  {
56  writeEntry(os, l[i]);
57  os << nl;
58  }
59 
60  // Write end delimiter
61  os << nl << token::END_LIST << nl;
62 }
63 
64 
65 template<class ListType>
66 void Foam::writeListEntries(Ostream& os, const word& keyword, const ListType& l)
67 {
68  writeKeyword(os, keyword);
69  writeListEntries(os, l);
70  os << token::END_STATEMENT << endl;
71 }
72 
73 
74 template<class T>
75 void Foam::writeEntry(Ostream& os, const UList<T>& l)
76 {
77  writeListEntry(os, l);
78 }
79 
80 
81 // * * * * * * * * * * * * * * * Ostream Operator * * * * * * * * * * * * * //
82 
83 template<class T>
85 {
86  // Write list contents depending on data format
87  if (os.format() == IOstream::ASCII || !contiguous<T>())
88  {
89  bool uniform = false;
90 
91  if (L.size() > 1 && contiguous<T>())
92  {
93  uniform = true;
94 
95  forAll(L, i)
96  {
97  if (L[i] != L[0])
98  {
99  uniform = false;
100  break;
101  }
102  }
103  }
104 
105  if (uniform)
106  {
107  // Write size and start delimiter
108  os << L.size() << token::BEGIN_BLOCK;
109 
110  // Write contents
111  os << L[0];
112 
113  // Write end delimiter
114  os << token::END_BLOCK;
115  }
116  else if (L.size() <= 1 || (L.size() < 11 && contiguous<T>()))
117  {
118  // Write size and start delimiter
119  os << L.size() << token::BEGIN_LIST;
120 
121  // Write contents
122  forAll(L, i)
123  {
124  if (i > 0) os << token::SPACE;
125  os << L[i];
126  }
127 
128  // Write end delimiter
129  os << token::END_LIST;
130  }
131  else
132  {
133  // Write size and start delimiter
134  os << nl << L.size() << nl << token::BEGIN_LIST;
135 
136  // Write contents
137  forAll(L, i)
138  {
139  os << nl << L[i];
140  }
141 
142  // Write end delimiter
143  os << nl << token::END_LIST << nl;
144  }
145  }
146  else
147  {
148  os << nl << L.size() << nl;
149  if (L.size())
150  {
151  os.write(reinterpret_cast<const char*>(L.v_), L.byteSize());
152  }
153  }
154 
155  // Check state of IOstream
156  os.check("Ostream& operator<<(Ostream&, const UList&)");
157 
158  return os;
159 }
160 
161 
162 template<class T>
163 Foam::Istream& Foam::operator>>(Istream& is, UList<T>& L)
164 {
165  is.fatalCheck("operator>>(Istream&, UList<T>&)");
166 
167  token firstToken(is);
168 
169  is.fatalCheck("operator>>(Istream&, UList<T>&) : reading first token");
170 
171  if (firstToken.isCompound())
172  {
173  List<T> elems;
174  elems.transfer
175  (
176  dynamicCast<token::Compound<List<T>>>
177  (
178  firstToken.transferCompoundToken(is)
179  )
180  );
181  // Check list length
182  label s = elems.size();
183 
184  if (s != L.size())
185  {
187  << "incorrect length for UList. Read " << s
188  << " expected " << L.size()
189  << exit(FatalIOError);
190  }
191  for (label i=0; i<s; i++)
192  {
193  L[i] = elems[i];
194  }
195  }
196  else if (firstToken.isLabel())
197  {
198  label s = firstToken.labelToken();
199 
200  // Set list length to that read
201  if (s != L.size())
202  {
204  << "incorrect length for UList. Read " << s
205  << " expected " << L.size()
206  << exit(FatalIOError);
207  }
208 
209  // Read list contents depending on data format
210 
211  if (is.format() == IOstream::ASCII || !contiguous<T>())
212  {
213  // Read beginning of contents
214  char delimiter = is.readBeginList("List");
215 
216  if (s)
217  {
218  if (delimiter == token::BEGIN_LIST)
219  {
220  for (label i=0; i<s; i++)
221  {
222  is >> L[i];
223 
224  is.fatalCheck
225  (
226  "operator>>(Istream&, UList<T>&) : reading entry"
227  );
228  }
229  }
230  else
231  {
232  T element;
233  is >> element;
234 
235  is.fatalCheck
236  (
237  "operator>>(Istream&, UList<T>&) : "
238  "reading the single entry"
239  );
240 
241  for (label i=0; i<s; i++)
242  {
243  L[i] = element;
244  }
245  }
246  }
247 
248  // Read end of contents
249  is.readEndList("List");
250  }
251  else
252  {
253  if (s)
254  {
255  is.read(reinterpret_cast<char*>(L.data()), s*sizeof(T));
256 
257  is.fatalCheck
258  (
259  "operator>>(Istream&, UList<T>&) : reading the binary block"
260  );
261  }
262  }
263  }
264  else if (firstToken.isPunctuation())
265  {
266  if (firstToken.pToken() != token::BEGIN_LIST)
267  {
269  << "incorrect first token, expected '(', found "
270  << firstToken.info()
271  << exit(FatalIOError);
272  }
273 
274  // Putback the opening bracket
275  is.putBack(firstToken);
276 
277  // Now read as a singly-linked list
278  SLList<T> sll(is);
279 
280  if (sll.size() != L.size())
281  {
283  << "incorrect length for UList. Read " << sll.size()
284  << " expected " << L.size()
285  << exit(FatalIOError);
286  }
287 
288  // Convert the singly-linked list to this list
289  label i = 0;
290  for
291  (
292  typename SLList<T>::const_iterator iter = sll.begin();
293  iter != sll.end();
294  ++iter
295  )
296  {
297  L[i] = iter();
298  }
299 
300  }
301  else
302  {
304  << "incorrect first token, expected <int> or '(', found "
305  << firstToken.info()
306  << exit(FatalIOError);
307  }
308 
309  return is;
310 }
311 
312 
313 // ************************************************************************* //
Non-intrusive singly-linked list.
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:449
streamFormat format() const
Return current stream format.
Definition: IOstream.H:377
virtual bool check(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:108
void fatalCheck(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:121
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
virtual Ostream & write(const token &)
Write token.
Definition: Ostream.C:51
virtual Ostream & writeCompoundTag(const word &typeName)
Write the compound token tag if the name is a compound token.
Definition: Ostream.C:135
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition: UList.H:74
label size() const
Return the number of elements in the UList.
Definition: UListI.H:311
std::streamsize byteSize() const
Return the binary size in number of characters of the UList.
Definition: UList.C:100
Traits class for primitives.
Definition: pTraits.H:53
Motion of the mesh specified as a list of pointMeshMovers.
A class for handling words, derived from string.
Definition: word.H:63
Template function to specify if the data of a type are contiguous.
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:346
gmvFile<< "tracers "<< particles.size()<< nl;forAllConstIter(lagrangian::Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().x()<< " ";}gmvFile<< nl;forAllConstIter(lagrangian::Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().y()<< " ";}gmvFile<< nl;forAllConstIter(lagrangian::Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().z()<< " ";}gmvFile<< nl;forAll(lagrangianScalarNames, i){ word name=lagrangianScalarNames[i];IOField< scalar > s(IOobject(name, runTime.name(), lagrangian::cloud::prefix, mesh, IOobject::MUST_READ, IOobject::NO_WRITE))
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
Istream & operator>>(Istream &, pointEdgeDist &)
Definition: pointEdgeDist.C:41
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
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:288
void writeListEntry(Ostream &os, const ListType &l)
Definition: UListIO.C:35
To & dynamicCast(From &r)
Reference type cast template function,.
Definition: typeInfo.H:98
void writeListEntries(Ostream &os, const ListType &l)
Definition: UListIO.C:48
IOerror FatalIOError
Ostream & operator<<(Ostream &os, const fvConstraints &constraints)
Ostream & writeKeyword(Foam::Ostream &os, const keyType &kw)
Write the keyword to the Ostream with the current level of indentation.
Definition: keyType.C:155
void T(GeometricField< Type, GeoMesh, PrimitiveField1 > &gf, const GeometricField< Type, GeoMesh, PrimitiveField2 > &gf1)
void writeEntry(Ostream &os, const word &key, const DimensionedFieldFunction< DimensionedFieldType > &f)
static const char nl
Definition: Ostream.H:297