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 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  "CompactIOField<T, BaseType>::readFromStream()",
51  is
52  ) << "unexpected class name " << headerClassName()
53  << " expected " << typeName << " or " << IOField<T>::typeName
54  << endl
55  << " while reading object " << name()
56  << exit(FatalIOError);
57  }
58 }
59 
60 
61 // * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * * //
62 
63 template<class T, class BaseType>
65 :
66  regIOobject(io)
67 {
68  if
69  (
72  )
73  {
74  readFromStream();
75  }
76 }
77 
78 
79 template<class T, class BaseType>
81 (
82  const IOobject& io,
83  const label size
84 )
85 :
86  regIOobject(io)
87 {
88  if
89  (
92  )
93  {
94  readFromStream();
95  }
96  else
97  {
98  Field<T>::setSize(size);
99  }
100 }
101 
102 
103 template<class T, class BaseType>
105 (
106  const IOobject& io,
107  const Field<T>& list
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>::operator=(list);
123  }
124 }
125 
126 
127 template<class T, class BaseType>
129 (
130  const IOobject& io,
131  const Xfer<Field<T> >& list
132 )
133 :
134  regIOobject(io)
135 {
136  Field<T>::transfer(list());
137 
138  if
139  (
141  || (io.readOpt() == IOobject::READ_IF_PRESENT && headerOk())
142  )
143  {
144  readFromStream();
145  }
146 }
147 
148 
149 // * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * * //
150 
151 template<class T, class BaseType>
153 {}
154 
155 
156 
157 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
158 
159 template<class T, class BaseType>
161 (
165 ) const
166 {
167  if (fmt == IOstream::ASCII)
168  {
169  // Change type to be non-compact format type
170  const word oldTypeName = typeName;
171 
172  const_cast<word&>(typeName) = IOField<T>::typeName;
173 
174  bool good = regIOobject::writeObject(fmt, ver, cmp);
175 
176  // Change type back
177  const_cast<word&>(typeName) = oldTypeName;
178 
179  return good;
180  }
181  else
182  {
183  return regIOobject::writeObject(fmt, ver, cmp);
184  }
185 }
186 
187 
188 template<class T, class BaseType>
190 {
191  return (os << *this).good();
192 }
193 
194 
195 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
196 
197 template<class T, class BaseType>
198 void Foam::CompactIOField<T, BaseType>::operator=
199 (
200  const CompactIOField<T, BaseType>& rhs
201 )
202 {
203  Field<T>::operator=(rhs);
204 }
205 
206 
207 template<class T, class BaseType>
209 {
210  Field<T>::operator=(rhs);
211 }
212 
213 
214 // * * * * * * * * * * * * * * * Friend Operators * * * * * * * * * * * * * //
215 
216 template<class T, class BaseType>
217 Foam::Istream& Foam::operator>>
218 (
219  Foam::Istream& is,
221 )
222 {
223  // Read compact
224  const labelList start(is);
225  const Field<BaseType> elems(is);
226 
227  // Convert
228  L.setSize(start.size()-1);
229 
230  forAll(L, i)
231  {
232  T& subField = L[i];
233 
234  label index = start[i];
235  subField.setSize(start[i+1] - index);
236 
237  forAll(subField, j)
238  {
239  subField[j] = elems[index++];
240  }
241  }
242 
243  return is;
244 }
245 
246 
247 template<class T, class BaseType>
248 Foam::Ostream& Foam::operator<<
249 (
250  Foam::Ostream& os,
252 )
253 {
254  // Keep ascii writing same.
255  if (os.format() == IOstream::ASCII)
256  {
257  os << static_cast<const Field<T>&>(L);
258  }
259  else
260  {
261  // Convert to compact format
262  labelList start(L.size()+1);
263 
264  start[0] = 0;
265  for (label i = 1; i < start.size(); i++)
266  {
267  start[i] = start[i-1]+L[i-1].size();
268  }
269 
270  Field<BaseType> elems(start[start.size()-1]);
271 
272  label elemI = 0;
273  forAll(L, i)
274  {
275  const T& subField = L[i];
276 
277  forAll(subField, j)
278  {
279  elems[elemI++] = subField[j];
280  }
281  }
282  os << start << elems;
283  }
284 
285  return os;
286 }
287 
288 
289 // ************************************************************************* //
Pre-declare related SubField type.
Definition: Field.H:61
static const char *const typeName
Definition: Field.H:94
readOption readOpt() const
Definition: IOobject.H:304
void transfer(List< Type > &)
Transfer the contents of the argument List into this list.
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
virtual bool writeObject(IOstream::streamFormat, IOstream::versionNumber, IOstream::compressionType) 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
tmp< Field< T > > T() const
Return the field transpose (only defined for second rank tensors)
A simple container for copying or transferring objects of type <T>.
Definition: Xfer.H:85
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.
A class for handling words, derived from string.
Definition: word.H:59
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
virtual bool writeObject(IOstream::streamFormat, IOstream::versionNumber, IOstream::compressionType) const
Write using given format, version and compression.
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:76
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
bool good() const
Definition: IOobject.H:406
CompactIOField(const IOobject &)
Construct from IOobject.
void operator=(const CompactIOField< T, BaseType > &)
regIOobject(const IOobject &, const bool isTime=false)
Construct from IOobject. Optional flag for if IOobject is the.
Definition: regIOobject.C:121
void setSize(const label)
Reset size of List.
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
IOerror FatalIOError
IOobject defines the attributes of an object for which implicit objectRegistry management is supporte...
Definition: IOobject.H:91
void operator=(const Field< Type > &)
Definition: Field.C:672
streamFormat
Enumeration for the format of data in the stream.
Definition: IOstream.H:86
#define forAll(list, i)
Definition: UList.H:421
virtual bool writeData(Ostream &) const
Pure virtual writaData function.
compressionType
Enumeration for the format of data in the stream.
Definition: IOstream.H:193
regIOobject is an abstract class derived from IOobject to handle automatic object registration with t...
Definition: regIOobject.H:60
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:53
bool headerOk()
Read and check header info.
Definition: IOobject.C:424
Version number type.
Definition: IOstream.H:96
#define FatalIOErrorIn(functionName, ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:325
A primitive field of type <T> with automated input and output.
Definition: IOField.H:50