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-2015 OpenFOAM Foundation
6  \\/ M anipulation | Copyright (C) 2015 OpenCFD Ltd.
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  << "unexpected class name " << headerClassName()
50  << " expected " << typeName << " or " << IOList<T>::typeName
51  << endl
52  << " while reading object " << name()
53  << exit(FatalIOError);
54  }
55 }
56 
57 
58 template<class T, class BaseType>
60 {
61  label size = 0;
62  forAll(*this, i)
63  {
64  label oldSize = size;
65  size += this->operator[](i).size();
66  if (size < oldSize)
67  {
68  return true;
69  }
70  }
71  return false;
72 }
73 
74 
75 // * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * * //
76 
77 template<class T, class BaseType>
79 :
80  regIOobject(io)
81 {
82  if
83  (
84  io.readOpt() == IOobject::MUST_READ
85  || (io.readOpt() == IOobject::READ_IF_PRESENT && headerOk())
86  )
87  {
88  readFromStream();
89  }
90 }
91 
92 
93 template<class T, class BaseType>
95 (
96  const IOobject& io,
97  const label size
98 )
99 :
100  regIOobject(io)
101 {
102  if
103  (
104  io.readOpt() == IOobject::MUST_READ
105  || (io.readOpt() == IOobject::READ_IF_PRESENT && headerOk())
106  )
107  {
108  readFromStream();
109  }
110  else
111  {
112  List<T>::setSize(size);
113  }
114 }
115 
116 
117 template<class T, class BaseType>
119 (
120  const IOobject& io,
121  const List<T>& list
122 )
123 :
124  regIOobject(io)
125 {
126  if
127  (
128  io.readOpt() == IOobject::MUST_READ
129  || (io.readOpt() == IOobject::READ_IF_PRESENT && headerOk())
130  )
131  {
132  readFromStream();
133  }
134  else
135  {
136  List<T>::operator=(list);
137  }
138 }
139 
140 
141 template<class T, class BaseType>
143 (
144  const IOobject& io,
145  const Xfer<List<T> >& list
146 )
147 :
148  regIOobject(io)
149 {
150  List<T>::transfer(list());
151 
152  if
153  (
154  io.readOpt() == IOobject::MUST_READ
155  || (io.readOpt() == IOobject::READ_IF_PRESENT && headerOk())
156  )
157  {
158  readFromStream();
159  }
160 }
161 
162 
163 // * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * * //
164 
165 template<class T, class BaseType>
167 {}
168 
169 
170 
171 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
172 
173 template<class T, class BaseType>
175 (
179 ) const
180 {
181  if (fmt == IOstream::ASCII)
182  {
183  // Change type to be non-compact format type
184  const word oldTypeName = typeName;
185 
186  const_cast<word&>(typeName) = IOList<T>::typeName;
187 
188  bool good = regIOobject::writeObject(fmt, ver, cmp);
189 
190  // Change type back
191  const_cast<word&>(typeName) = oldTypeName;
192 
193  return good;
194  }
195  else if (overflows())
196  {
198  << "Overall number of elements of CompactIOList of size "
199  << this->size() << " overflows the representation of a label"
200  << endl << " Switching to ascii writing" << endl;
201 
202  // Change type to be non-compact format type
203  const word oldTypeName = typeName;
204 
205  const_cast<word&>(typeName) = IOList<T>::typeName;
206 
207  bool good = regIOobject::writeObject(IOstream::ASCII, ver, cmp);
208 
209  // Change type back
210  const_cast<word&>(typeName) = oldTypeName;
211 
212  return good;
213  }
214  else
215  {
216  return regIOobject::writeObject(fmt, ver, cmp);
217  }
218 }
219 
220 
221 template<class T, class BaseType>
223 {
224  return (os << *this).good();
225 }
226 
227 
228 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
229 
230 template<class T, class BaseType>
232 (
233  const CompactIOList<T, BaseType>& rhs
234 )
235 {
236  List<T>::operator=(rhs);
237 }
238 
239 
240 template<class T, class BaseType>
242 {
243  List<T>::operator=(rhs);
244 }
245 
246 
247 // * * * * * * * * * * * * * * * Friend Operators * * * * * * * * * * * * * //
248 
249 template<class T, class BaseType>
250 Foam::Istream& Foam::operator>>
251 (
252  Foam::Istream& is,
254 )
255 {
256  // Read compact
257  const labelList start(is);
258  const List<BaseType> elems(is);
259 
260  // Convert
261  L.setSize(start.size()-1);
262 
263  forAll(L, i)
264  {
265  T& subList = L[i];
266 
267  label index = start[i];
268  subList.setSize(start[i+1] - index);
269 
270  forAll(subList, j)
271  {
272  subList[j] = elems[index++];
273  }
274  }
275 
276  return is;
277 }
278 
279 
280 template<class T, class BaseType>
281 Foam::Ostream& Foam::operator<<
282 (
283  Foam::Ostream& os,
285 )
286 {
287  // Keep ascii writing same.
288  if (os.format() == IOstream::ASCII)
289  {
290  os << static_cast<const List<T>&>(L);
291  }
292  else
293  {
294  // Convert to compact format
295  labelList start(L.size()+1);
296 
297  start[0] = 0;
298  for (label i = 1; i < start.size(); i++)
299  {
300  label prev = start[i-1];
301  start[i] = prev+L[i-1].size();
302 
303  if (start[i] < prev)
304  {
306  << "Overall number of elements " << start[i]
307  << " of CompactIOList of size "
308  << L.size() << " overflows the representation of a label"
309  << endl << "Please recompile with a larger representation"
310  << " for label" << exit(FatalIOError);
311  }
312  }
313 
314  List<BaseType> elems(start[start.size()-1]);
315 
316  label elemI = 0;
317  forAll(L, i)
318  {
319  const T& subList = L[i];
320 
321  forAll(subList, j)
322  {
323  elems[elemI++] = subList[j];
324  }
325  }
326  os << start << elems;
327  }
328 
329  return os;
330 }
331 
332 
333 // ************************************************************************* //
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::CompactIOList::overflows
bool overflows() const
Has too many elements in it?
Definition: CompactIOList.C:59
Foam::word
A class for handling words, derived from string.
Definition: word.H:59
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::CompactIOList::writeObject
virtual bool writeObject(IOstream::streamFormat, IOstream::versionNumber, IOstream::compressionType) const
Definition: CompactIOList.C:175
Foam::CompactIOList::CompactIOList
CompactIOList(const IOobject &)
Construct from IOobject.
Foam::IOstream::compressionType
compressionType
Enumeration for the format of data in the stream.
Definition: IOstream.H:193
Foam::FatalIOError
IOerror FatalIOError
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
Foam::IOstream::versionNumber
Version number type.
Definition: IOstream.H:96
Foam::CompactIOList::readFromStream
void readFromStream()
Read according to header type.
Definition: CompactIOList.C:32
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::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::CompactIOList
A List of objects of type <T> with automated input and output using a compact storage....
Definition: CompactIOList.H:53
Foam::CompactIOList::writeData
virtual bool writeData(Ostream &) const
Definition: CompactIOList.C:222
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
Foam::CompactIOList::operator=
void operator=(const CompactIOList< T, BaseType > &)
Foam::regIOobject
regIOobject is an abstract class derived from IOobject to handle automatic object registration with t...
Definition: regIOobject.H:60
T
const volScalarField & T
Definition: createFields.H:25
CompactIOList.H
Foam::List< T >
Foam::IOList
A List of objects of type <T> with automated input and output.
Definition: IOList.H:50
Foam::CompactIOList::~CompactIOList
virtual ~CompactIOList()
Definition: CompactIOList.C:166
FatalIOErrorInFunction
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:330
List
Definition: Test.C:19
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:53
WarningInFunction
#define WarningInFunction
Report a warning using Foam::Warning.
Definition: messageStream.H:259
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