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-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 "CompactIOField.H"
27 #include "labelList.H"
28 
29 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
30 
31 template<class T, class BaseType>
33 {
34  Istream& is = readStream(word::null, valid);
35 
36  if (valid)
37  {
38  if (headerClassName() == IOField<T>::typeName)
39  {
40  is >> static_cast<Field<T>&>(*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<T>::typeName
55  << endl
56  << " while reading object " << name()
57  << exit(FatalIOError);
58  }
59  }
60 }
61 
62 
63 // * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * * //
64 
65 template<class T, class BaseType>
67 :
68  regIOobject(io)
69 {
70  if
71  (
74  )
75  {
76  readFromStream();
77  }
78 }
79 
80 
81 template<class T, class BaseType>
83 (
84  const IOobject& io,
85  const bool valid
86 )
87 :
88  regIOobject(io)
89 {
90  if (io.readOpt() == IOobject::MUST_READ)
91  {
92  readFromStream(valid);
93  }
94  else if (io.readOpt() == IOobject::READ_IF_PRESENT)
95  {
96  bool haveFile = headerOk();
97  readFromStream(valid && haveFile);
98  }
99 }
100 
101 
102 template<class T, 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<T>::setSize(size);
122  }
123 }
124 
125 
126 template<class T, class BaseType>
128 (
129  const IOobject& io,
130  const Field<T>& list
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<T>::operator=(list);
146  }
147 }
148 
149 
150 template<class T, class BaseType>
152 (
153  const IOobject& io,
154  const Xfer<Field<T>>& list
155 )
156 :
157  regIOobject(io)
158 {
159  Field<T>::transfer(list());
160 
161  if
162  (
164  || (io.readOpt() == IOobject::READ_IF_PRESENT && headerOk())
165  )
166  {
167  readFromStream();
168  }
169 }
170 
171 
172 // * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * * //
173 
174 template<class T, class BaseType>
176 {}
177 
178 
179 
180 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
181 
182 template<class T, class BaseType>
184 (
188  const bool valid
189 ) const
190 {
191  if (fmt == IOstream::ASCII)
192  {
193  // Change type to be non-compact format type
194  const word oldTypeName = typeName;
195 
196  const_cast<word&>(typeName) = IOField<T>::typeName;
197 
198  bool good = regIOobject::writeObject(fmt, ver, cmp, valid);
199 
200  // Change type back
201  const_cast<word&>(typeName) = oldTypeName;
202 
203  return good;
204  }
205  else
206  {
207  return regIOobject::writeObject(fmt, ver, cmp, valid);
208  }
209 }
210 
211 
212 template<class T, class BaseType>
214 {
215  return (os << *this).good();
216 }
217 
218 
219 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
220 
221 template<class T, class BaseType>
222 void Foam::CompactIOField<T, BaseType>::operator=
223 (
224  const CompactIOField<T, BaseType>& rhs
225 )
226 {
227  Field<T>::operator=(rhs);
228 }
229 
230 
231 template<class T, class BaseType>
233 {
234  Field<T>::operator=(rhs);
235 }
236 
237 
238 // * * * * * * * * * * * * * * * Friend Operators * * * * * * * * * * * * * //
239 
240 template<class T, class BaseType>
241 Foam::Istream& Foam::operator>>
242 (
243  Foam::Istream& is,
245 )
246 {
247  // Read compact
248  const labelList start(is);
249  const Field<BaseType> elems(is);
250 
251  // Convert
252  L.setSize(start.size()-1);
253 
254  forAll(L, i)
255  {
256  T& subField = L[i];
257 
258  label index = start[i];
259  subField.setSize(start[i+1] - index);
260 
261  forAll(subField, j)
262  {
263  subField[j] = elems[index++];
264  }
265  }
266 
267  return is;
268 }
269 
270 
271 template<class T, class BaseType>
272 Foam::Ostream& Foam::operator<<
273 (
274  Foam::Ostream& os,
276 )
277 {
278  // Keep ascii writing same.
279  if (os.format() == IOstream::ASCII)
280  {
281  os << static_cast<const Field<T>&>(L);
282  }
283  else
284  {
285  // Convert to compact format
286  labelList start(L.size()+1);
287 
288  start[0] = 0;
289  for (label i = 1; i < start.size(); i++)
290  {
291  start[i] = start[i-1]+L[i-1].size();
292  }
293 
294  Field<BaseType> elems(start[start.size()-1]);
295 
296  label elemI = 0;
297  forAll(L, i)
298  {
299  const T& subField = L[i];
300 
301  forAll(subField, j)
302  {
303  elems[elemI++] = subField[j];
304  }
305  }
306  os << start << elems;
307  }
308 
309  return os;
310 }
311 
312 
313 // ************************************************************************* //
A simple container for copying or transferring objects of type <T>.
Definition: Xfer.H:85
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:428
bool good() const
Definition: IOobject.H:468
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
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:94
virtual bool writeObject(IOstream::streamFormat, IOstream::versionNumber, IOstream::compressionType, const bool valid) const
Write using given format, version and compression.
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:163
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:256
bool headerOk()
Read and check header info.
Definition: regIOobject.C:430
Pre-declare related SubField type.
Definition: Field.H:61
regIOobject(const IOobject &, const bool isTime=false)
Construct from IOobject. Optional flag for if IOobject is the.
Definition: regIOobject.C:56
virtual bool writeObject(IOstream::streamFormat, IOstream::versionNumber, IOstream::compressionType, const bool valid) const
Write using given format, version and compression.
A class for handling words, derived from string.
Definition: word.H:59
void operator=(const CompactIOField< T, BaseType > &)
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:53
void operator=(const Field< Type > &)
Definition: Field.C:764
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:331
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:65
tmp< Field< T > > T() const
Return the field transpose (only defined for second rank tensors)
Definition: Field.C:717
readOption readOpt() const
Definition: IOobject.H:359
IOobject defines the attributes of an object for which implicit objectRegistry management is supporte...
Definition: IOobject.H:92
A primitive field of type <T> with automated input and output.
Definition: IOField.H:50
void transfer(List< Type > &)
Transfer the contents of the argument List into this list.
Definition: List.C:342
IOerror FatalIOError