FixedListIO.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-2018 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  token::compound::isCompound("List<" + word(pTraits<T>::typeName) + '>')
142  )
143  {
144  os << word("List<" + word(pTraits<T>::typeName) + '>') << " ";
145  }
146 
147  os << *this;
148 }
149 
150 
151 template<class T, unsigned Size>
153 (
154  const word& keyword,
155  Ostream& os
156 ) const
157 {
158  os.writeKeyword(keyword);
159  writeEntry(os);
160  os << token::END_STATEMENT << endl;
161 }
162 
163 
164 template<class T, unsigned Size>
165 Foam::Ostream& Foam::operator<<(Ostream& os, const FixedList<T, Size>& L)
166 {
167  // Write list contents depending on data format
168  if (os.format() == IOstream::ASCII || !contiguous<T>())
169  {
170  bool uniform = false;
171 
172  if (Size > 1 && contiguous<T>())
173  {
174  uniform = true;
175 
176  forAll(L, i)
177  {
178  if (L[i] != L[0])
179  {
180  uniform = false;
181  break;
182  }
183  }
184  }
185 
186  if (uniform)
187  {
188  // Write size (so it is valid dictionary entry) and start delimiter
189  os << L.size() << token::BEGIN_BLOCK;
190 
191  // Write contents
192  os << L[0];
193 
194  // Write end delimiter
195  os << token::END_BLOCK;
196  }
197  else if (Size <= 1 ||(Size < 11 && contiguous<T>()))
198  {
199  // Write start delimiter
200  os << token::BEGIN_LIST;
201 
202  // Write contents
203  forAll(L, i)
204  {
205  if (i > 0) os << token::SPACE;
206  os << L[i];
207  }
208 
209  // Write end delimiter
210  os << token::END_LIST;
211  }
212  else
213  {
214  // Write start delimiter
215  os << nl << token::BEGIN_LIST;
216 
217  // Write contents
218  forAll(L, i)
219  {
220  os << nl << L[i];
221  }
222 
223  // Write end delimiter
224  os << nl << token::END_LIST << nl;
225  }
226  }
227  else
228  {
229  os.write(reinterpret_cast<const char*>(L.cdata()), Size*sizeof(T));
230  }
231 
232  // Check state of IOstream
233  os.check("Ostream& operator<<(Ostream&, const FixedList&)");
234 
235  return os;
236 }
237 
238 
239 // ************************************************************************* //
bool isLabel() const
Definition: tokenI.H:271
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:428
void writeEntry(Ostream &) const
Write the FixedList as a dictionary entry.
Definition: FixedListIO.C:137
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:54
virtual bool check(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:92
char readEndList(const char *funcName)
Definition: Istream.C:148
InfoProxy< token > info() const
Return info proxy.
Definition: token.H:380
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
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
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
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
void checkSize(const label size) const
Check size is within valid range (0 ... size)
Definition: FixedListI.H:161
label size() const
Return the number of elements in the FixedList.
Definition: FixedListI.H:429
Ostream & writeKeyword(const keyType &)
Write the keyword followed by an appropriate indentation.
Definition: Ostream.C:54
const volScalarField & T
FixedList()
Null constructor.
Definition: FixedListI.H:33
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:331
T * data()
Return a pointer to the first data element,.
Definition: FixedListI.H:220
label labelToken() const
Definition: tokenI.H:276
virtual Ostream & write(const token &)=0
Write next token to stream.
bool isPunctuation() const
Definition: tokenI.H:212
const T * cdata() const
Return a const pointer to the first data element,.
Definition: FixedListI.H:212
IOerror FatalIOError