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-2020 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 (
178  IOstreamOption streamOpt,
179  const bool valid
180 ) const
181 {
182  if (streamOpt.format() == IOstream::ASCII)
183  {
184  // Change type to be non-compact format type
185  const word oldTypeName(typeName);
186 
187  const_cast<word&>(typeName) = IOField<T>::typeName;
188 
189  bool good = regIOobject::writeObject(streamOpt, valid);
190 
191  // Restore type
192  const_cast<word&>(typeName) = oldTypeName;
193 
194  return good;
195  }
196 
197  return regIOobject::writeObject(streamOpt, valid);
198 }
199 
200 
201 template<class T, class BaseType>
203 {
204  return (os << *this).good();
205 }
206 
207 
208 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
209 
210 template<class T, class BaseType>
212 (
213  const CompactIOField<T, BaseType>& rhs
214 )
215 {
216  if (this == &rhs)
217  {
218  return; // Self-assigment is a no-op
219  }
220 
221  Field<T>::operator=(rhs);
222 }
223 
224 
225 // * * * * * * * * * * * * * * * Friend Operators * * * * * * * * * * * * * //
226 
227 template<class T, class BaseType>
228 Foam::Istream& Foam::operator>>
229 (
230  Foam::Istream& is,
232 )
233 {
234  // Read compact
235  const labelList start(is);
236  const Field<BaseType> elems(is);
237 
238  // Convert
239  L.setSize(start.size()-1);
240 
241  forAll(L, i)
242  {
243  T& subField = L[i];
244 
245  label index = start[i];
246  subField.setSize(start[i+1] - index);
247 
248  forAll(subField, j)
249  {
250  subField[j] = elems[index++];
251  }
252  }
253 
254  return is;
255 }
256 
257 
258 template<class T, class BaseType>
259 Foam::Ostream& Foam::operator<<
260 (
261  Foam::Ostream& os,
263 )
264 {
265  // Keep ascii writing same.
266  if (os.format() == IOstream::ASCII)
267  {
268  os << static_cast<const Field<T>&>(L);
269  }
270  else
271  {
272  // Convert to compact format
273  labelList start(L.size()+1);
274 
275  start[0] = 0;
276  for (label i = 1; i < start.size(); i++)
277  {
278  start[i] = start[i-1]+L[i-1].size();
279  }
280 
281  Field<BaseType> elems(start[start.size()-1]);
282 
283  label elemI = 0;
284  forAll(L, i)
285  {
286  const T& subField = L[i];
287 
288  forAll(subField, j)
289  {
290  elems[elemI++] = subField[j];
291  }
292  }
293  os << start << elems;
294  }
295 
296  return os;
297 }
298 
299 
300 // ************************************************************************* //
Foam::labelList
List< label > labelList
A List of labels.
Definition: List.H:67
setSize
points setSize(newPointi)
Foam::IOobject
Defines the attributes of an object for which implicit objectRegistry management is supported,...
Definition: IOobject.H:169
L
const vector L(dict.get< vector >("L"))
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:65
Foam::IOField
A primitive field of type <T> with automated input and output.
Definition: foamVtkLagrangianWriter.H:61
Foam::IOstreamOption::format
streamFormat format() const noexcept
Get the current stream format.
Definition: IOstreamOption.H:286
Foam::FatalIOError
IOerror FatalIOError
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:369
Foam::CompactIOField
A Field of objects of type <T> with automated input and output using a compact storage....
Definition: CompactIOField.H:53
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:296
labelList.H
Foam::Field< T >
Foam::CompactIOField::writeObject
virtual bool writeObject(IOstreamOption streamOpt, const bool valid) const
Write using stream options.
Definition: CompactIOField.C:177
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:61
Foam::IOobject::READ_IF_PRESENT
Definition: IOobject.H:187
Foam::T
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
Definition: FieldFieldFunctions.C:58
Foam::IOstreamOption
The IOstreamOption is a simple container for options an IOstream can normally have.
Definition: IOstreamOption.H:63
os
OBJstream os(runTime.globalPath()/outputName)
Foam::regIOobject::writeObject
virtual bool writeObject(IOstreamOption streamOpt, const bool valid) const
Write using stream options.
Definition: regIOobjectWrite.C:36
Foam::CompactIOField::CompactIOField
CompactIOField(const CompactIOField &)=default
Default copy construct.
Foam::Field::operator=
void operator=(const Field< Type > &)
Copy assignment.
Definition: Field.C:635
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
Foam::IOobject::readOpt
readOption readOpt() const noexcept
The read option.
Definition: IOobjectI.H:164
Foam::CompactIOField::writeData
virtual bool writeData(Ostream &os) const
Definition: CompactIOField.C:202
Foam::regIOobject
regIOobject is an abstract class derived from IOobject to handle automatic object registration with t...
Definition: regIOobject.H:73
Foam::IOstreamOption::ASCII
"ascii" (normal default)
Definition: IOstreamOption.H:72
Foam::List< label >
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::name
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59
FatalIOErrorInFunction
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:473
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
Foam::VectorSpace::size
static constexpr direction size() noexcept
The number of elements in the VectorSpace = Ncmpts.
Definition: VectorSpace.H:176
CompactIOField.H
Foam::IOobject::MUST_READ
Definition: IOobject.H:185