FixedListIO.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-2016 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 "FixedList.H"
27 #include "Istream.H"
28 #include "Ostream.H"
29 #include "token.H"
30 #include "contiguous.H"
31 
32 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
33 
34 template<class T, unsigned Size>
36 {
37  operator>>(is, *this);
38 }
39 
40 
41 template<class T, unsigned Size>
43 {
44  is.fatalCheck("operator>>(Istream&, FixedList<T, Size>&)");
45 
46  if (is.format() == IOstream::ASCII || !contiguous<T>())
47  {
48  token firstToken(is);
49 
50  is.fatalCheck
51  (
52  "operator>>(Istream&, FixedList<T, Size>&) : reading first token"
53  );
54 
55  if (firstToken.isCompound())
56  {
57  L = dynamicCast<token::Compound<List<T>>>
58  (
59  firstToken.transferCompoundToken(is)
60  );
61  }
62  else if (firstToken.isLabel())
63  {
64  label s = firstToken.labelToken();
65 
66  // Set list length to that read
67  L.checkSize(s);
68  }
69  else if (!firstToken.isPunctuation())
70  {
72  << "incorrect first token, expected <label> "
73  "or '(' or '{', found "
74  << firstToken.info()
75  << exit(FatalIOError);
76  }
77  else
78  {
79  // Putback the opening bracket
80  is.putBack(firstToken);
81  }
82 
83  // Read beginning of contents
84  char delimiter = is.readBeginList("FixedList");
85 
86  if (delimiter == token::BEGIN_LIST)
87  {
88  for (unsigned i=0; i<Size; i++)
89  {
90  is >> L[i];
91 
92  is.fatalCheck
93  (
94  "operator>>(Istream&, FixedList<T, Size>&) : "
95  "reading entry"
96  );
97  }
98  }
99  else
100  {
101  T element;
102  is >> element;
103 
104  is.fatalCheck
105  (
106  "operator>>(Istream&, FixedList<T, Size>&) : "
107  "reading the single entry"
108  );
109 
110  for (unsigned i=0; i<Size; i++)
111  {
112  L[i] = element;
113  }
114  }
115 
116  // Read end of contents
117  is.readEndList("FixedList");
118  }
119  else
120  {
121  is.read(reinterpret_cast<char*>(L.data()), Size*sizeof(T));
122 
123  is.fatalCheck
124  (
125  "operator>>(Istream&, FixedList<T, Size>&) : "
126  "reading the binary block"
127  );
128  }
129 
130  return is;
131 }
132 
133 
134 // * * * * * * * * * * * * * * * Ostream Operator * * * * * * * * * * * * * //
135 
136 template<class T, unsigned Size>
138 {
139  if
140  (
141  size()
142  && token::compound::isCompound
143  (
144  "List<" + word(pTraits<T>::typeName) + '>'
145  )
146  )
147  {
148  os << word("List<" + word(pTraits<T>::typeName) + '>') << " ";
149  }
150 
151  os << *this;
152 }
153 
154 
155 template<class T, unsigned Size>
157 (
158  const word& keyword,
159  Ostream& os
160 ) const
161 {
162  os.writeKeyword(keyword);
163  writeEntry(os);
164  os << token::END_STATEMENT << endl;
165 }
166 
167 
168 template<class T, unsigned Size>
169 Foam::Ostream& Foam::operator<<(Ostream& os, const FixedList<T, Size>& L)
170 {
171  // Write list contents depending on data format
172  if (os.format() == IOstream::ASCII || !contiguous<T>())
173  {
174  bool uniform = false;
175 
176  if (Size > 1 && contiguous<T>())
177  {
178  uniform = true;
179 
180  forAll(L, i)
181  {
182  if (L[i] != L[0])
183  {
184  uniform = false;
185  break;
186  }
187  }
188  }
189 
190  if (uniform)
191  {
192  // Write size (so it is valid dictionary entry) and start delimiter
193  os << L.size() << token::BEGIN_BLOCK;
194 
195  // Write contents
196  os << L[0];
197 
198  // Write end delimiter
199  os << token::END_BLOCK;
200  }
201  else if (Size <= 1 ||(Size < 11 && contiguous<T>()))
202  {
203  // Write start delimiter
204  os << token::BEGIN_LIST;
205 
206  // Write contents
207  forAll(L, i)
208  {
209  if (i > 0) os << token::SPACE;
210  os << L[i];
211  }
212 
213  // Write end delimiter
214  os << token::END_LIST;
215  }
216  else
217  {
218  // Write start delimiter
219  os << nl << token::BEGIN_LIST;
220 
221  // Write contents
222  forAll(L, i)
223  {
224  os << nl << L[i];
225  }
226 
227  // Write end delimiter
228  os << nl << token::END_LIST << nl;
229  }
230  }
231  else
232  {
233  os.write(reinterpret_cast<const char*>(L.cdata()), Size*sizeof(T));
234  }
235 
236  // Check state of IOstream
237  os.check("Ostream& operator<<(Ostream&, const FixedList&)");
238 
239  return os;
240 }
241 
242 
243 // ************************************************************************* //
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:428
streamFormat format() const
Return current stream format.
Definition: IOstream.H:377
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
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
A 1D vector of objects of type <T> with a fixed size <Size>.
Definition: FixedList.H:53
label labelToken() const
Definition: tokenI.H:259
char readEndList(const char *funcName)
Definition: Istream.C:148
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
A token holds items read from Istream.
Definition: token.H:69
virtual bool check(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:92
void putBack(const token &)
Put back token.
Definition: Istream.C:30
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:253
Template function to specify if the data of a type are contiguous.
Traits class for primitives.
Definition: pTraits.H:50
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 &)
char readBeginList(const char *funcName)
Definition: Istream.C:127
label size() const
Return the number of elements in the FixedList.
Definition: FixedListI.H:402
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:262
Ostream & writeKeyword(const keyType &)
Write the keyword followed by an appropriate indentation.
Definition: Ostream.C:54
void checkSize(const label size) const
Check size is within valid range (0 ... size).
Definition: FixedListI.H:141
const volScalarField & T
FixedList()
Null constructor.
Definition: FixedListI.H:33
bool isLabel() const
Definition: tokenI.H:254
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:331
InfoProxy< token > info() const
Return info proxy.
Definition: token.H:374
T * data()
Return a pointer to the first data element,.
Definition: FixedListI.H:200
Uniform/equally-weighted distribution model.
Definition: uniform.H:47
void writeEntry(Ostream &) const
Write the FixedList as a dictionary entry.
Definition: FixedListIO.C:137
virtual Ostream & write(const token &)=0
Write next token to stream.
bool isPunctuation() const
Definition: tokenI.H:195
const T * cdata() const
Return a const pointer to the first data element,.
Definition: FixedListI.H:192
void fatalCheck(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:105
bool isCompound() const
Definition: tokenI.H:354
IOerror FatalIOError