Commit 8b3d77ba authored by Mark Olesen's avatar Mark Olesen Committed by Andrew Heather
Browse files

ENH: make OSstream indentation adjustable

- this is principally for cases where reduced indentation is desired,
  such as when streaming to a memory location. If the indentation size
  is zero or one, only a single space will be used to separate the
  key/value.

  This change does not affect the stream allocation size, since the
  extra data falls within the padding.

ENH: relocate label/scalar sizes from Istream to IOstream.

- could allow future use for output streams as well?

  Due to padding, reorganization has no effect on allocated size
  of output streams.

STYLE: add read/write name qualifier to beginRaw, endRaw

- removes ambiguity for bi-directional streams

STYLE: fix inconsistent 'const' qualifier on std::streamsize

- base Ostream was without const, some derived streams with const
parent 6f8da834
......@@ -73,6 +73,17 @@ void printTokens(Istream& is)
}
// Generate some dictionary-like content
template<class OS>
void outputDict(OS& os)
{
os.beginBlock("testDict");
os.writeEntry("bool", "false");
os.writeEntry("scalar", 3.14159);
os.endBlock();
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// Main program:
......@@ -188,6 +199,28 @@ int main(int argc, char *argv[])
<< "content size=" << written.size()
<< " capacity=" << written.capacity() << nl;
Info<< nl << "Test dictionary" << nl;
{
OListStream os1;
outputDict(os1);
Info<< "Regular" << nl;
printInfo(os1);
}
{
OListStream os2;
os2.indentSize() = 0;
outputDict(os2);
Info<< "Compact" << nl;
printInfo(os2);
}
Info<< "\nEnd\n" << endl;
return 0;
......
......@@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd |
\\ / A nd | Copyright (C) 2009-2019 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
| Copyright (C) 2011 OpenFOAM Foundation
......@@ -62,6 +62,19 @@ using namespace Foam;
int main(int argc, char *argv[])
{
cout<<"sizeof\n------\n";
if (true)
{
cout<<"IOstream:" << sizeof(IOstream) << nl;
cout<<"Istream:" << sizeof(Istream) << nl;
cout<<"IPstream:" << sizeof(IPstream) << nl;
cout<<"ISstream:" << sizeof(ISstream) << nl;
cout<<"Ostream:" << sizeof(Ostream) << nl;
cout<<"OPstream:" << sizeof(OPstream) << nl;
cout<<"OSstream:" << sizeof(OSstream) << nl;
}
{
nil x;
cout<<"nil:" << sizeof(x) << nl;
......
......@@ -102,7 +102,7 @@ Foam::Ostream& Foam::IndirectListBase<T, Addr>::writeList
{
// The TOTAL number of bytes to be written.
// - possibly add start delimiter
os.beginRaw(len*sizeof(T));
os.beginRawWrite(len*sizeof(T));
// Contents
for (label i=0; i < len; ++i)
......@@ -115,7 +115,7 @@ Foam::Ostream& Foam::IndirectListBase<T, Addr>::writeList
}
// End delimiter and/or cleanup.
os.endRaw();
os.endRawWrite();
}
}
......
......@@ -298,8 +298,8 @@ Foam::IOobject::IOobject
registerObject_(registerObject),
globalObject_(false),
objState_(GOOD),
labelByteSize_(sizeof(Foam::label)),
scalarByteSize_(sizeof(Foam::scalar))
labelByteSize_(sizeof(label)),
scalarByteSize_(sizeof(scalar))
{
if (objectRegistry::debug)
{
......@@ -334,8 +334,8 @@ Foam::IOobject::IOobject
registerObject_(registerObject),
globalObject_(globalObject),
objState_(GOOD),
labelByteSize_(sizeof(Foam::label)),
scalarByteSize_(sizeof(Foam::scalar))
labelByteSize_(sizeof(label)),
scalarByteSize_(sizeof(scalar))
{
if (objectRegistry::debug)
{
......@@ -368,8 +368,8 @@ Foam::IOobject::IOobject
registerObject_(registerObject),
globalObject_(globalObject),
objState_(GOOD),
labelByteSize_(sizeof(Foam::label)),
scalarByteSize_(sizeof(Foam::scalar))
labelByteSize_(sizeof(label)),
scalarByteSize_(sizeof(scalar))
{
if (!fileNameComponents(path, instance_, local_, name_))
{
......@@ -534,6 +534,8 @@ void Foam::IOobject::operator=(const IOobject& io)
wOpt_ = io.wOpt_;
globalObject_ = io.globalObject_;
objState_ = io.objState_;
labelByteSize_ = io.labelByteSize_;
scalarByteSize_ = io.scalarByteSize_;
}
......
......@@ -95,7 +95,6 @@ class objectRegistry;
class IOobject
{
public:
// Public data types
......@@ -155,7 +154,7 @@ private:
//- Local path component
fileName local_;
//- objectRegistry reference
//- Reference to the objectRegistry
const objectRegistry& db_;
//- Read option
......@@ -230,7 +229,7 @@ public:
static inline word groupName(StringType name, const word& group);
//- Return group (extension part of name)
//- Return group (extension part of name)
static word group(const word& name);
//- Return member (name without the extension)
......
......@@ -95,8 +95,8 @@ bool Foam::IOobject::readHeader(Istream& is)
// The note entry is optional
headerDict.readIfPresent("note", note_);
labelByteSize_ = sizeof(Foam::label);
scalarByteSize_ = sizeof(Foam::scalar);
labelByteSize_ = sizeof(label);
scalarByteSize_ = sizeof(scalar);
// The arch information is optional
string arch;
......
......@@ -23,6 +23,9 @@ License
You should have received a copy of the GNU General Public License
along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
Note
File included by global/global.Cver
\*---------------------------------------------------------------------------*/
#include "IOstreamOption.H"
......
......@@ -2,10 +2,8 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2018 OpenCFD Ltd.
\\ / A nd | Copyright (C) 2004-2019 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
| Copyright (C) 2011 OpenFOAM Foundation
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -42,7 +40,7 @@ Description
namespace Foam
{
// Forward declarations
// Forward Declarations
template<class T> class Smanip;
template<class T> class Imanip;
......@@ -65,6 +63,7 @@ inline Ostream& operator<<(Ostream& os, const Omanip<T>& m);
Class Smanip Declaration
\*---------------------------------------------------------------------------*/
//- An IOstream manipulator taking arguments
template<class T>
class Smanip
{
......@@ -104,6 +103,7 @@ inline Ostream& operator<<(Ostream& os, const Smanip<T>& m)
Class Imanip Declaration
\*---------------------------------------------------------------------------*/
//- An Istream manipulator taking arguments
template<class T>
class Imanip
{
......@@ -134,6 +134,7 @@ inline Istream& operator>>(Istream& is, const Imanip<T>& m)
Class Omanip Declaration
\*---------------------------------------------------------------------------*/
//- An Ostream manipulator taking arguments
template<class T>
class Omanip
{
......
......@@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2018 OpenCFD Ltd.
\\ / A nd | Copyright (C) 2018-2019 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
| Copyright (C) 2011-2015 OpenFOAM Foundation
......@@ -80,7 +80,7 @@ public:
// Public Data Types
//- Enumeration for whether the stream open or closed
//- Enumeration for stream open/closed state
enum streamAccess : char
{
CLOSED = 0, //!< stream not open
......@@ -105,6 +105,13 @@ protected:
ios_base::iostate ioState_;
//- The label byte-size (could also be stored as byte)
unsigned short labelByteSize_;
//- The scalar byte-size (could also be stored as byte)
unsigned short scalarByteSize_;
//- The file line
label lineNumber_;
......@@ -147,12 +154,14 @@ public:
IOstreamOption(option),
openClosed_(CLOSED),
ioState_(ios_base::iostate(0)),
labelByteSize_(sizeof(label)),
scalarByteSize_(sizeof(scalar)),
lineNumber_(0)
{
setBad();
}
//- Construct setting format and version
//- Construct with format, version
IOstream
(
streamFormat format,
......@@ -242,6 +251,30 @@ public:
// Stream State Functions
//- The label byte-size associated with the stream
unsigned labelByteSize() const
{
return labelByteSize_;
}
//- The scalar byte-size associated with the stream
unsigned scalarByteSize() const
{
return scalarByteSize_;
}
//- Set the label byte-size associated with the stream
void setLabelByteSize(unsigned nbytes)
{
labelByteSize_ = nbytes;
}
//- Set the scalar byte-size associated with the stream
void setScalarByteSize(unsigned nbytes)
{
scalarByteSize_ = nbytes;
}
//- Const access to the current stream line number
label lineNumber() const
{
......@@ -258,7 +291,7 @@ public:
// \return the previous value
label lineNumber(const label num)
{
label old(lineNumber_);
const label old(lineNumber_);
lineNumber_ = num;
return old;
}
......@@ -345,7 +378,7 @@ public:
};
// Ostream operator
// Ostream Operator
template<>
Ostream& operator<<(Ostream& os, const InfoProxy<IOstream>& ip);
......@@ -355,6 +388,7 @@ Ostream& operator<<(Ostream& os, const InfoProxy<IOstream>& ip);
// ------ Manipulators (not taking arguments)
// --------------------------------------------------------------------
//- An IOstream manipulator
typedef IOstream& (*IOstreamManip)(IOstream&);
//- operator<< handling for manipulators without arguments
......
......@@ -61,7 +61,7 @@ class Istream
:
public IOstream
{
// Private data
// Private Data
//- Has a token been put back on the stream?
bool putBack_;
......@@ -69,12 +69,6 @@ class Istream
//- The last token put back on the stream
token putBackToken_;
//- The label byte-size (could also be stored as byte)
unsigned short labelByteSize_;
//- The scalar byte-size (could also be stored as byte)
unsigned short scalarByteSize_;
public:
......@@ -89,9 +83,7 @@ public:
)
:
IOstream(format, version, compression),
putBack_(false),
labelByteSize_(sizeof(Foam::label)),
scalarByteSize_(sizeof(Foam::scalar))
putBack_(false)
{}
......@@ -101,34 +93,7 @@ public:
// Member Functions
// Characteristics
//- The label byte-size associated with the stream
inline unsigned labelByteSize() const
{
return labelByteSize_;
}
//- The scalar byte-size associated with the stream
inline unsigned scalarByteSize() const
{
return scalarByteSize_;
}
//- Associate a label byte-size with the stream
inline void setLabelByteSize(unsigned val)
{
labelByteSize_ = val;
}
//- Associate a scalar byte-size with the stream
inline void setScalarByteSize(unsigned val)
{
scalarByteSize_ = val;
}
// Read functions
// Read Functions
//- Put back token
// Only a single put back is permitted
......@@ -168,10 +133,10 @@ public:
virtual Istream& read(char*, std::streamsize) = 0;
//- Start of low-level raw binary read
virtual bool beginRaw() = 0;
virtual bool beginRawRead() = 0;
//- End of low-level raw binary read
virtual bool endRaw() = 0;
virtual bool endRawRead() = 0;
//- Low-level raw binary read
virtual Istream& readRaw(char*, std::streamsize) = 0;
......@@ -212,6 +177,7 @@ public:
// ------ Manipulators (not taking arguments)
// --------------------------------------------------------------------
//- An Istream manipulator
typedef Istream& (*IstreamManip)(Istream&);
//- operator>> handling for manipulators without arguments
......
......@@ -38,8 +38,7 @@ void Foam::Ostream::decrIndent()
if (!indentLevel_)
{
std::cerr
<< "Ostream::decrIndent() : attempt to decrement 0 indent level"
<< std::endl;
<< "Ostream::decrIndent() : attempt to decrement 0 indent level\n";
}
else
{
......@@ -59,6 +58,12 @@ Foam::Ostream& Foam::Ostream::writeKeyword(const keyType& kw)
indent();
writeQuoted(kw, kw.isPattern());
if (indentSize_ <= 1)
{
write(char(token::SPACE));
return *this;
}
label nSpaces = entryIndentation_ - label(kw.size());
// Account for quotes surrounding pattern
......
......@@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2016-2018 OpenCFD Ltd.
\\ / A nd | Copyright (C) 2016-2019 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
| Copyright (C) 2011-2016 OpenFOAM Foundation
......@@ -46,7 +46,7 @@ SourceFiles
namespace Foam
{
// Forward declaration of classes
// Forward Declarations
class token;
/*---------------------------------------------------------------------------*\
......@@ -57,19 +57,18 @@ class Ostream
:
public IOstream
{
protected:
// Protected data
//- Number of spaces per indent level
static const unsigned short indentSize_ = 4;
//- Indentation of the entry from the start of the keyword
static const unsigned short entryIndentation_ = 16;
static constexpr const unsigned short entryIndentation_ = 16;
//- Number of spaces per indent level
unsigned short indentSize_ = 4;
//- Current indent level
unsigned short indentLevel_;
unsigned short indentLevel_ = 0;
public:
......@@ -84,8 +83,7 @@ public:
compressionType compression=UNCOMPRESSED
)
:
IOstream(format, version, compression),
indentLevel_(0)
IOstream(format, version, compression)
{}
......@@ -93,9 +91,9 @@ public:
virtual ~Ostream() = default;
// Member functions
// Member Functions
// Write functions
// Write Functions
//- Write token to stream or otherwise handle it.
// \return false if the token type was not handled by this method
......@@ -151,14 +149,26 @@ public:
//- Emit begin marker for low-level raw binary output.
// The count indicates the number of bytes for subsequent
// writeRaw calls.
virtual bool beginRaw(std::streamsize count) = 0;
virtual bool beginRawWrite(std::streamsize count) = 0;
//- Emit end marker for low-level raw binary output.
virtual bool endRaw() = 0;
virtual bool endRawWrite() = 0;
//- Add indentation characters
virtual void indent() = 0;
//- Return indent level
unsigned short indentSize() const
{
return indentSize_;
}
//- Access to indent size
unsigned short& indentSize()
{
return indentSize_;
}
//- Return indent level
unsigned short indentLevel() const
{
......@@ -276,6 +286,7 @@ public:
// ------ Manipulators (not taking arguments)
// --------------------------------------------------------------------
//- An Ostream manipulator
typedef Ostream& (*OstreamManip)(Ostream&);
//- operator<< handling for manipulators without arguments
......
......@@ -384,9 +384,9 @@ Foam::Istream& Foam::UIPstream::read(doubleScalar& val)
Foam::Istream& Foam::UIPstream::read(char* data, std::streamsize count)
{
beginRaw();
beginRawRead();
readRaw(data, count);
endRaw();
endRawRead();
return *this;
}
......@@ -395,7 +395,7 @@ Foam::Istream& Foam::UIPstream::read(char* data, std::streamsize count)
Foam::Istream& Foam::UIPstream::readRaw(char* data, std::streamsize count)
{
// No check for format() == BINARY since this is either done in the
// beginRaw() method, or the caller knows what they are doing.
// beginRawRead() method, or the caller knows what they are doing.
// Any alignment must have been done prior to this call
readFromBuffer(data, count);
......@@ -403,7 +403,7 @@ Foam::Istream& Foam::UIPstream::readRaw(char* data, std::streamsize count)
}
bool Foam::UIPstream::beginRaw()
bool Foam::UIPstream::beginRawRead()
{
if (format() != BINARY)
{
......
......@@ -175,10 +175,10 @@ public:
Istream& readRaw(char* data, std::streamsize count);
//- Start of low-level raw binary read
bool beginRaw();
bool beginRawRead();
//- End of low-level raw binary read
bool endRaw()
bool endRawRead()
{
return true;
}
......
......@@ -323,11 +323,7 @@ Foam::Ostream& Foam::UOPstream::write(const doubleScalar val)
}
Foam::Ostream& Foam::UOPstream::write
(
const char* data,
const std::streamsize count
)
Foam::Ostream& Foam::UOPstream::write(const char* data, std::streamsize count)
{
if (format() != BINARY)
{
......@@ -345,20 +341,20 @@ Foam::Ostream& Foam::UOPstream::write
Foam::Ostream& Foam::UOPstream::writeRaw
(
const char* data,
const std::streamsize count
std::streamsize count
)
{
// No check for format() == BINARY since this is either done in the
// beginRaw() method, or the caller knows what they are doing.
// beginRawWrite() method, or the caller knows what they are doing.
// Previously aligned and sizes reserved via beginRaw()
// Previously aligned and sizes reserved via beginRawWrite()
writeToBuffer(data, count, 1);
return *this;
}
bool Foam::UOPstream::beginRaw(const std::streamsize count)
bool Foam::UOPstream::beginRawWrite(std::streamsize count)
{