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 | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2011-2017 OpenFOAM Foundation
9  Copyright (C) 2015-2024 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 \*---------------------------------------------------------------------------*/
28 
29 #include "CompactIOList.H"
30 #include "labelList.H"
31 
32 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
33 
34 template<class T, class BaseType>
36 {
37  if
38  (
39  readOpt() == IOobject::MUST_READ
40  || (isReadOptional() && headerOk())
41  )
42  {
43  Istream& is = readStream(word::null);
44 
45  if (headerClassName() == IOList<T>::typeName)
46  {
47  is >> static_cast<List<T>&>(*this);
48  close();
49  }
50  else if (headerClassName() == typeName)
51  {
52  is >> *this;
53  close();
54  }
55  else
56  {
58  << "Unexpected class name " << headerClassName()
59  << " expected " << typeName
60  << " or " << IOList<T>::typeName << endl
61  << " while reading object " << name()
62  << exit(FatalIOError);
63  }
64 
65  return true;
66  }
67 
68  return false;
69 }
70 
71 
72 template<class T, class BaseType>
74 {
75  const List<T>& lists = *this;
76 
77  label total = 0;
78  for (const auto& sublist : lists)
79  {
80  const label prev = total;
81  total += sublist.size();
82  if (total < prev)
83  {
84  return true;
85  }
86  }
87  return false;
88 }
89 
90 
91 // * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * * //
92 
93 template<class T, class BaseType>
95 :
97 {
98  readIOcontents();
99 }
100 
101 
102 template<class T, class BaseType>
104 (
105  const IOobject& io,
106  Foam::zero
107 )
108 :
109  regIOobject(io)
110 {
111  readIOcontents();
112 }
113 
114 
115 template<class T, class BaseType>
117 (
118  const IOobject& io,
119  const label len
120 )
121 :
122  regIOobject(io)
123 {
124  if (!readIOcontents())
125  {
127  }
128 }
129 
130 
131 template<class T, class BaseType>
133 (
134  const IOobject& io,
135  const UList<T>& content
136 )
137 :
138  regIOobject(io)
139 {
140  if (!readIOcontents())
141  {
143  }
144 }
145 
146 
147 template<class T, class BaseType>
149 (
150  const IOobject& io,
151  List<T>&& content
152 )
153 :
154  regIOobject(io)
155 {
156  List<T>::transfer(content);
157 
158  readIOcontents();
159 }
160 
161 
162 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
163 
164 template<class T, class BaseType>
166 (
167  IOstreamOption streamOpt,
168  const bool writeOnProc
169 ) const
170 {
171  if
172  (
173  streamOpt.format() == IOstreamOption::BINARY
174  && overflows()
175  )
176  {
177  streamOpt.format(IOstreamOption::ASCII);
178 
180  << "Overall number of elements of CompactIOList of size "
181  << this->size() << " overflows the representation of a label"
182  << nl << " Switching to ascii writing" << endl;
183  }
184 
185  if (streamOpt.format() == IOstreamOption::ASCII)
186  {
187  // Change type to be non-compact format type
188  const word oldTypeName(typeName);
189 
190  const_cast<word&>(typeName) = IOList<T>::typeName;
191 
192  bool good = regIOobject::writeObject(streamOpt, writeOnProc);
193 
194  // Change type back
195  const_cast<word&>(typeName) = oldTypeName;
196 
197  return good;
198  }
199 
200  return regIOobject::writeObject(streamOpt, writeOnProc);
201 }
202 
203 
204 template<class T, class BaseType>
206 {
207  return (os << *this).good();
208 }
209 
210 
211 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
212 
213 template<class T, class BaseType>
215 (
216  const CompactIOList<T, BaseType>& rhs
217 )
218 {
219  List<T>::operator=(rhs);
220 }
221 
222 
223 // * * * * * * * * * * * * * * * Friend Operators * * * * * * * * * * * * * //
224 
225 template<class T, class BaseType>
226 Foam::Istream& Foam::operator>>
227 (
228  Foam::Istream& is,
230 )
231 {
232  // Read compact
233  const labelList start(is);
234  const List<BaseType> elems(is);
235 
236  // Convert
237  L.setSize(start.size()-1);
238 
239  forAll(L, i)
240  {
241  T& subList = L[i];
242 
243  label index = start[i];
244  subList.setSize(start[i+1] - index);
245 
246  forAll(subList, j)
247  {
248  subList[j] = elems[index++];
249  }
250  }
251 
252  return is;
253 }
254 
255 
256 template<class T, class BaseType>
257 Foam::Ostream& Foam::operator<<
258 (
259  Foam::Ostream& os,
261 )
262 {
263  // Keep ASCII writing same
264  if (os.format() == IOstreamOption::ASCII)
265  {
266  os << static_cast<const List<T>&>(L);
267  }
268  else
269  {
270  // Convert to compact format
271  labelList start(L.size()+1);
272 
273  start[0] = 0;
274  for (label i = 1; i < start.size(); i++)
275  {
276  const label prev = start[i-1];
277  start[i] = prev+L[i-1].size();
278 
279  if (start[i] < prev)
280  {
282  << "Overall number of elements " << start[i]
283  << " of CompactIOList of size "
284  << L.size() << " overflows the representation of a label"
285  << endl << "Please recompile with a larger representation"
286  << " for label" << exit(FatalIOError);
287  }
288  }
289 
290  List<BaseType> elems(start[start.size()-1]);
291 
292  label elemI = 0;
293  forAll(L, i)
294  {
295  const T& subList = L[i];
296 
297  forAll(subList, j)
298  {
299  elems[elemI++] = subList[j];
300  }
301  }
302  os << start << elems;
303  }
304 
305  return os;
306 }
307 
308 
309 // ************************************************************************* //
void size(const label n)
Older name for setAddressableSize.
Definition: UList.H:116
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:125
patchWriters resize(patchIds.size())
const vector L(dict.get< vector >("L"))
CompactIOList(const CompactIOList &)=default
Default copy construct.
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: BitOps.H:56
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:50
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:531
A simple container for options an IOstream can normally have.
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:421
virtual bool writeData(Ostream &os) const
Pure virtual writeData function.
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for expressions::valueTypeCode::INVALID.
Definition: exprTraits.C:127
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:56
virtual bool writeObject(IOstreamOption streamOpt, const bool writeOnProc) const
Write using stream options. Checks for overflow in binary.
OBJstream os(runTime.globalPath()/outputName)
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:48
const volScalarField & T
#define WarningInFunction
Report a warning using Foam::Warning.
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:637
regIOobject is an abstract class derived from IOobject to handle automatic object registration with t...
Definition: regIOobject.H:68
A class representing the concept of 0 (zero) that can be used to avoid manipulating objects known to ...
Definition: zero.H:57
List< label > labelList
A List of labels.
Definition: List.H:62
IOobject io("surfaceFilmProperties", mesh.time().constant(), mesh, IOobject::READ_IF_PRESENT, IOobject::NO_WRITE, IOobject::NO_REGISTER)
Defines the attributes of an object for which implicit objectRegistry management is supported...
Definition: IOobject.H:180
streamFormat format() const noexcept
Get the current stream format.
IOerror FatalIOError
Error stream (stdout output on all processes), with additional &#39;FOAM FATAL IO ERROR&#39; header text and ...