/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011-2017 OpenFOAM Foundation
\\/ M anipulation | Copyright (C) 2018 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
OpenFOAM is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with OpenFOAM. If not, see .
\*---------------------------------------------------------------------------*/
#include "CompactIOField.H"
#include "labelList.H"
// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
template
void Foam::CompactIOField::readFromStream(const bool valid)
{
Istream& is = readStream(word::null, valid);
if (valid)
{
if (headerClassName() == IOField::typeName)
{
is >> static_cast&>(*this);
close();
}
else if (headerClassName() == typeName)
{
is >> *this;
close();
}
else
{
FatalIOErrorInFunction
(
is
) << "unexpected class name " << headerClassName()
<< " expected " << typeName << " or " << IOField::typeName
<< endl
<< " while reading object " << name()
<< exit(FatalIOError);
}
}
}
// * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * * //
template
Foam::CompactIOField::CompactIOField(const IOobject& io)
:
regIOobject(io)
{
if
(
io.readOpt() == IOobject::MUST_READ
|| (io.readOpt() == IOobject::READ_IF_PRESENT && headerOk())
)
{
readFromStream();
}
}
template
Foam::CompactIOField::CompactIOField
(
const IOobject& io,
const bool valid
)
:
regIOobject(io)
{
if (io.readOpt() == IOobject::MUST_READ)
{
readFromStream(valid);
}
else if (io.readOpt() == IOobject::READ_IF_PRESENT)
{
bool haveFile = headerOk();
readFromStream(valid && haveFile);
}
}
template
Foam::CompactIOField::CompactIOField
(
const IOobject& io,
const label size
)
:
regIOobject(io)
{
if
(
io.readOpt() == IOobject::MUST_READ
|| (io.readOpt() == IOobject::READ_IF_PRESENT && headerOk())
)
{
readFromStream();
}
else
{
Field::setSize(size);
}
}
template
Foam::CompactIOField::CompactIOField
(
const IOobject& io,
const UList& content
)
:
regIOobject(io)
{
if
(
io.readOpt() == IOobject::MUST_READ
|| (io.readOpt() == IOobject::READ_IF_PRESENT && headerOk())
)
{
readFromStream();
}
else
{
Field::operator=(content);
}
}
template
Foam::CompactIOField::CompactIOField
(
const IOobject& io,
Field&& content
)
:
regIOobject(io)
{
Field::transfer(content);
if
(
io.readOpt() == IOobject::MUST_READ
|| (io.readOpt() == IOobject::READ_IF_PRESENT && headerOk())
)
{
readFromStream();
}
}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
template
bool Foam::CompactIOField::writeObject
(
IOstream::streamFormat fmt,
IOstream::versionNumber ver,
IOstream::compressionType cmp,
const bool valid
) const
{
if (fmt == IOstream::ASCII)
{
// Change type to be non-compact format type
const word oldTypeName(typeName);
const_cast(typeName) = IOField::typeName;
bool good = regIOobject::writeObject(IOstream::ASCII, ver, cmp, valid);
// Change type back
const_cast(typeName) = oldTypeName;
return good;
}
return regIOobject::writeObject(fmt, ver, cmp, valid);
}
template
bool Foam::CompactIOField::writeData(Ostream& os) const
{
return (os << *this).good();
}
// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
template
void Foam::CompactIOField::operator=
(
const CompactIOField& rhs
)
{
Field::operator=(rhs);
}
// * * * * * * * * * * * * * * * Friend Operators * * * * * * * * * * * * * //
template
Foam::Istream& Foam::operator>>
(
Foam::Istream& is,
Foam::CompactIOField& L
)
{
// Read compact
const labelList start(is);
const Field elems(is);
// Convert
L.setSize(start.size()-1);
forAll(L, i)
{
T& subField = L[i];
label index = start[i];
subField.setSize(start[i+1] - index);
forAll(subField, j)
{
subField[j] = elems[index++];
}
}
return is;
}
template
Foam::Ostream& Foam::operator<<
(
Foam::Ostream& os,
const Foam::CompactIOField& L
)
{
// Keep ascii writing same.
if (os.format() == IOstream::ASCII)
{
os << static_cast&>(L);
}
else
{
// Convert to compact format
labelList start(L.size()+1);
start[0] = 0;
for (label i = 1; i < start.size(); i++)
{
start[i] = start[i-1]+L[i-1].size();
}
Field elems(start[start.size()-1]);
label elemI = 0;
forAll(L, i)
{
const T& subField = L[i];
forAll(subField, j)
{
elems[elemI++] = subField[j];
}
}
os << start << elems;
}
return os;
}
// ************************************************************************* //