From 06f22a9bf3eda6decf4d85a5b1cce50ddfe74138 Mon Sep 17 00:00:00 2001 From: Mark Olesen <Mark.Olesen@Germany> Date: Mon, 6 Jun 2016 11:51:05 +0100 Subject: [PATCH] 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. --- src/OpenFOAM/db/IOstreams/IOstreams/Ostream.C | 20 ++++--- src/OpenFOAM/db/IOstreams/IOstreams/Ostream.H | 58 ++++++++++++++++--- src/OpenFOAM/db/dictionary/dictionaryIO.C | 4 +- .../GeometricField/GeometricBoundaryField.C | 8 +-- src/OpenFOAM/global/profiling/profiling.C | 51 ++++++---------- .../global/profiling/profilingSysInfo.C | 21 ++----- .../global/profiling/profilingSysInfo.H | 14 ----- .../polyBoundaryMesh/polyBoundaryMesh.C | 4 +- .../meshes/primitiveShapes/plane/plane.C | 11 ++-- .../primitives/functions/Function1/CSV/CSV.C | 37 ++++-------- .../functions/Function1/Sine/Sine.C | 8 +-- .../functions/Function1/Square/Square.C | 10 ++-- .../functions/Function1/TableFile/TableFile.C | 9 +-- 13 files changed, 124 insertions(+), 131 deletions(-) diff --git a/src/OpenFOAM/db/IOstreams/IOstreams/Ostream.C b/src/OpenFOAM/db/IOstreams/IOstreams/Ostream.C index 4131da7af45..754c5fb1859 100644 --- a/src/OpenFOAM/db/IOstreams/IOstreams/Ostream.C +++ b/src/OpenFOAM/db/IOstreams/IOstreams/Ostream.C @@ -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; } diff --git a/src/OpenFOAM/db/IOstreams/IOstreams/Ostream.H b/src/OpenFOAM/db/IOstreams/IOstreams/Ostream.H index 0bd02a5b553..1634689c641 100644 --- a/src/OpenFOAM/db/IOstreams/IOstreams/Ostream.H +++ b/src/OpenFOAM/db/IOstreams/IOstreams/Ostream.H @@ -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'; diff --git a/src/OpenFOAM/db/dictionary/dictionaryIO.C b/src/OpenFOAM/db/dictionary/dictionaryIO.C index d2880798fc4..b9db794b803 100644 --- a/src/OpenFOAM/db/dictionary/dictionaryIO.C +++ b/src/OpenFOAM/db/dictionary/dictionaryIO.C @@ -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; } } diff --git a/src/OpenFOAM/fields/GeometricFields/GeometricField/GeometricBoundaryField.C b/src/OpenFOAM/fields/GeometricFields/GeometricField/GeometricBoundaryField.C index fe43d9225db..7e273aa9fd2 100644 --- a/src/OpenFOAM/fields/GeometricFields/GeometricField/GeometricBoundaryField.C +++ b/src/OpenFOAM/fields/GeometricFields/GeometricField/GeometricBoundaryField.C @@ -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 diff --git a/src/OpenFOAM/global/profiling/profiling.C b/src/OpenFOAM/global/profiling/profiling.C index 64710228505..10c61852ad0 100644 --- a/src/OpenFOAM/global/profiling/profiling.C +++ b/src/OpenFOAM/global/profiling/profiling.C @@ -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; } diff --git a/src/OpenFOAM/global/profiling/profilingSysInfo.C b/src/OpenFOAM/global/profiling/profilingSysInfo.C index 9b6ff2f34b9..51b06017364 100644 --- a/src/OpenFOAM/global/profiling/profilingSysInfo.C +++ b/src/OpenFOAM/global/profiling/profilingSysInfo.C @@ -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"); diff --git a/src/OpenFOAM/global/profiling/profilingSysInfo.H b/src/OpenFOAM/global/profiling/profilingSysInfo.H index 26201fac360..b2e84f888f7 100644 --- a/src/OpenFOAM/global/profiling/profilingSysInfo.H +++ b/src/OpenFOAM/global/profiling/profilingSysInfo.H @@ -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(); diff --git a/src/OpenFOAM/meshes/polyMesh/polyBoundaryMesh/polyBoundaryMesh.C b/src/OpenFOAM/meshes/polyMesh/polyBoundaryMesh/polyBoundaryMesh.C index aa619792c3a..edb37c727bf 100644 --- a/src/OpenFOAM/meshes/polyMesh/polyBoundaryMesh/polyBoundaryMesh.C +++ b/src/OpenFOAM/meshes/polyMesh/polyBoundaryMesh/polyBoundaryMesh.C @@ -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; diff --git a/src/OpenFOAM/meshes/primitiveShapes/plane/plane.C b/src/OpenFOAM/meshes/primitiveShapes/plane/plane.C index 06a08a35980..99539ee2c68 100644 --- a/src/OpenFOAM/meshes/primitiveShapes/plane/plane.C +++ b/src/OpenFOAM/meshes/primitiveShapes/plane/plane.C @@ -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; } diff --git a/src/OpenFOAM/primitives/functions/Function1/CSV/CSV.C b/src/OpenFOAM/primitives/functions/Function1/CSV/CSV.C index a929f868ee5..9d2d8581744 100644 --- a/src/OpenFOAM/primitives/functions/Function1/CSV/CSV.C +++ b/src/OpenFOAM/primitives/functions/Function1/CSV/CSV.C @@ -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; } diff --git a/src/OpenFOAM/primitives/functions/Function1/Sine/Sine.C b/src/OpenFOAM/primitives/functions/Function1/Sine/Sine.C index 85a7aefe042..a11acd4df83 100644 --- a/src/OpenFOAM/primitives/functions/Function1/Sine/Sine.C +++ b/src/OpenFOAM/primitives/functions/Function1/Sine/Sine.C @@ -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; } diff --git a/src/OpenFOAM/primitives/functions/Function1/Square/Square.C b/src/OpenFOAM/primitives/functions/Function1/Square/Square.C index 9c55e8800e6..0e258bc30ea 100644 --- a/src/OpenFOAM/primitives/functions/Function1/Square/Square.C +++ b/src/OpenFOAM/primitives/functions/Function1/Square/Square.C @@ -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; } diff --git a/src/OpenFOAM/primitives/functions/Function1/TableFile/TableFile.C b/src/OpenFOAM/primitives/functions/Function1/TableFile/TableFile.C index 414e6160772..3682836999f 100644 --- a/src/OpenFOAM/primitives/functions/Function1/TableFile/TableFile.C +++ b/src/OpenFOAM/primitives/functions/Function1/TableFile/TableFile.C @@ -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; } -- GitLab