CompactIOField.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 "CompactIOField.H"
27 #include "labelList.H"
28 
29 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
30 
31 template<class Type, class BaseType>
33 {
34  Istream& is = readStream(word::null, read);
35 
36  if (read)
37  {
38  if (headerClassName() == IOField<Type>::typeName)
39  {
40  is >> static_cast<Field<Type>&>(*this);
41  close();
42  }
43  else if (headerClassName() == typeName)
44  {
45  is >> *this;
46  close();
47  }
48  else
49  {
51  (
52  is
53  ) << "unexpected class name " << headerClassName()
54  << " expected " << typeName << " or " << IOField<Type>::typeName
55  << endl
56  << " while reading object " << name()
57  << exit(FatalIOError);
58  }
59  }
60 }
61 
62 
63 // * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * * //
64 
65 template<class Type, class BaseType>
67 :
68  regIOobject(io)
69 {
70  if
71  (
74  )
75  {
76  readFromStream();
77  }
78 }
79 
80 
81 template<class Type, class BaseType>
83 (
84  const IOobject& io,
85  const bool read
86 )
87 :
88  regIOobject(io)
89 {
90  if (io.readOpt() == IOobject::MUST_READ)
91  {
92  readFromStream(read);
93  }
94  else if (io.readOpt() == IOobject::READ_IF_PRESENT)
95  {
96  bool haveFile = headerOk();
97  readFromStream(read && haveFile);
98  }
99 }
100 
101 
102 template<class Type, class BaseType>
104 (
105  const IOobject& io,
106  const label size
107 )
108 :
109  regIOobject(io)
110 {
111  if
112  (
114  || (io.readOpt() == IOobject::READ_IF_PRESENT && headerOk())
115  )
116  {
117  readFromStream();
118  }
119  else
120  {
121  Field<Type>::setSize(size);
122  }
123 }
124 
125 
126 template<class Type, class BaseType>
128 (
129  const IOobject& io,
130  const Field<Type>& field
131 )
132 :
133  regIOobject(io)
134 {
135  if
136  (
138  || (io.readOpt() == IOobject::READ_IF_PRESENT && headerOk())
139  )
140  {
141  readFromStream();
142  }
143  else
144  {
145  Field<Type>::operator=(field);
146  }
147 }
148 
149 
150 template<class Type, class BaseType>
152 (
153  const IOobject& io,
154  Field<Type>&& field
155 )
156 :
157  regIOobject(io),
158  Field<Type>(move(field))
159 {
160  if
161  (
163  || (io.readOpt() == IOobject::READ_IF_PRESENT && headerOk())
164  )
165  {
166  readFromStream();
167  }
168 }
169 
170 
171 template<class Type, class BaseType>
173 (
175 )
176 :
177  regIOobject(move(field)),
178  Field<Type>(move(field))
179 {}
180 
181 
182 // * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * * //
183 
184 template<class Type, class BaseType>
186 {}
187 
188 
189 
190 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
191 
192 template<class Type, class BaseType>
194 (
198  const bool write
199 ) const
200 {
201  if (fmt == IOstream::ASCII)
202  {
203  // Change type to be non-compact format type
204  const word oldTypeName = typeName;
205 
206  const_cast<word&>(typeName) = IOField<Type>::typeName;
207 
208  bool good = regIOobject::writeObject(fmt, ver, cmp, write);
209 
210  // Change type back
211  const_cast<word&>(typeName) = oldTypeName;
212 
213  return good;
214  }
215  else
216  {
217  return regIOobject::writeObject(fmt, ver, cmp, write);
218  }
219 }
220 
221 
222 template<class Type, class BaseType>
224 {
225  return (os << *this).good();
226 }
227 
228 
229 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
230 
231 template<class Type, class BaseType>
232 void Foam::CompactIOField<Type, BaseType>::operator=
233 (
235 )
236 {
238 }
239 
240 
241 template<class Type, class BaseType>
242 void Foam::CompactIOField<Type, BaseType>::operator=
243 (
245 )
246 {
247  Field<Type>::operator=(move(rhs));
248 }
249 
250 
251 template<class Type, class BaseType>
253 {
255 }
256 
257 
258 template<class Type, class BaseType>
260 {
261  Field<Type>::operator=(move(rhs));
262 }
263 
264 
265 // * * * * * * * * * * * * * * * Friend Operators * * * * * * * * * * * * * //
266 
267 template<class Type, class BaseType>
268 Foam::Istream& Foam::operator>>
269 (
270  Foam::Istream& is,
272 )
273 {
274  // Read compact
275  const labelList start(is);
276  const Field<BaseType> elems(is);
277 
278  // Convert
279  L.setSize(start.size()-1);
280 
281  forAll(L, i)
282  {
283  Type& subField = L[i];
284 
285  label index = start[i];
286  subField.setSize(start[i+1] - index);
287 
288  forAll(subField, j)
289  {
290  subField[j] = elems[index++];
291  }
292  }
293 
294  return is;
295 }
296 
297 
298 template<class Type, class BaseType>
299 Foam::Ostream& Foam::operator<<
300 (
301  Foam::Ostream& os,
303 )
304 {
305  // Keep ascii writing same.
306  if (os.format() == IOstream::ASCII)
307  {
308  os << static_cast<const Field<Type>&>(L);
309  }
310  else
311  {
312  // Convert to compact format
313  labelList start(L.size()+1);
314 
315  start[0] = 0;
316  for (label i = 1; i < start.size(); i++)
317  {
318  start[i] = start[i-1]+L[i-1].size();
319  }
320 
321  Field<BaseType> elems(start[start.size()-1]);
322 
323  label elemI = 0;
324  forAll(L, i)
325  {
326  const Type& subField = L[i];
327 
328  forAll(subField, j)
329  {
330  elems[elemI++] = subField[j];
331  }
332  }
333  os << start << elems;
334  }
335 
336  return os;
337 }
338 
339 
340 // ************************************************************************* //
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
bool good() const
Definition: IOobject.H:480
CompactIOField(const IOobject &)
Construct from IOobject.
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
static const char *const typeName
Definition: Field.H:105
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:164
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
bool headerOk()
Read and check header info.
Definition: regIOobject.C:443
Pre-declare related SubField type.
Definition: Field.H:60
regIOobject(const IOobject &, const bool isTime=false)
Construct from IOobject. Optional flag for if IOobject is the.
Definition: regIOobject.C:41
void operator=(const CompactIOField< Type, BaseType > &)
virtual bool writeObject(IOstream::streamFormat, IOstream::versionNumber, IOstream::compressionType, const bool write) const
Write using given format, version and compression.
Pre-declare SubField and related Field type.
Definition: Field.H:56
A class for handling words, derived from string.
Definition: word.H:59
streamFormat
Enumeration for the format of data in the stream.
Definition: IOstream.H:86
virtual bool writeData(Ostream &) const
Pure virtual writaData function.
compressionType
Enumeration for the format of data in the stream.
Definition: IOstream.H:193
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:54
void operator=(const Field< Type > &)
Definition: Field.C:526
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
void setSize(const label)
Reset size of List.
Definition: List.C:281
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:318
A Field of objects of type <T> with automated input and output using a compact storage. Behaves like IOField except when binary output in case it writes a CompactListList.
Version number type.
Definition: IOstream.H:96
regIOobject is an abstract class derived from IOobject to handle automatic object registration with t...
Definition: regIOobject.H:52
virtual bool writeObject(IOstream::streamFormat, IOstream::versionNumber, IOstream::compressionType, const bool write) const
Write using given format, version and compression.
readOption readOpt() const
Definition: IOobject.H:365
IOobject defines the attributes of an object for which implicit objectRegistry management is supporte...
Definition: IOobject.H:98
A primitive field of type <T> with automated input and output.
Definition: IOField.H:50
IOerror FatalIOError