1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 \\ / A nd | Copyright (C) 2011 OpenFOAM Foundation
7 -------------------------------------------------------------------------------
9 This file is part of OpenFOAM.
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.
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
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/>.
24 \*---------------------------------------------------------------------------*/
26 #include "CompactIOList.H"
27 #include "labelList.H"
29 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
31 template<class T, class BaseType>
32 void Foam::CompactIOList<T, BaseType>::readFromStream()
34 Istream& is = readStream(word::null);
36 if (headerClassName() == IOList<T>::typeName)
38 is >> static_cast<List<T>&>(*this);
41 else if (headerClassName() == typeName)
50 "CompactIOList<T, BaseType>::readFromStream()",
52 ) << "unexpected class name " << headerClassName()
53 << " expected " << typeName << " or " << IOList<T>::typeName
55 << " while reading object " << name()
56 << exit(FatalIOError);
61 // * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * * //
63 template<class T, class BaseType>
64 Foam::CompactIOList<T, BaseType>::CompactIOList(const IOobject& io)
70 io.readOpt() == IOobject::MUST_READ
71 || (io.readOpt() == IOobject::READ_IF_PRESENT && headerOk())
79 template<class T, class BaseType>
80 Foam::CompactIOList<T, BaseType>::CompactIOList
90 io.readOpt() == IOobject::MUST_READ
91 || (io.readOpt() == IOobject::READ_IF_PRESENT && headerOk())
98 List<T>::setSize(size);
103 template<class T, class BaseType>
104 Foam::CompactIOList<T, BaseType>::CompactIOList
114 io.readOpt() == IOobject::MUST_READ
115 || (io.readOpt() == IOobject::READ_IF_PRESENT && headerOk())
122 List<T>::operator=(list);
127 template<class T, class BaseType>
128 Foam::CompactIOList<T, BaseType>::CompactIOList
131 const Xfer<List<T> >& list
136 List<T>::transfer(list());
140 io.readOpt() == IOobject::MUST_READ
141 || (io.readOpt() == IOobject::READ_IF_PRESENT && headerOk())
149 // * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * * //
151 template<class T, class BaseType>
152 Foam::CompactIOList<T, BaseType>::~CompactIOList()
157 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
159 template<class T, class BaseType>
160 bool Foam::CompactIOList<T, BaseType>::writeObject
162 IOstream::streamFormat fmt,
163 IOstream::versionNumber ver,
164 IOstream::compressionType cmp
167 if (fmt == IOstream::ASCII)
169 // Change type to be non-compact format type
170 const word oldTypeName = typeName;
172 const_cast<word&>(typeName) = IOList<T>::typeName;
174 bool good = regIOobject::writeObject(fmt, ver, cmp);
177 const_cast<word&>(typeName) = oldTypeName;
183 return regIOobject::writeObject(fmt, ver, cmp);
188 template<class T, class BaseType>
189 bool Foam::CompactIOList<T, BaseType>::writeData(Ostream& os) const
191 return (os << *this).good();
195 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
197 template<class T, class BaseType>
198 void Foam::CompactIOList<T, BaseType>::operator=
200 const CompactIOList<T, BaseType>& rhs
203 List<T>::operator=(rhs);
207 template<class T, class BaseType>
208 void Foam::CompactIOList<T, BaseType>::operator=(const List<T>& rhs)
210 List<T>::operator=(rhs);
214 // * * * * * * * * * * * * * * * Friend Operators * * * * * * * * * * * * * //
216 template<class T, class BaseType>
217 Foam::Istream& Foam::operator>>
220 Foam::CompactIOList<T, BaseType>& L
224 const labelList start(is);
225 const List<BaseType> elems(is);
228 L.setSize(start.size()-1);
234 label index = start[i];
235 subList.setSize(start[i+1] - index);
239 subList[j] = elems[index++];
247 template<class T, class BaseType>
248 Foam::Ostream& Foam::operator<<
251 const Foam::CompactIOList<T, BaseType>& L
254 // Keep ascii writing same.
255 if (os.format() == IOstream::ASCII)
257 os << static_cast<const List<T>&>(L);
261 // Convert to compact format
262 labelList start(L.size()+1);
265 for (label i = 1; i < start.size(); i++)
267 start[i] = start[i-1]+L[i-1].size();
270 List<BaseType> elems(start[start.size()-1]);
275 const T& subList = L[i];
279 elems[elemI++] = subList[j];
282 os << start << elems;
289 // ************************************************************************* //