openfoam/applications/test/OTstream/Test-OTstream.C
Mark Olesen 9fd696e1ae ENH: add ITstream append and seek methods.
- ITstream append() would previously have used the append from the
  underlying tokenList, which leaves the tokenIndex untouched and
  renders the freshly appended tokens effectively invisible if
  interspersed with primitiveEntry::read() that itself uses tokenIndex
  when building the list.

  The new append() method makes this hidden ITstream bi-directionality
  easier to manage. For efficiency, we only append lists
  (not individual tokens) and support a 'lazy' resizing that allows
  the final resizing to occur later when all tokens have been appended.

- The new ITstream seek() method provides a conveniently means to move
  to the end of the list or reposition to the middle.
  Using rewind() and using seek(0) are identical.

ENH: added OTstream to output directly to a list of tokens

---

BUG: List::newElem resized incorrectly

- had a simple doubling of the List size without checking that this
  would indeed be sufficient for the requested index.

  Bug was not triggered since primitiveEntry was the only class using
  this call, and it added the tokens sequentially.
2019-12-06 17:23:59 +01:00

143 lines
3.1 KiB
C

/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2019 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 <http://www.gnu.org/licenses/>.
Description
\*---------------------------------------------------------------------------*/
#include "ITstream.H"
#include "OTstream.H"
#include "primitiveFields.H"
#include "argList.H"
using namespace Foam;
void printTokens(const UList<token>& toks)
{
label count = 0;
for (const token& t : toks)
{
Info<< "token: " << t.info() << nl;
++count;
}
Info<< count << " tokens" << nl << endl;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// Main program:
int main(int argc, char *argv[])
{
// Test fields
{
scalarField fld1({1, 2, 3, 4});
OTstream os;
fld1.writeEntry("field", os);
Info<< "Field: " << fld1 << nl
<< "Tokens: " << flatOutput(os) << nl;
printTokens(os);
}
{
scalarField fld1(10, scalar(5));
OTstream os;
fld1.writeEntry("field", os);
Info<< "Field: " << fld1 << nl
<< "Tokens: " << flatOutput(os) << nl;
printTokens(os);
}
{
vector val(1,2, 3);
OTstream os;
os << val;
Info<< "Value: " << val << nl
<< "Tokens: " << flatOutput(os) << nl;
printTokens(os);
}
{
bool val(true);
OTstream os;
os << val;
Info<< "Value: " << val << nl
<< "Tokens: " << flatOutput(os) << nl;
printTokens(os);
}
{
tensorField fld1(1, tensor::I);
OTstream os;
fld1.writeEntry("field", os);
Info<< "Field: " << fld1 << nl
<< "Tokens: " << flatOutput(os) << nl;
printTokens(os);
}
{
labelList fld1(identity(5));
OTstream os;
fld1.writeEntry("field", os);
Info<< "Field: " << fld1 << nl
<< "Tokens: " << flatOutput(os) << nl;
printTokens(os);
}
Info<< "\nEnd\n" << endl;
return 0;
}
// ************************************************************************* //