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-2022 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>
35 void Foam::CompactIOField<T, BaseType>::readFromStream(const bool readOnProc)
36 {
37  Istream& is = readStream(word::null, readOnProc);
38 
39  if (readOnProc)
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
56  << " or " << IOField<T>::typeName << nl
57  << " while reading object " << name()
58  << exit(FatalIOError);
59  }
60  }
61 }
62 
63 
64 template<class T, class BaseType>
66 {
67  if
68  (
69  readOpt() == IOobject::MUST_READ
70  || (isReadOptional() && headerOk())
71  )
72  {
73  readFromStream();
74  return true;
75  }
76 
77  return false;
78 }
79 
80 
81 // * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * * //
82 
83 template<class T, class BaseType>
85 :
87 {
88  readContents();
89 }
90 
91 
92 template<class T, class BaseType>
94 (
95  const IOobject& io,
96  const bool readOnProc
97 )
98 :
100 {
101  if (readOpt() == IOobject::MUST_READ)
102  {
103  readFromStream(readOnProc);
104  }
105  else if (isReadOptional())
106  {
107  const bool haveFile = headerOk();
108  readFromStream(readOnProc && haveFile);
109  }
110 }
111 
112 
113 template<class T, class BaseType>
115 (
116  const IOobject& io,
117  Foam::zero
118 )
119 :
120  regIOobject(io)
121 {
122  readContents();
123 }
124 
125 
126 template<class T, class BaseType>
128 (
129  const IOobject& io,
130  const label len
131 )
132 :
133  regIOobject(io)
134 {
135  if (!readContents())
136  {
138  }
139 }
140 
141 
142 template<class T, class BaseType>
144 (
145  const IOobject& io,
146  const UList<T>& content
147 )
148 :
149  regIOobject(io)
150 {
151  if (!readContents())
152  {
154  }
155 }
156 
157 
158 template<class T, class BaseType>
160 (
161  const IOobject& io,
162  Field<T>&& content
163 )
164 :
165  regIOobject(io)
166 {
167  Field<T>::transfer(content);
168 
169  readContents();
170 }
171 
172 
173 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
174 
175 template<class T, class BaseType>
177 (
178  IOstreamOption streamOpt,
179  const bool writeOnProc
180 ) const
181 {
182  if (streamOpt.format() == IOstreamOption::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, writeOnProc);
190 
191  // Restore type
192  const_cast<word&>(typeName) = oldTypeName;
193 
194  return good;
195  }
196 
197  return regIOobject::writeObject(streamOpt, writeOnProc);
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
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 // ************************************************************************* //
virtual bool writeObject(IOstreamOption streamOpt, const bool writeOnProc) const
Write using stream options.
readOption readOpt() const noexcept
Get the read option.
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:125
CompactIOField(const CompactIOField &)=default
Default copy construct.
void resize(const label len)
Adjust allocated size of list.
Definition: ListI.H:160
void transfer(List< Type > &list)
Transfer the contents of the argument List into this list and annul the argument list.
Definition: List.C:326
const vector L(dict.get< vector >("L"))
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:50
"ascii" (normal default)
bool headerOk()
Read and check header info. Does not check the headerClassName.
Definition: regIOobject.C:505
virtual bool writeData(Ostream &os) const
Pure virtual writeData function.
A simple container for options an IOstream can normally have.
virtual bool writeObject(IOstreamOption streamOpt, const bool writeOnProc) const
Write using stream options.
bool isReadOptional() const noexcept
True if (LAZY_READ) bits are set [same as READ_IF_PRESENT].
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:421
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for expressions::valueTypeCode::INVALID.
Definition: exprTraits.C:127
A class for handling words, derived from Foam::string.
Definition: word.H:63
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:56
OBJstream os(runTime.globalPath()/outputName)
void operator=(const Field< Type > &)
Copy assignment.
Definition: Field.C:747
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:627
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.
regIOobject is an abstract class derived from IOobject to handle automatic object registration with t...
Definition: regIOobject.H:66
A class representing the concept of 0 (zero) that can be used to avoid manipulating objects known to ...
Definition: zero.H:57
List< label > labelList
A List of labels.
Definition: List.H:62
IOobject io("surfaceFilmProperties", mesh.time().constant(), mesh, IOobject::READ_IF_PRESENT, IOobject::NO_WRITE, IOobject::NO_REGISTER)
Defines the attributes of an object for which implicit objectRegistry management is supported...
Definition: IOobject.H:172
A primitive field of type <T> with automated input and output.
streamFormat format() const noexcept
Get the current stream format.
IOerror FatalIOError
Error stream (stdout output on all processes), with additional &#39;FOAM FATAL IO ERROR&#39; header text and ...