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 | Copyright (C) 2011-2015 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);
35 
36  if (headerClassName() == IOField<T>::typeName)
37  {
38  is >> static_cast<Field<T>&>(*this);
39  close();
40  }
41  else if (headerClassName() == typeName)
42  {
43  is >> *this;
44  close();
45  }
46  else
47  {
49  (
50  is
51  ) << "unexpected class name " << headerClassName()
52  << " expected " << typeName << " or " << IOField<T>::typeName
53  << endl
54  << " while reading object " << name()
55  << exit(FatalIOError);
56  }
57 }
58 
59 
60 // * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * * //
61 
62 template<class T, class BaseType>
64 :
65  regIOobject(io)
66 {
67  if
68  (
70  || (io.readOpt() == IOobject::READ_IF_PRESENT && headerOk())
71  )
72  {
74  }
75 }
76 
77 
78 template<class T, class BaseType>
80 (
81  const IOobject& io,
82  const label size
83 )
84 :
85  regIOobject(io)
86 {
87  if
88  (
89  io.readOpt() == IOobject::MUST_READ
90  || (io.readOpt() == IOobject::READ_IF_PRESENT && headerOk())
91  )
92  {
94  }
95  else
96  {
97  Field<T>::setSize(size);
98  }
99 }
100 
101 
102 template<class T, class BaseType>
104 (
105  const IOobject& io,
106  const Field<T>& list
107 )
108 :
109  regIOobject(io)
110 {
111  if
112  (
113  io.readOpt() == IOobject::MUST_READ
114  || (io.readOpt() == IOobject::READ_IF_PRESENT && headerOk())
115  )
116  {
117  readFromStream();
118  }
119  else
120  {
121  Field<T>::operator=(list);
122  }
123 }
124 
125 
126 template<class T, class BaseType>
128 (
129  const IOobject& io,
130  const Xfer<Field<T> >& list
131 )
132 :
133  regIOobject(io)
134 {
135  Field<T>::transfer(list());
136 
137  if
138  (
139  io.readOpt() == IOobject::MUST_READ
140  || (io.readOpt() == IOobject::READ_IF_PRESENT && headerOk())
141  )
142  {
143  readFromStream();
144  }
145 }
146 
147 
148 // * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * * //
149 
150 template<class T, class BaseType>
152 {}
153 
154 
155 
156 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
157 
158 template<class T, class BaseType>
160 (
164 ) const
165 {
166  if (fmt == IOstream::ASCII)
167  {
168  // Change type to be non-compact format type
169  const word oldTypeName = typeName;
170 
171  const_cast<word&>(typeName) = IOField<T>::typeName;
172 
173  bool good = regIOobject::writeObject(fmt, ver, cmp);
174 
175  // Change type back
176  const_cast<word&>(typeName) = oldTypeName;
177 
178  return good;
179  }
180  else
181  {
182  return regIOobject::writeObject(fmt, ver, cmp);
183  }
184 }
185 
186 
187 template<class T, class BaseType>
189 {
190  return (os << *this).good();
191 }
192 
193 
194 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
195 
196 template<class T, class BaseType>
198 (
199  const CompactIOField<T, BaseType>& rhs
200 )
201 {
202  Field<T>::operator=(rhs);
203 }
204 
205 
206 template<class T, class BaseType>
207 void Foam::CompactIOField<T, BaseType>::operator=(const Field<T>& rhs)
208 {
209  Field<T>::operator=(rhs);
210 }
211 
212 
213 // * * * * * * * * * * * * * * * Friend Operators * * * * * * * * * * * * * //
214 
215 template<class T, class BaseType>
216 Foam::Istream& Foam::operator>>
217 (
218  Foam::Istream& is,
220 )
221 {
222  // Read compact
223  const labelList start(is);
224  const Field<BaseType> elems(is);
225 
226  // Convert
227  L.setSize(start.size()-1);
228 
229  forAll(L, i)
230  {
231  T& subField = L[i];
232 
233  label index = start[i];
234  subField.setSize(start[i+1] - index);
235 
236  forAll(subField, j)
237  {
238  subField[j] = elems[index++];
239  }
240  }
241 
242  return is;
243 }
244 
245 
246 template<class T, class BaseType>
247 Foam::Ostream& Foam::operator<<
248 (
249  Foam::Ostream& os,
251 )
252 {
253  // Keep ascii writing same.
254  if (os.format() == IOstream::ASCII)
255  {
256  os << static_cast<const Field<T>&>(L);
257  }
258  else
259  {
260  // Convert to compact format
261  labelList start(L.size()+1);
262 
263  start[0] = 0;
264  for (label i = 1; i < start.size(); i++)
265  {
266  start[i] = start[i-1]+L[i-1].size();
267  }
268 
269  Field<BaseType> elems(start[start.size()-1]);
270 
271  label elemI = 0;
272  forAll(L, i)
273  {
274  const T& subField = L[i];
275 
276  forAll(subField, j)
277  {
278  elems[elemI++] = subField[j];
279  }
280  }
281  os << start << elems;
282  }
283 
284  return os;
285 }
286 
287 
288 // ************************************************************************* //
setSize
points setSize(newPointi)
Foam::IOobject
IOobject defines the attributes of an object for which implicit objectRegistry management is supporte...
Definition: IOobject.H:91
Foam::CompactIOField::writeData
virtual bool writeData(Ostream &) const
Definition: CompactIOField.C:188
Foam::CompactIOField::~CompactIOField
virtual ~CompactIOField()
Definition: CompactIOField.C:151
Foam::regIOobject::writeObject
virtual bool writeObject(IOstream::streamFormat, IOstream::versionNumber, IOstream::compressionType) const
Write using given format, version and compression.
Definition: regIOobjectWrite.C:37
Foam::word
A class for handling words, derived from string.
Definition: word.H:59
Foam::IOField
A primitive field of type <T> with automated input and output.
Definition: IOField.H:50
Foam::labelList
List< label > labelList
A List of labels.
Definition: labelList.H:56
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:406
Foam::IOstream::compressionType
compressionType
Enumeration for the format of data in the stream.
Definition: IOstream.H:193
Foam::IOobject::MUST_READ
@ MUST_READ
Definition: IOobject.H:108
Foam::FatalIOError
IOerror FatalIOError
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
Foam::CompactIOField
A Field of objects of type <T> with automated input and output using a compact storage....
Definition: CompactIOField.H:50
Foam::IOstream::versionNumber
Version number type.
Definition: IOstream.H:96
Foam::IOstream::ASCII
@ ASCII
Definition: IOstream.H:88
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:59
labelList.H
Foam::Field< T >
Foam::IOobject::readOpt
readOption readOpt() const
Definition: IOobject.H:317
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:57
Foam::CompactIOField::writeObject
virtual bool writeObject(IOstream::streamFormat, IOstream::versionNumber, IOstream::compressionType) const
Definition: CompactIOField.C:160
Foam::T
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
Definition: FieldFieldFunctions.C:55
Foam::CompactIOField::readFromStream
void readFromStream()
Read according to header type.
Definition: CompactIOField.C:32
Foam::CompactIOField::CompactIOField
CompactIOField(const IOobject &)
Construct from IOobject.
Foam::Field::operator=
void operator=(const Field< Type > &)
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
Foam::regIOobject
regIOobject is an abstract class derived from IOobject to handle automatic object registration with t...
Definition: regIOobject.H:60
FatalIOErrorInFunction
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:330
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:53
CompactIOField.H
Foam::IOobject::READ_IF_PRESENT
@ READ_IF_PRESENT
Definition: IOobject.H:110
Foam::CompactIOField::operator=
void operator=(const CompactIOField< T, BaseType > &)
Foam::name
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
Foam::IOstream::streamFormat
streamFormat
Enumeration for the format of data in the stream.
Definition: IOstream.H:86