CompactIOField.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 | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2011-2017 OpenFOAM Foundation
9  Copyright (C) 2018 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 \*---------------------------------------------------------------------------*/
28 
29 #include "CompactIOField.H"
30 #include "labelList.H"
31 
32 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
33 
34 template<class T, class BaseType>
36 {
37  Istream& is = readStream(word::null, valid);
38 
39  if (valid)
40  {
41  if (headerClassName() == IOField<T>::typeName)
42  {
43  is >> static_cast<Field<T>&>(*this);
44  close();
45  }
46  else if (headerClassName() == typeName)
47  {
48  is >> *this;
49  close();
50  }
51  else
52  {
54  << "unexpected class name " << headerClassName()
55  << " expected " << typeName << " or " << IOField<T>::typeName
56  << endl
57  << " while reading object " << name()
58  << exit(FatalIOError);
59  }
60  }
61 }
62 
63 
64 // * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * * //
65 
66 template<class T, class BaseType>
68 :
69  regIOobject(io)
70 {
71  if
72  (
74  || (io.readOpt() == IOobject::READ_IF_PRESENT && headerOk())
75  )
76  {
77  readFromStream();
78  }
79 }
80 
81 
82 template<class T, class BaseType>
84 (
85  const IOobject& io,
86  const bool valid
87 )
88 :
89  regIOobject(io)
90 {
91  if (io.readOpt() == IOobject::MUST_READ)
92  {
93  readFromStream(valid);
94  }
95  else if (io.readOpt() == IOobject::READ_IF_PRESENT)
96  {
97  bool haveFile = headerOk();
98  readFromStream(valid && haveFile);
99  }
100 }
101 
102 
103 template<class T, class BaseType>
105 (
106  const IOobject& io,
107  const label size
108 )
109 :
110  regIOobject(io)
111 {
112  if
113  (
115  || (io.readOpt() == IOobject::READ_IF_PRESENT && headerOk())
116  )
117  {
118  readFromStream();
119  }
120  else
121  {
122  Field<T>::setSize(size);
123  }
124 }
125 
126 
127 template<class T, class BaseType>
129 (
130  const IOobject& io,
131  const UList<T>& content
132 )
133 :
134  regIOobject(io)
135 {
136  if
137  (
139  || (io.readOpt() == IOobject::READ_IF_PRESENT && headerOk())
140  )
141  {
142  readFromStream();
143  }
144  else
145  {
146  Field<T>::operator=(content);
147  }
148 }
149 
150 
151 template<class T, class BaseType>
153 (
154  const IOobject& io,
155  Field<T>&& content
156 )
157 :
158  regIOobject(io)
159 {
160  Field<T>::transfer(content);
161 
162  if
163  (
165  || (io.readOpt() == IOobject::READ_IF_PRESENT && headerOk())
166  )
167  {
168  readFromStream();
169  }
170 }
171 
172 
173 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
174 
175 template<class T, class BaseType>
177 (
181  const bool valid
182 ) const
183 {
184  if (fmt == IOstream::ASCII)
185  {
186  // Change type to be non-compact format type
187  const word oldTypeName(typeName);
188 
189  const_cast<word&>(typeName) = IOField<T>::typeName;
190 
191  bool good = regIOobject::writeObject(IOstream::ASCII, ver, cmp, valid);
192 
193  // Change type back
194  const_cast<word&>(typeName) = oldTypeName;
195 
196  return good;
197  }
198 
199  return regIOobject::writeObject(fmt, ver, cmp, valid);
200 }
201 
202 
203 template<class T, class BaseType>
205 {
206  return (os << *this).good();
207 }
208 
209 
210 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
211 
212 template<class T, class BaseType>
214 (
215  const CompactIOField<T, BaseType>& rhs
216 )
217 {
218  if (this == &rhs)
219  {
220  return; // Self-assigment is a no-op
221  }
222 
223  Field<T>::operator=(rhs);
224 }
225 
226 
227 // * * * * * * * * * * * * * * * Friend Operators * * * * * * * * * * * * * //
228 
229 template<class T, class BaseType>
230 Foam::Istream& Foam::operator>>
231 (
232  Foam::Istream& is,
234 )
235 {
236  // Read compact
237  const labelList start(is);
238  const Field<BaseType> elems(is);
239 
240  // Convert
241  L.setSize(start.size()-1);
242 
243  forAll(L, i)
244  {
245  T& subField = L[i];
246 
247  label index = start[i];
248  subField.setSize(start[i+1] - index);
249 
250  forAll(subField, j)
251  {
252  subField[j] = elems[index++];
253  }
254  }
255 
256  return is;
257 }
258 
259 
260 template<class T, class BaseType>
261 Foam::Ostream& Foam::operator<<
262 (
263  Foam::Ostream& os,
265 )
266 {
267  // Keep ascii writing same.
268  if (os.format() == IOstream::ASCII)
269  {
270  os << static_cast<const Field<T>&>(L);
271  }
272  else
273  {
274  // Convert to compact format
275  labelList start(L.size()+1);
276 
277  start[0] = 0;
278  for (label i = 1; i < start.size(); i++)
279  {
280  start[i] = start[i-1]+L[i-1].size();
281  }
282 
283  Field<BaseType> elems(start[start.size()-1]);
284 
285  label elemI = 0;
286  forAll(L, i)
287  {
288  const T& subField = L[i];
289 
290  forAll(subField, j)
291  {
292  elems[elemI++] = subField[j];
293  }
294  }
295  os << start << elems;
296  }
297 
298  return os;
299 }
300 
301 
302 // ************************************************************************* //
Foam::labelList
List< label > labelList
A List of labels.
Definition: List.H:74
setSize
points setSize(newPointi)
Foam::IOobject
Defines the attributes of an object for which implicit objectRegistry management is supported,...
Definition: IOobject.H:104
Foam::regIOobject::writeObject
virtual bool writeObject(IOstream::streamFormat, IOstream::versionNumber, IOstream::compressionType, const bool valid) const
Write using given format, version and compression.
Definition: regIOobjectWrite.C:39
L
const vector L(dict.get< vector >("L"))
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:62
Foam::IOField
A primitive field of type <T> with automated input and output.
Definition: foamVtkLagrangianWriter.H:61
Foam::VectorSpace::size
static constexpr direction size()
Return the number of elements in the VectorSpace = Ncmpts.
Definition: VectorSpaceI.H:92
Foam::FatalIOError
IOerror FatalIOError
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:337
Foam::CompactIOField
A Field of objects of type <T> with automated input and output using a compact storage....
Definition: CompactIOField.H:52
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:290
Foam::label
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:62
Foam::IOstreamOption::versionNumber
Representation of a major/minor version number.
Definition: IOstreamOption.H:79
labelList.H
Foam::Field< T >
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:61
Foam::name
word name(const complex &c)
Return string representation of complex.
Definition: complex.C:76
Foam::IOobject::READ_IF_PRESENT
Definition: IOobject.H:122
Foam::T
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
Definition: FieldFieldFunctions.C:58
Foam::IOstreamOption::streamFormat
streamFormat
Data format (ascii | binary)
Definition: IOstreamOption.H:64
Foam::CompactIOField::CompactIOField
CompactIOField(const CompactIOField &)=default
Default copy construct.
Foam::Field::operator=
void operator=(const Field< Type > &)
Copy assignment.
Definition: Field.C:658
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
Foam::CompactIOField::writeData
virtual bool writeData(Ostream &os) const
Definition: CompactIOField.C:204
Foam::CompactIOField::writeObject
virtual bool writeObject(IOstream::streamFormat, IOstream::versionNumber, IOstream::compressionType, const bool valid) const
Definition: CompactIOField.C:177
Foam::regIOobject
regIOobject is an abstract class derived from IOobject to handle automatic object registration with t...
Definition: regIOobject.H:67
Foam::IOstreamOption::ASCII
"ascii"
Definition: IOstreamOption.H:66
Foam::List< label >
Foam::IOobject::readOpt
readOption readOpt() const
The read option.
Definition: IOobjectI.H:141
Foam::start
label ListType::const_reference const label start
Definition: ListOps.H:408
Foam::UList
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition: HashTable.H:103
Foam::IOstreamOption::compressionType
compressionType
Compression treatment (UNCOMPRESSED | COMPRESSED)
Definition: IOstreamOption.H:71
FatalIOErrorInFunction
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:375
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
CompactIOField.H
Foam::IOobject::MUST_READ
Definition: IOobject.H:120