CompactIOList.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-2016 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 "CompactIOList.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() == IOList<T>::typeName)
37  {
38  is >> static_cast<List<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 " << IOList<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  (
71  )
72  {
73  readFromStream();
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  (
91  )
92  {
93  readFromStream();
94  }
95  else
96  {
97  List<T>::setSize(size);
98  }
99 }
100 
101 
102 template<class T, class BaseType>
104 (
105  const IOobject& io,
106  const List<T>& list
107 )
108 :
109  regIOobject(io)
110 {
111  if
112  (
114  || (io.readOpt() == IOobject::READ_IF_PRESENT && headerOk())
115  )
116  {
117  readFromStream();
118  }
119  else
120  {
121  List<T>::operator=(list);
122  }
123 }
124 
125 
126 template<class T, class BaseType>
128 (
129  const IOobject& io,
130  const Xfer<List<T>>& list
131 )
132 :
133  regIOobject(io)
134 {
135  List<T>::transfer(list());
136 
137  if
138  (
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) = IOList<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>
197 void Foam::CompactIOList<T, BaseType>::operator=
198 (
199  const CompactIOList<T, BaseType>& rhs
200 )
201 {
202  List<T>::operator=(rhs);
203 }
204 
205 
206 template<class T, class BaseType>
208 {
209  List<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 List<BaseType> elems(is);
225 
226  // Convert
227  L.setSize(start.size()-1);
228 
229  forAll(L, i)
230  {
231  T& subList = L[i];
232 
233  label index = start[i];
234  subList.setSize(start[i+1] - index);
235 
236  forAll(subList, j)
237  {
238  subList[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 List<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  List<BaseType> elems(start[start.size()-1]);
270 
271  label elemI = 0;
272  forAll(L, i)
273  {
274  const T& subList = L[i];
275 
276  forAll(subList, j)
277  {
278  elems[elemI++] = subList[j];
279  }
280  }
281  os << start << elems;
282  }
283 
284  return os;
285 }
286 
287 
288 // ************************************************************************* //
A simple container for copying or transferring objects of type <T>.
Definition: Xfer.H:85
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:428
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
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: HashTable.H:59
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:76
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:253
A List of objects of type <T> with automated input and output.
Definition: IOList.H:50
regIOobject(const IOobject &, const bool isTime=false)
Construct from IOobject. Optional flag for if IOobject is the.
Definition: regIOobject.C:119
A List obtained as a section of another List.
Definition: SubList.H:53
virtual bool writeObject(IOstream::streamFormat, IOstream::versionNumber, IOstream::compressionType) const
Write using given format, version and compression.
A class for handling words, derived from string.
Definition: word.H:59
CompactIOList(const IOobject &)
Construct from IOobject.
Definition: CompactIOList.C:63
streamFormat
Enumeration for the format of data in the stream.
Definition: IOstream.H:86
virtual bool writeObject(IOstream::streamFormat, IOstream::versionNumber, IOstream::compressionType) const
Write using given format, version and compression.
compressionType
Enumeration for the format of data in the stream.
Definition: IOstream.H:193
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:53
virtual ~CompactIOList()
A List of objects of type <T> with automated input and output using a compact storage. Behaves like IOList except when binary output in case it writes a CompactListList.
Definition: CompactIOList.H:53
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
void operator=(const UList< T > &)
Assignment from UList operator. Takes linear time.
Definition: List.C:399
void setSize(const label)
Reset size of List.
Definition: List.C:295
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:331
bool good() const
Definition: IOobject.H:406
readOption readOpt() const
Definition: IOobject.H:304
Version number type.
Definition: IOstream.H:96
regIOobject is an abstract class derived from IOobject to handle automatic object registration with t...
Definition: regIOobject.H:60
bool headerOk()
Read and check header info.
Definition: IOobject.C:400
void operator=(const CompactIOList< T, BaseType > &)
virtual bool writeData(Ostream &) const
Pure virtual writaData function.
IOobject defines the attributes of an object for which implicit objectRegistry management is supporte...
Definition: IOobject.H:91
void transfer(List< T > &)
Transfer the contents of the argument List into this list.
Definition: List.C:365
IOerror FatalIOError