Commit 2bd2f83f authored by Mark Olesen's avatar Mark Olesen
Browse files

ENH: cleanup and rationalize memory-backed streams

- more consistent naming:
  * Versions that hold and manage their own memory:
      IListStream, OListStream

  * Versions that reference a fixed size external memory:
      UIListStream, UOListStream

- use List storage instead of DynamicList within OListStream.
  Avoids duplicate bookkeeping, more direct handling of resizing.
parent 5c1ec7ec
Test-IListStream.C
EXE = $(FOAM_USER_APPBIN)/Test-IListStream
/* EXE_INC = */
/* EXE_LIBS = */
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2017 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
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 "ListStream.H"
#include "UListStream.H"
#include "wordList.H"
#include "IOstreams.H"
#include "argList.H"
using namespace Foam;
Ostream& toString(Ostream& os, const UList<char>& list)
{
os << '"';
for (const char c : list)
{
os << c;
}
os << '"';
return os;
}
template<class BufType>
void printInfo(const BufType& buf)
{
Info<< nl << "=========================" << endl;
buf.print(Info);
toString(Info, buf.list());
Info<< nl << "=========================" << endl;
}
void printTokens(Istream& is)
{
label count = 0;
token t;
while (is.good())
{
is >> t;
if (t.good())
{
++count;
Info<<"token: " << t << endl;
}
}
Info<< count << " tokens" << endl;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// Main program:
int main(int argc, char *argv[])
{
// Buffer storage
DynamicList<char> storage(16);
OListStream obuf(std::move(storage));
obuf << 1002 << " " << "abcd" << " " << "def" << " " << 3.14159 << ";\n";
// Move contents to output buffer
printInfo(obuf);
Info<<nl << "as string: ";
toString(Info, obuf.list()) << endl;
Info<< "transfer contents to a List" << endl;
IListStream ibuf(obuf.xfer());
Info<< nl;
Info<< nl << "input string:";
printInfo(ibuf);
Info<< nl << "orig output:";
printInfo(obuf);
printTokens(ibuf);
Info<<nl << "after:";
printInfo(ibuf);
// This should also work
ibuf.list() = 'X';
Info<<nl << "overwritten with const value:";
printInfo(ibuf);
// Can also change content like this:
{
const int n = min(26, ibuf.size());
for (int i=0; i<n; ++i)
{
ibuf.list()[i] = 'A' + i;
}
}
Info<<nl << "directly written:";
printInfo(ibuf);
// But cannot easily swap in/out an entirely new list storage:
//
// List<char> newvalues(52);
// {
// for (int i=0; i<26; ++i)
// {
// newvalues[2*i+0] = char('a' + i);
// newvalues[2*i+1] = char('A' + i);
// }
// }
// ibuf.swap(newvalues);
//
// Info<<nl << "after swap:";
// printInfo(ibuf);
Info<< "\nEnd\n" << endl;
return 0;
}
// ************************************************************************* //
Test-OCountStream.C
EXE = $(FOAM_USER_APPBIN)/Test-OCountStream
/* EXE_INC = */
/* EXE_LIBS = */
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2017 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
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 "OCountStream.H"
#include "StringStream.H"
#include "IOstreams.H"
#include "argList.H"
using namespace Foam;
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// Main program:
int main(int argc, char *argv[])
{
OCountStream cnt;
OStringStream str;
for (label i = 0; i < 50; ++i)
{
str << 1002 << " " << "abcd" << " "
<< "def" << " " << 3.14159 << ";\n";
cnt << 1002 << " " << "abcd" << " "
<< "def" << " " << 3.14159 << ";\n";
}
cnt.print(Info);
Info<< "via string-stream: " << str.str().size() << " chars" << endl;
Info<< "\nEnd\n" << endl;
return 0;
}
// ************************************************************************* //
/* EXE_INC = -I$(LIB_SRC)/finiteVolume/lnInclude */
/* EXE_LIBS = -lfiniteVolume */
/* EXE_INC = */
/* EXE_LIBS = */
......@@ -25,7 +25,7 @@ Description
\*---------------------------------------------------------------------------*/
#include "OListStream.H"
#include "ListStream.H"
#include "wordList.H"
#include "IOstreams.H"
#include "argList.H"
......@@ -45,10 +45,31 @@ Ostream& toString(Ostream& os, const UList<char>& list)
}
void printInfo(const OListStream& buf)
template<class BufType>
void printInfo(const BufType& buf)
{
Info<< nl << buf.size() << " chars (" << buf.capacity() << " capacity) ";
toString(Info, buf.list()) << endl;
Info<< nl << "=========================" << endl;
buf.print(Info);
toString(Info, buf.list());
Info<< nl << "=========================" << endl;
}
void printTokens(Istream& is)
{
label count = 0;
token t;
while (is.good())
{
is >> t;
if (t.good())
{
++count;
Info<<"token: " << t << endl;
}
}
Info<< count << " tokens" << endl;
}
......@@ -58,18 +79,126 @@ void printInfo(const OListStream& buf)
int main(int argc, char *argv[])
{
// Buffer storage
DynamicList<char> storage(8);
DynamicList<char> storage(16);
OListStream obuf(std::move(storage));
obuf << 1002 << " " << "abcd" << " " << "def" << " " << 3.14159 << ";\n";
obuf.setBlockSize(100);
printInfo(obuf);
// Fill with some content
for (label i = 0; i < 50; ++i)
{
obuf<< 1002 << " " << "abcd" << " "
<< "def" << " " << 3.14159 << ";\n";
}
printInfo(obuf);
obuf.rewind();
obuf << 100;
printInfo(obuf);
for (label i=0; i < 10; ++i)
{
obuf << "item" << i << "\n";
}
printInfo(obuf);
obuf.shrink();
Info<< "after shrink" << nl;
printInfo(obuf);
// Add some more
for (label i=10; i < 15; ++i)
{
obuf << "more" << i << nl;
}
Info<< "appended more" << nl;
printInfo(obuf);
// Overwrite at some position
obuf.stdStream().rdbuf()->pubseekpos(0.60 * obuf.size());
obuf << "<" << nl << "OVERWRITE" << nl;
Info<<"after overwrite" << nl;
printInfo(obuf);
Info<< "transfer contents to a List or IListStream" << nl;
IListStream ibuf(obuf.xfer());
Info<<"original:";
printInfo(obuf);
Info<<"new input:" << nl;
printInfo(ibuf);
printTokens(ibuf);
// Create from other storage types
Info<< nl;
{
Info<<"create std::move(List)" << endl;
List<char> list(16, 'A');
Info<<"input:";
toString(Info, list) << endl;
OListStream buf1(std::move(list));
for (label i = 0; i < 26; ++i)
{
buf1 << char('A' +i);
}
for (label i = 0; i < 26; ++i)
{
buf1 << char('a' +i);
}
Info<<"orig:";
toString(Info, list) << endl;
printInfo(buf1);
}
Info<< nl;
List<char> written;
{
Info<<"create List.xfer()" << endl;
List<char> list(16, 'B');
Info<<"input:";
toString(Info, list) << endl;
OListStream buf1(list.xfer());
for (label i = 0; i < 26; ++i)
{
buf1 << char('A' + i);
}
for (label i = 0; i < 26; ++i)
{
buf1 << char('a' +i);
}
Info<<"orig:";
toString(Info, list) << endl;
printInfo(buf1);
// Move back to written
written = buf1.xfer();
printInfo(buf1);
}
Info<<"'captured' content ";
toString(Info, written);
Info<< "\nEnd\n" << endl;
return 0;
......
Test-UIBufStream.C
EXE = $(FOAM_USER_APPBIN)/Test-UIBufStream
/* EXE_INC = -I$(LIB_SRC)/finiteVolume/lnInclude */
/* EXE_LIBS = -lfiniteVolume */
Test-UIListStream.C
EXE = $(FOAM_USER_APPBIN)/Test-UIListStream
/* EXE_INC = */
/* EXE_LIBS = */
......@@ -25,14 +25,54 @@ Description
\*---------------------------------------------------------------------------*/
#include "UIBufStream.H"
#include "UOBufStream.H"
#include "UListStream.H"
#include "wordList.H"
#include "IOstreams.H"
#include "argList.H"
using namespace Foam;
Ostream& toString(Ostream& os, const UList<char>& list)
{
os << '"';
for (const char c : list)
{
os << c;
}
os << '"';
return os;
}
template<class BufType>
void printInfo(const BufType& buf)
{
Info<< nl << "=========================" << endl;
buf.print(Info);
toString(Info, buf.list());
Info<< nl << "=========================" << endl;
}
void printTokens(Istream& is)
{
label count = 0;
token t;
while (is.good())
{
is >> t;
if (t.good())
{
++count;
Info<<"token: " << t << endl;
}
}
Info<< count << " tokens" << endl;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// Main program:
......@@ -41,10 +81,10 @@ int main(int argc, char *argv[])
// Buffer storage
DynamicList<char> storage(1000);
UOBufStream obuf(storage);
UOListStream obuf(storage);
obuf << 1002 << " " << "abcd" << " " << "def" << " " << 3.14159 << ";\n";
Info<<"formatted: " << obuf.size() << " chars" << endl;
obuf.print(Info);
// Match size
storage.resize(obuf.size());
......@@ -53,31 +93,14 @@ int main(int argc, char *argv[])
// Attach input buffer - could also do without previous resize
UIBufStream ibuf(storage, storage.size());
UIListStream ibuf(storage);
token t;
while (ibuf.good())
{
ibuf >> t;
if (t.good())
{
Info<<"token: " << t << endl;
}
}
printTokens(ibuf);
Info<< nl << "Repeat..." << endl;
ibuf.rewind();
while (ibuf.good())
{
ibuf >> t;
if (t.good())
{
Info<<"token: " << t << endl;
}
}
printTokens(ibuf);
Info<< "\nEnd\n" << endl;
......
......@@ -175,6 +175,9 @@ $(hashes)/base64Layer.C
gzstream = $(Streams)/gzstream
$(gzstream)/gzstream.C
memstream = $(Streams)/memory
$(memstream)/ListStream.C
Fstreams = $(Streams)/Fstreams
$(Fstreams)/IFstream.C
$(Fstreams)/OFstream.C
......
......@@ -79,11 +79,6 @@ protected:
{}
//- Destructor
~StringStreamAllocator()
{}
public:
// Public Member Functions
......@@ -112,6 +107,8 @@ class IStringStream
public StringStreamAllocator<std::istringstream>,
public ISstream
{
typedef StringStreamAllocator<std::istringstream> allocator_type;
public:
// Constructors
......@@ -125,7 +122,7 @@ public:
const Foam::string& name="input"
)
:
StringStreamAllocator<std::istringstream>(buffer),
allocator_type(buffer),
ISstream(stream_, name, format, version)
{}
......@@ -139,7 +136,7 @@ public:
const Foam::string& name="input"
)
:
StringStreamAllocator<std::istringstream>(buffer),
allocator_type(buffer),
ISstream(stream_, name, format, version)
{}
......@@ -147,28 +144,23 @@ public:
//- Construct as copy of content
IStringStream(const IStringStream& str)
:
StringStreamAllocator<std::istringstream>(str.str()),
allocator_type(str.str()),
ISstream(stream_, str.name(), str.format(), str.version())
{}
//- Destructor
~IStringStream()
{}
// Member Functions
//- Print description to Ostream
void print(Ostream& os) const;
//- Reset the input buffer and rewind the stream
void reset(const std::string& s)
virtual void reset(const std::string& s)
{
this->str(s);
this->rewind();
}
//- Print description to Ostream
virtual void print(Ostream& os) const;
// Member operators
......@@ -192,6 +184,8 @@ class OStringStream