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-2019 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 Functions * * * * * * * * * * * * //
33 
34 template<class T, unsigned Size>
36 {
37  writeListEntry(os, l);
38 }
39 
40 
41 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
42 
43 template<class T, unsigned Size>
45 {
46  operator>>(is, *this);
47 }
48 
49 
50 template<class T, unsigned Size>
52 {
53  is.fatalCheck("operator>>(Istream&, FixedList<T, Size>&)");
54 
55  if (is.format() == IOstream::ASCII || !contiguous<T>())
56  {
57  token firstToken(is);
58 
59  is.fatalCheck
60  (
61  "operator>>(Istream&, FixedList<T, Size>&) : reading first token"
62  );
63 
64  if (firstToken.isCompound())
65  {
66  L = dynamicCast<token::Compound<List<T>>>
67  (
68  firstToken.transferCompoundToken(is)
69  );
70  }
71  else if (firstToken.isLabel())
72  {
73  label s = firstToken.labelToken();
74 
75  // Set list length to that read
76  L.checkSize(s);
77  }
78  else if (!firstToken.isPunctuation())
79  {
81  << "incorrect first token, expected <label> "
82  "or '(' or '{', found "
83  << firstToken.info()
84  << exit(FatalIOError);
85  }
86  else
87  {
88  // Putback the opening bracket
89  is.putBack(firstToken);
90  }
91 
92  // Read beginning of contents
93  char delimiter = is.readBeginList("FixedList");
94 
95  if (delimiter == token::BEGIN_LIST)
96  {
97  for (unsigned i=0; i<Size; i++)
98  {
99  is >> L[i];
100 
101  is.fatalCheck
102  (
103  "operator>>(Istream&, FixedList<T, Size>&) : "
104  "reading entry"
105  );
106  }
107  }
108  else
109  {
110  T element;
111  is >> element;
112 
113  is.fatalCheck
114  (
115  "operator>>(Istream&, FixedList<T, Size>&) : "
116  "reading the single entry"
117  );
118 
119  for (unsigned i=0; i<Size; i++)
120  {
121  L[i] = element;
122  }
123  }
124 
125  // Read end of contents
126  is.readEndList("FixedList");
127  }
128  else
129  {
130  is.read(reinterpret_cast<char*>(L.data()), Size*sizeof(T));
131 
132  is.fatalCheck
133  (
134  "operator>>(Istream&, FixedList<T, Size>&) : "
135  "reading the binary block"
136  );
137  }
138 
139  return is;
140 }
141 
142 
143 // * * * * * * * * * * * * * * * Ostream Operator * * * * * * * * * * * * * //
144 
145 template<class T, unsigned Size>
147 {
148  // Write list contents depending on data format
149  if (os.format() == IOstream::ASCII || !contiguous<T>())
150  {
151  bool uniform = false;
152 
153  if (Size > 1 && contiguous<T>())
154  {
155  uniform = true;
156 
157  forAll(L, i)
158  {
159  if (L[i] != L[0])
160  {
161  uniform = false;
162  break;
163  }
164  }
165  }
166 
167  if (uniform)
168  {
169  // Write size (so it is valid dictionary entry) and start delimiter
170  os << L.size() << token::BEGIN_BLOCK;
171 
172  // Write contents
173  os << L[0];
174 
175  // Write end delimiter
176  os << token::END_BLOCK;
177  }
178  else if (Size <= 1 ||(Size < 11 && contiguous<T>()))
179  {
180  // Write start delimiter
181  os << token::BEGIN_LIST;
182 
183  // Write contents
184  forAll(L, i)
185  {
186  if (i > 0) os << token::SPACE;
187  os << L[i];
188  }
189 
190  // Write end delimiter
191  os << token::END_LIST;
192  }
193  else
194  {
195  // Write start delimiter
196  os << nl << token::BEGIN_LIST;
197 
198  // Write contents
199  forAll(L, i)
200  {
201  os << nl << L[i];
202  }
203 
204  // Write end delimiter
205  os << nl << token::END_LIST << nl;
206  }
207  }
208  else
209  {
210  os.write(reinterpret_cast<const char*>(L.cdata()), Size*sizeof(T));
211  }
212 
213  // Check state of IOstream
214  os.check("Ostream& operator<<(Ostream&, const FixedList&)");
215 
216  return os;
217 }
218 
219 
220 // ************************************************************************* //
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
A 1D vector of objects of type <T> with a fixed size <Size>.
Definition: FixedList.H:78
label size() const
Return the number of elements in the FixedList.
Definition: FixedListI.H:418
const T * cdata() const
Return a const pointer to the first data element,.
Definition: FixedListI.H:202
void checkSize(const label size) const
Check size is within valid range (0 ... size)
Definition: FixedListI.H:151
T * data()
Return a pointer to the first data element,.
Definition: FixedListI.H:210
FixedList()
Null constructor.
Definition: FixedListI.H:33
streamFormat format() const
Return current stream format.
Definition: IOstream.H:374
virtual bool check(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:92
void fatalCheck(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:105
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:60
char readEndList(const char *funcName)
Definition: Istream.C:148
virtual Istream & read(token &)=0
Return next token from stream.
char readBeginList(const char *funcName)
Definition: Istream.C:127
void putBack(const token &)
Put back token.
Definition: Istream.C:30
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:57
virtual Ostream & write(const char)=0
Write character.
A token holds items read from Istream.
Definition: token.H:73
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(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.name(), cloud::prefix, mesh, IOobject::MUST_READ, IOobject::NO_WRITE))
Istream & operator>>(Istream &is, volFieldValue::Result< Type > &result)
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
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 writeListEntry(Ostream &os, const ListType &l)
Definition: UListIO.C:35
void writeEntry(Ostream &os, const HashTable< T, Key, Hash > &ht)
Definition: HashTableIO.C:96
Istream & operator>>(Istream &, pistonPointEdgeData &)
IOerror FatalIOError
Ostream & operator<<(Ostream &os, const fvConstraints &constraints)
static const char nl
Definition: Ostream.H:266
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)