Commit 06f22a9b authored by Mark Olesen's avatar Mark Olesen
Browse files

ENH: cleanup Ostream to ease usage (issue #254)

- Include newline in beginBlock/endBlock, since this corresponds to
  the standard usage. The beginBlock now takes keyType instead of word.

- Provide Ostream::writeEntry method to reduce clutter and simplify
  writing of entries.

  Before
  ======
      os << indent << "name" << nl
         << indent << token::BEGIN_BLOCK << incrIndent << nl;
      os.writeKeyword("key1") << val1 << token::END_STATEMENT << nl;
      os.writeKeyword("key2") << val2 << token::END_STATEMENT << nl;
      os << decrIndent << indent << token::END_BLOCK << nl;

  After
  =====
      os.beginBlock("name");
      os.writeEntry("key1", val1);
      os.writeEntry("key2", val2);
      os.endBlock();

- For completeness, support inline use of various Ostream methods.
  For example,

      os << beginBlock;
      os.writeEntry("key1", val1);
      os.writeEntry("key2", val2);
      os << endBlock;

- For those who wish to write in long form, can also use endEntry inline:

      os.beginBlock("name");
      os.writeKeyword("key1") << val2 << endEntry;
      os.writeKeyword("key2") << val2 << endEntry;
      os.endBlock();

The endEntry encapsulates a semi-colon, newline combination.
parent 182f0a72
......@@ -79,11 +79,9 @@ Foam::Ostream& Foam::Ostream::writeKeyword(const keyType& kw)
}
Foam::Ostream& Foam::Ostream::beginBlock(const word& keyword)
Foam::Ostream& Foam::Ostream::beginBlock(const keyType& keyword)
{
indent();
write(keyword);
endl();
indent(); write(keyword); write('\n');
beginBlock();
return *this;
......@@ -92,8 +90,7 @@ Foam::Ostream& Foam::Ostream::beginBlock(const word& keyword)
Foam::Ostream& Foam::Ostream::beginBlock()
{
indent();
write(char(token::BEGIN_BLOCK));
indent(); write(char(token::BEGIN_BLOCK)); write('\n');
incrIndent();
return *this;
......@@ -103,8 +100,15 @@ Foam::Ostream& Foam::Ostream::beginBlock()
Foam::Ostream& Foam::Ostream::endBlock()
{
decrIndent();
indent();
write(char(token::END_BLOCK));
indent(); write(char(token::END_BLOCK)); write('\n');
return *this;
}
Foam::Ostream& Foam::Ostream::endEntry()
{
write(char(token::END_STATEMENT)); write('\n');
return *this;
}
......
......@@ -154,33 +154,47 @@ public:
return indentLevel_;
}
//- Incrememt the indent level
//- Increment the indent level
void incrIndent()
{
++indentLevel_;
}
//- Decrememt the indent level
//- Decrement the indent level
void decrIndent();
//- Write the keyword followed by an appropriate indentation
virtual Ostream& writeKeyword(const keyType&);
//- Write begin block group with the given name
// Uses the appropriate indentation,
// does not include a trailing newline.
virtual Ostream& beginBlock(const word&);
// Increments indentation, adds newline.
virtual Ostream& beginBlock(const keyType&);
//- Write begin block group without a name
// Uses the appropriate indentation,
// does not include a trailing newline.
// Increments indentation, adds newline.
virtual Ostream& beginBlock();
//- Write end block group
// Uses the appropriate indentation,
// does not include a trailing newline.
// Decrements indentation, adds newline.
virtual Ostream& endBlock();
//- Write end entry (';') followed by newline.
virtual Ostream& endEntry();
//- Write a keyword/value entry.
// The following two are functionally equivalent:
// \code
// os.writeEntry(key, value);
//
// os.writeKeyword(key) << value << endEntry;
// \endcode
template<class T>
Ostream& writeEntry(const keyType& key, const T& value)
{
writeKeyword(key) << value;
return endEntry();
}
// Stream state functions
......@@ -273,6 +287,32 @@ inline Ostream& endl(Ostream& os)
}
//- Write begin block group without a name
// Increments indentation, adds newline.
inline Ostream& beginBlock(Ostream& os)
{
os.beginBlock();
return os;
}
//- Write end block group
// Decrements indentation, adds newline.
inline Ostream& endBlock(Ostream& os)
{
os.endBlock();
return os;
}
//- Write end entry (';') followed by newline.
inline Ostream& endEntry(Ostream& os)
{
os.endEntry();
return os;
}
// Useful aliases for tab and newline characters
static const char tab = '\t';
static const char nl = '\n';
......
......@@ -175,7 +175,7 @@ void Foam::dictionary::write(Ostream& os, bool subDict) const
if (subDict)
{
os << nl;
os.beginBlock() << nl;
os.beginBlock();
}
forAllConstIter(IDLList<entry>, *this, iter)
......@@ -203,7 +203,7 @@ void Foam::dictionary::write(Ostream& os, bool subDict) const
if (subDict)
{
os.endBlock() << endl;
os.endBlock() << flush;
}
}
......
......@@ -569,16 +569,16 @@ template<class Type, template<class> class PatchField, class GeoMesh>
void Foam::GeometricField<Type, PatchField, GeoMesh>::Boundary::
writeEntry(const word& keyword, Ostream& os) const
{
os.beginBlock(keyword) << nl;
os.beginBlock(keyword);
forAll(*this, patchi)
{
os.beginBlock(this->operator[](patchi).patch().name()) << nl;
os.beginBlock(this->operator[](patchi).patch().name());
os << this->operator[](patchi);
os.endBlock() << endl;
os.endBlock();
}
os.endBlock() << endl;
os.endBlock() << flush;
// Check state of IOstream
os.check
......
......@@ -41,17 +41,6 @@ Foam::label Foam::profiling::Information::nextId_(0);
// * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
// file-scope function
template<class T>
inline static void writeEntry
(
Foam::Ostream& os, const Foam::word& key, const T& value
)
{
os.writeKeyword(key) << value << Foam::token::END_STATEMENT << '\n';
}
Foam::label Foam::profiling::Information::getNextId()
{
return nextId_++;
......@@ -369,7 +358,7 @@ void Foam::profiling::Information::update(const scalar& elapsed)
bool Foam::profiling::writeData(Ostream& os) const
{
os.beginBlock("profiling") << nl; // FUTURE: without nl
os.beginBlock("profiling");
// Add extra new line between entries
label nTrigger = 0;
......@@ -420,22 +409,22 @@ bool Foam::profiling::writeData(Ostream& os) const
}
}
os.endBlock() << nl; // FUTURE: without nl
os.endBlock();
if (sysInfo_)
{
os << nl;
os.beginBlock("sysInfo") << nl; // FUTURE: without nl
os.beginBlock("sysInfo");
sysInfo_->write(os);
os.endBlock() << nl; // FUTURE: without nl
os.endBlock();
}
if (cpuInfo_)
{
os << nl;
os.beginBlock("cpuInfo") << nl; // FUTURE: without nl
os.beginBlock("cpuInfo");
cpuInfo_->write(os);
os.endBlock() << nl; // FUTURE: without nl
os.endBlock();
}
if (memInfo_)
......@@ -443,10 +432,10 @@ bool Foam::profiling::writeData(Ostream& os) const
memInfo_->update();
os << nl;
os.beginBlock("memInfo") << nl; // FUTURE: without nl
os.beginBlock("memInfo");
memInfo_->write(os);
writeEntry(os, "units", "kB");
os.endBlock() << nl; // FUTURE: without nl
os.writeEntry("units", "kB");
os.endBlock();
}
return os;
......@@ -536,26 +525,24 @@ Foam::Ostream& Foam::profiling::Information::write
{
// write in dictionary format
os.beginBlock("trigger" + Foam::name(id_)) << nl; // FUTURE: without nl
// FUTURE: os.writeEntry(key, value);
os.beginBlock(word("trigger" + Foam::name(id_)));
writeEntry(os, "id", id_);
os.writeEntry("id", id_);
if (id_ != parent().id())
{
writeEntry(os, "parentId", parent().id());
os.writeEntry("parentId", parent().id());
}
writeEntry(os, "description", description());
writeEntry(os, "calls", calls() + (offset ? 1 : 0));
writeEntry(os, "totalTime", totalTime() + elapsedTime);
writeEntry(os, "childTime", childTime() + childTimes);
os.writeEntry("description", description());
os.writeEntry("calls", calls() + (offset ? 1 : 0));
os.writeEntry("totalTime", totalTime() + elapsedTime);
os.writeEntry("childTime", childTime() + childTimes);
if (maxMem_)
{
writeEntry(os, "maxMem", maxMem_);
os.writeEntry("maxMem", maxMem_);
}
writeEntry(os, "onStack", Switch(onStack()));
os.writeEntry("onStack", Switch(onStack()));
os.endBlock() << nl; // FUTURE: without nl
os.endBlock();
return os;
}
......
......@@ -29,17 +29,6 @@ License
// * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
// file-scope function
template<class T>
inline static void writeEntry
(
Foam::Ostream& os, const Foam::word& key, const T& value
)
{
os.writeKeyword(key) << value << Foam::token::END_STATEMENT << '\n';
}
// file-scope function
inline static void printEnv
(
......@@ -49,7 +38,7 @@ inline static void printEnv
const std::string value = getEnv(envName);
if (!value.empty())
{
writeEntry(os, key, value);
os.writeEntry(key, value);
}
}
......@@ -74,12 +63,12 @@ Foam::Ostream& Foam::profiling::sysInfo::write
Ostream& os
) const
{
writeEntry(os, "host", hostName(false)); // short name
writeEntry(os, "date", clock::dateTime());
os.writeEntry("host", hostName(false)); // short name
os.writeEntry("date", clock::dateTime());
// compile-time information
writeEntry(os, "version", std::string(FOAMversion));
writeEntry(os, "build", std::string(FOAMbuild));
os.writeEntry("version", std::string(FOAMversion));
os.writeEntry("build", std::string(FOAMbuild));
printEnv(os, "arch", "WM_ARCH");
printEnv(os, "compilerType", "WM_COMPILER_TYPE");
......
......@@ -51,12 +51,6 @@ class Ostream;
class profiling::sysInfo
{
// Private Static Data Members
// Private Data Members
// Private Member Functions
//- Disallow default bitwise copy construct
......@@ -72,9 +66,6 @@ protected:
friend class profiling;
// Member Functions
public:
......@@ -90,11 +81,6 @@ public:
// Member Functions
// Access
// Edit
//- Update it with a new timing information
void update();
......
......@@ -1120,9 +1120,9 @@ bool Foam::polyBoundaryMesh::writeData(Ostream& os) const
forAll(patches, patchi)
{
os.beginBlock(patches[patchi].name()) << nl;
os.beginBlock(patches[patchi].name());
os << patches[patchi];
os.endBlock() << endl;
os.endBlock();
}
os << decrIndent << token::END_LIST;
......
......@@ -449,15 +449,14 @@ Foam::point Foam::plane::mirror(const point& p) const
void Foam::plane::writeDict(Ostream& os) const
{
os.writeKeyword("planeType") << "pointAndNormal"
<< token::END_STATEMENT << nl;
os.writeEntry("planeType", "pointAndNormal");
os.beginBlock("pointAndNormalDict") << nl;
os.beginBlock("pointAndNormalDict");
os.writeKeyword("point") << point_ << token::END_STATEMENT << nl;
os.writeKeyword("normal") << normal_ << token::END_STATEMENT << nl;
os.writeEntry("point", point_);
os.writeEntry("normal", normal_);
os.endBlock() << endl;
os.endBlock() << flush;
}
......
......@@ -263,41 +263,28 @@ template<class Type>
void Foam::Function1Types::CSV<Type>::writeData(Ostream& os) const
{
Function1<Type>::writeData(os);
os << token::END_STATEMENT << nl;
os.endEntry();
os.beginBlock(word(this->name() + "Coeffs")) << nl;
os.beginBlock(word(this->name() + "Coeffs"));
// Note: for TableBase write the dictionary entries it needs but not
// the values themselves
TableBase<Type>::writeEntries(os);
os.writeKeyword("nHeaderLine") << nHeaderLine_
<< token::END_STATEMENT << nl;
os.writeKeyword("refColumn") << refColumn_
<< token::END_STATEMENT << nl;
os.writeEntry("nHeaderLine", nHeaderLine_);
os.writeEntry("refColumn", refColumn_);
// Force writing labelList in ascii
os.writeKeyword("componentColumns");
if (os.format() == IOstream::BINARY)
{
os.format(IOstream::ASCII);
os << componentColumns_;
os.format(IOstream::BINARY);
}
else
{
os << componentColumns_;
}
os << token::END_STATEMENT << nl;
const enum IOstream::streamFormat fmt = os.format();
os.format(IOstream::ASCII);
os.writeEntry("componentColumns", componentColumns_);
os.format(fmt);
os.writeKeyword("separator") << string(separator_)
<< token::END_STATEMENT << nl;
os.writeKeyword("mergeSeparators") << mergeSeparators_
<< token::END_STATEMENT << nl;
os.writeKeyword("fileName") << fName_
<< token::END_STATEMENT << nl;
os.writeEntry("separator", string(separator_));
os.writeEntry("mergeSeparators", mergeSeparators_);
os.writeEntry("fileName", fName_);
os.endBlock() << endl;
os.endBlock() << flush;
}
......
......@@ -89,17 +89,17 @@ template<class Type>
void Foam::Function1Types::Sine<Type>::writeData(Ostream& os) const
{
Function1<Type>::writeData(os);
os << token::END_STATEMENT << nl;
os.endEntry();
os.beginBlock(word(this->name() + "Coeffs")) << nl;
os.beginBlock(word(this->name() + "Coeffs"));
os.writeKeyword("t0") << t0_ << token::END_STATEMENT << nl;
os.writeEntry("t0", t0_);
amplitude_->writeData(os);
frequency_->writeData(os);
scale_->writeData(os);
level_->writeData(os);
os.endBlock() << endl;
os.endBlock() << flush;
}
......
......@@ -102,18 +102,18 @@ template<class Type>
void Foam::Function1Types::Square<Type>::writeData(Ostream& os) const
{
Function1<Type>::writeData(os);
os << token::END_STATEMENT << nl;
os.endEntry();
os.beginBlock(word(this->name() + "Coeffs")) << nl;
os.beginBlock(word(this->name() + "Coeffs"));
os.writeKeyword("t0") << t0_ << token::END_STATEMENT << nl;
os.writeKeyword("markSpace") << markSpace_ << token::END_STATEMENT << nl;
os.writeEntry("t0", t0_);
os.writeEntry("markSpace", markSpace_);
amplitude_->writeData(os);
frequency_->writeData(os);
scale_->writeData(os);
level_->writeData(os);
os.endBlock() << endl;
os.endBlock() << flush;
}
......
......@@ -78,16 +78,17 @@ template<class Type>
void Foam::Function1Types::TableFile<Type>::writeData(Ostream& os) const
{
Function1<Type>::writeData(os);
os << token::END_STATEMENT << nl;
os.endEntry();
os.beginBlock(word(this->name() + "Coeffs")) << nl;
os.beginBlock(word(this->name() + "Coeffs"));
// Note: for TableBase write the dictionary entries it needs but not
// the values themselves
TableBase<Type>::writeEntries(os);
os.writeKeyword("fileName")<< fName_ << token::END_STATEMENT << nl;
os.endBlock() << endl;
os.writeEntry("fileName", fName_);
os.endBlock() << flush;
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment