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 "CompactIOField.H"
27 #include "labelList.H"
29 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
31 template<class T, class BaseType>
32 void Foam::CompactIOField<T, BaseType>::readFromStream()
34 Istream& is = readStream(word::null);
36 if (headerClassName() == IOField<T>::typeName)
38 is >> static_cast<Field<T>&>(*this);
41 else if (headerClassName() == typeName)
50 "CompactIOField<T, BaseType>::readFromStream()",
52 ) << "unexpected class name " << headerClassName()
53 << " expected " << typeName << " or " << IOField<T>::typeName
55 << " while reading object " << name()
56 << exit(FatalIOError);
61 // * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * * //
63 template<class T, class BaseType>
64 Foam::CompactIOField<T, BaseType>::CompactIOField(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::CompactIOField<T, BaseType>::CompactIOField
90 io.readOpt() == IOobject::MUST_READ
91 || (io.readOpt() == IOobject::READ_IF_PRESENT && headerOk())
98 Field<T>::setSize(size);
103 template<class T, class BaseType>
104 Foam::CompactIOField<T, BaseType>::CompactIOField
114 io.readOpt() == IOobject::MUST_READ
115 || (io.readOpt() == IOobject::READ_IF_PRESENT && headerOk())
122 Field<T>::operator=(list);
127 template<class T, class BaseType>
128 Foam::CompactIOField<T, BaseType>::CompactIOField
131 const Xfer<Field<T> >& list
136 Field<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::CompactIOField<T, BaseType>::~CompactIOField()
157 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
159 template<class T, class BaseType>
160 bool Foam::CompactIOField<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) = IOField<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::CompactIOField<T, BaseType>::writeData(Ostream& os) const
191 return (os << *this).good();
195 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
197 template<class T, class BaseType>
198 void Foam::CompactIOField<T, BaseType>::operator=
200 const CompactIOField<T, BaseType>& rhs
203 Field<T>::operator=(rhs);
207 template<class T, class BaseType>
208 void Foam::CompactIOField<T, BaseType>::operator=(const Field<T>& rhs)
210 Field<T>::operator=(rhs);
214 // * * * * * * * * * * * * * * * Friend Operators * * * * * * * * * * * * * //
216 template<class T, class BaseType>
217 Foam::Istream& Foam::operator>>
220 Foam::CompactIOField<T, BaseType>& L
224 const labelList start(is);
225 const Field<BaseType> elems(is);
228 L.setSize(start.size()-1);
234 label index = start[i];
235 subField.setSize(start[i+1] - index);
239 subField[j] = elems[index++];
247 template<class T, class BaseType>
248 Foam::Ostream& Foam::operator<<
251 const Foam::CompactIOField<T, BaseType>& L
254 // Keep ascii writing same.
255 if (os.format() == IOstream::ASCII)
257 os << static_cast<const Field<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 Field<BaseType> elems(start[start.size()-1]);
275 const T& subField = L[i];
279 elems[elemI++] = subField[j];
282 os << start << elems;
289 // ************************************************************************* //