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-2017 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 template<class T, class BaseType>
62 {
63  label size = 0;
64  forAll(*this, i)
65  {
66  label oldSize = size;
67  size += this->operator[](i).size();
68  if (size < oldSize)
69  {
70  return true;
71  }
72  }
73  return false;
74 }
75 
76 
77 // * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * * //
78 
79 template<class T, class BaseType>
81 :
82  regIOobject(io)
83 {
84  if
85  (
86  io.readOpt() == IOobject::MUST_READ
87  || (io.readOpt() == IOobject::READ_IF_PRESENT && headerOk())
88  )
89  {
90  readFromStream();
91  }
92 }
93 
94 
95 template<class T, class BaseType>
97 (
98  const IOobject& io,
99  const label size
100 )
101 :
102  regIOobject(io)
103 {
104  if
105  (
106  io.readOpt() == IOobject::MUST_READ
107  || (io.readOpt() == IOobject::READ_IF_PRESENT && headerOk())
108  )
109  {
110  readFromStream();
111  }
112  else
113  {
114  List<T>::setSize(size);
115  }
116 }
117 
118 
119 template<class T, class BaseType>
121 (
122  const IOobject& io,
123  const List<T>& list
124 )
125 :
126  regIOobject(io)
127 {
128  if
129  (
130  io.readOpt() == IOobject::MUST_READ
131  || (io.readOpt() == IOobject::READ_IF_PRESENT && headerOk())
132  )
133  {
134  readFromStream();
135  }
136  else
137  {
138  List<T>::operator=(list);
139  }
140 }
141 
142 
143 template<class T, class BaseType>
145 (
146  const IOobject& io,
147  const Xfer<List<T>>& list
148 )
149 :
150  regIOobject(io)
151 {
152  List<T>::transfer(list());
153 
154  if
155  (
156  io.readOpt() == IOobject::MUST_READ
157  || (io.readOpt() == IOobject::READ_IF_PRESENT && headerOk())
158  )
159  {
160  readFromStream();
161  }
162 }
163 
164 
165 // * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * * //
166 
167 template<class T, class BaseType>
169 {}
170 
171 
172 
173 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
174 
175 template<class T, class BaseType>
177 (
181  const bool valid
182 ) const
183 {
184  if (fmt == IOstream::ASCII)
185  {
186  // Change type to be non-compact format type
187  const word oldTypeName = typeName;
188 
189  const_cast<word&>(typeName) = IOList<T>::typeName;
190 
191  bool good = regIOobject::writeObject(fmt, ver, cmp, valid);
192 
193  // Change type back
194  const_cast<word&>(typeName) = oldTypeName;
195 
196  return good;
197  }
198  else if (overflows())
199  {
201  << "Overall number of elements of CompactIOList of size "
202  << this->size() << " overflows the representation of a label"
203  << endl << " Switching to ascii writing" << endl;
204 
205  // Change type to be non-compact format type
206  const word oldTypeName = typeName;
207 
208  const_cast<word&>(typeName) = IOList<T>::typeName;
209 
210  bool good = regIOobject::writeObject(IOstream::ASCII, ver, cmp, valid);
211 
212  // Change type back
213  const_cast<word&>(typeName) = oldTypeName;
214 
215  return good;
216  }
217  else
218  {
219  return regIOobject::writeObject(fmt, ver, cmp, valid);
220  }
221 }
222 
223 
224 template<class T, class BaseType>
226 {
227  return (os << *this).good();
228 }
229 
230 
231 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
232 
233 template<class T, class BaseType>
234 void Foam::CompactIOList<T, BaseType>::operator=
235 (
236  const CompactIOList<T, BaseType>& rhs
237 )
238 {
239  List<T>::operator=(rhs);
240 }
241 
242 
243 template<class T, class BaseType>
245 {
246  List<T>::operator=(rhs);
247 }
248 
249 
250 // * * * * * * * * * * * * * * * Friend Operators * * * * * * * * * * * * * //
251 
252 template<class T, class BaseType>
253 Foam::Istream& Foam::operator>>
254 (
255  Foam::Istream& is,
257 )
258 {
259  // Read compact
260  const labelList start(is);
261  const List<BaseType> elems(is);
262 
263  // Convert
264  L.setSize(start.size()-1);
265 
266  forAll(L, i)
267  {
268  T& subList = L[i];
269 
270  label index = start[i];
271  subList.setSize(start[i+1] - index);
272 
273  forAll(subList, j)
274  {
275  subList[j] = elems[index++];
276  }
277  }
278 
279  return is;
280 }
281 
282 
283 template<class T, class BaseType>
284 Foam::Ostream& Foam::operator<<
285 (
286  Foam::Ostream& os,
288 )
289 {
290  // Keep ascii writing same.
291  if (os.format() == IOstream::ASCII)
292  {
293  os << static_cast<const List<T>&>(L);
294  }
295  else
296  {
297  // Convert to compact format
298  labelList start(L.size()+1);
299 
300  start[0] = 0;
301  for (label i = 1; i < start.size(); i++)
302  {
303  label prev = start[i-1];
304  start[i] = prev+L[i-1].size();
305 
306  if (start[i] < prev)
307  {
309  << "Overall number of elements " << start[i]
310  << " of CompactIOList of size "
311  << L.size() << " overflows the representation of a label"
312  << endl << "Please recompile with a larger representation"
313  << " for label" << exit(FatalIOError);
314  }
315  }
316 
317  List<BaseType> elems(start[start.size()-1]);
318 
319  label elemI = 0;
320  forAll(L, i)
321  {
322  const T& subList = L[i];
323 
324  forAll(subList, j)
325  {
326  elems[elemI++] = subList[j];
327  }
328  }
329  os << start << elems;
330  }
331 
332  return os;
333 }
334 
335 
336 // ************************************************************************* //
A simple container for copying or transferring objects of type <T>.
Definition: Xfer.H:85
virtual bool writeObject(IOstream::streamFormat, IOstream::versionNumber, IOstream::compressionType, const bool valid) const
Write using given format, version and compression.
#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:60
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:163
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
points setSize(newPointi)
A List obtained as a section of another List.
Definition: SubList.H:53
A class for handling words, derived from string.
Definition: word.H:59
CompactIOList(const IOobject &)
Construct from IOobject.
Definition: CompactIOList.C:80
streamFormat
Enumeration for the format of data in the stream.
Definition: IOstream.H:86
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
const volScalarField & T
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
void setSize(const label)
Reset size of List.
Definition: List.C:281
#define WarningInFunction
Report a warning using Foam::Warning.
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:331
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:65
void operator=(const CompactIOList< T, BaseType > &)
readOption readOpt() const
Definition: IOobject.H:353
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:92
IOerror FatalIOError