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