CompactIOList.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2019 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 "IOList.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<ListCompactIO<T, BaseType>&>(*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  (
69  io.readOpt() == IOobject::MUST_READ
70  || (io.readOpt() == IOobject::READ_IF_PRESENT && headerOk())
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  (
89  io.readOpt() == IOobject::MUST_READ
90  || (io.readOpt() == IOobject::READ_IF_PRESENT && headerOk())
91  )
92  {
93  readFromStream();
94  }
95  else
96  {
97  this->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  (
113  io.readOpt() == IOobject::MUST_READ
114  || (io.readOpt() == IOobject::READ_IF_PRESENT && headerOk())
115  )
116  {
117  readFromStream();
118  }
119  else
120  {
122  }
123 }
124 
125 
126 template<class T, class BaseType>
128 (
129  const IOobject& io,
130  List<T>&& list
131 )
132 :
133  regIOobject(io),
134  ListCompactIO<T, BaseType>(move(list))
135 {
136  if
137  (
138  io.readOpt() == IOobject::MUST_READ
139  || (io.readOpt() == IOobject::READ_IF_PRESENT && headerOk())
140  )
141  {
142  readFromStream();
143  }
144 }
145 
146 
147 template<class T, class BaseType>
149 (
151 )
152 :
153  regIOobject(move(list)),
154  List<T>(move(list))
155 {}
156 
157 
158 // * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * * //
159 
160 template<class T, class BaseType>
162 {}
163 
164 
165 
166 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
167 
168 template<class T, class BaseType>
170 (
174  const bool write
175 ) const
176 {
177  if (fmt == IOstream::ASCII)
178  {
179  // Change type to be non-compact format type
180  const word oldTypeName = typeName;
181 
182  const_cast<word&>(typeName) = IOList<T>::typeName;
183 
184  bool good = regIOobject::writeObject(fmt, ver, cmp, write);
185 
186  // Change type back
187  const_cast<word&>(typeName) = oldTypeName;
188 
189  return good;
190  }
191  else if (this->overflows())
192  {
194  << "Overall number of elements of CompactIOList of size "
195  << this->size() << " overflows the representation of a label"
196  << endl << " Switching to ascii writing" << endl;
197 
198  // Change type to be non-compact format type
199  const word oldTypeName = typeName;
200 
201  const_cast<word&>(typeName) = IOList<T>::typeName;
202 
203  bool good = regIOobject::writeObject(IOstream::ASCII, ver, cmp, write);
204 
205  // Change type back
206  const_cast<word&>(typeName) = oldTypeName;
207 
208  return good;
209  }
210  else
211  {
212  return regIOobject::writeObject(fmt, ver, cmp, write);
213  }
214 }
215 
216 
217 template<class T, class BaseType>
219 {
220  return (os << *this).good();
221 }
222 
223 
224 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
225 
226 template<class T, class BaseType>
227 void Foam::CompactIOList<T, BaseType>::operator=
228 (
229  const CompactIOList<T, BaseType>& rhs
230 )
231 {
233 }
234 
235 
236 template<class T, class BaseType>
237 void Foam::CompactIOList<T, BaseType>::operator=
238 (
240 )
241 {
243 }
244 
245 
246 template<class T, class BaseType>
247 void Foam::CompactIOList<T, BaseType>::operator=
248 (
249  const List<T>& rhs
250 )
251 {
253 }
254 
255 
256 template<class T, class BaseType>
257 void Foam::CompactIOList<T, BaseType>::operator=
258 (
259  List<T>&& rhs
260 )
261 {
263 }
264 
265 
266 // ************************************************************************* //
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
A List of objects of type <T> with input and output using a compact storage. Behaves like List except...
Definition: ListCompactIO.H:52
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
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
A List of objects of type <T> with automated input and output.
Definition: IOList.H:50
points setSize(newPointi)
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
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:54
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:54
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
#define WarningInFunction
Report a warning using Foam::Warning.
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:318
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:52
virtual bool writeObject(IOstream::streamFormat, IOstream::versionNumber, IOstream::compressionType, const bool write) const
Write using given format, version and compression.
readOption readOpt() const
Definition: IOobject.H:365
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:98
IOerror FatalIOError