From 4d8981bef7c59871c973c81ad10f704b751f30c2 Mon Sep 17 00:00:00 2001 From: Henry Weller Date: Tue, 20 Aug 2019 01:39:18 +0100 Subject: [PATCH] functionEntries: Simplified implementation and reduced code duplication --- .../functionEntries/calcEntry/calcEntry.C | 135 +++++++----------- .../functionEntries/calcEntry/calcEntry.H | 9 ++ .../functionEntries/codeStream/codeStream.C | 46 ++---- .../functionEntries/codeStream/codeStream.H | 11 +- .../functionEntry/functionEntry.C | 26 ++++ .../functionEntry/functionEntry.H | 17 +++ .../includeEntry/includeEntry.C | 3 +- .../includeEtcEntry/includeEtcEntry.C | 1 - .../includeIfPresentEntry.C | 4 +- .../functionEntries/negEntry/negEntry.C | 32 ++--- 10 files changed, 146 insertions(+), 138 deletions(-) diff --git a/src/OpenFOAM/db/dictionary/functionEntries/calcEntry/calcEntry.C b/src/OpenFOAM/db/dictionary/functionEntries/calcEntry/calcEntry.C index db5406c92c..51d0a309df 100644 --- a/src/OpenFOAM/db/dictionary/functionEntries/calcEntry/calcEntry.C +++ b/src/OpenFOAM/db/dictionary/functionEntries/calcEntry/calcEntry.C @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | Website: https://openfoam.org - \\ / A nd | Copyright (C) 2011-2018 OpenFOAM Foundation + \\ / A nd | Copyright (C) 2011-2019 OpenFOAM Foundation \\/ M anipulation | ------------------------------------------------------------------------------- License @@ -57,8 +57,61 @@ namespace functionEntries } +// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * // + +Foam::string Foam::functionEntries::calcEntry::calc +( + const dictionary& parentDict, + Istream& is +) +{ + Info<< "Using #calcEntry at line " << is.lineNumber() + << " in file " << parentDict.name() << endl; + + dynamicCode::checkSecurity + ( + "functionEntries::calcEntry::execute(..)", + parentDict + ); + + // Read string + string s(is); + // Make sure we stop this entry + // is.putBack(token(token::END_STATEMENT, is.lineNumber())); + + // Construct codeDict for codeStream + // must reference parent for stringOps::expand to work nicely. + dictionary codeSubDict; + codeSubDict.add("code", "os << (" + s + ");"); + dictionary codeDict(parentDict, codeSubDict); + + codeStream::streamingFunctionType function = codeStream::getFunction + ( + parentDict, + codeDict + ); + + // Use function to write stream + OStringStream os(is.format()); + (*function)(os, parentDict); + + // Return the string containing the results of the calculation + return os.str(); +} + + // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // +bool Foam::functionEntries::calcEntry::execute +( + dictionary& parentDict, + Istream& is +) +{ + return insert(parentDict, calc(parentDict, is)); +} + + bool Foam::functionEntries::calcEntry::execute ( const dictionary& parentDict, @@ -66,85 +119,7 @@ bool Foam::functionEntries::calcEntry::execute Istream& is ) { - Info<< "Using #calcEntry at line " << is.lineNumber() - << " in file " << parentDict.name() << endl; - - dynamicCode::checkSecurity - ( - "functionEntries::calcEntry::execute(..)", - parentDict - ); - - // Read string - string s(is); - // Make sure we stop this entry - // is.putBack(token(token::END_STATEMENT, is.lineNumber())); - - // Construct codeDict for codeStream - // must reference parent for stringOps::expand to work nicely. - dictionary codeSubDict; - codeSubDict.add("code", "os << (" + s + ");"); - dictionary codeDict(parentDict, codeSubDict); - - codeStream::streamingFunctionType function = codeStream::getFunction - ( - parentDict, - codeDict - ); - - // use function to write stream - OStringStream os(is.format()); - (*function)(os, parentDict); - - // get the entry from this stream - IStringStream resultStream(os.str()); - thisEntry.read(parentDict, resultStream); - - return true; -} - - -bool Foam::functionEntries::calcEntry::execute -( - dictionary& parentDict, - Istream& is -) -{ - Info<< "Using #calcEntry at line " << is.lineNumber() - << " in file " << parentDict.name() << endl; - - dynamicCode::checkSecurity - ( - "functionEntries::calcEntry::execute(..)", - parentDict - ); - - // Read string - string s(is); - // Make sure we stop this entry - // is.putBack(token(token::END_STATEMENT, is.lineNumber())); - - // Construct codeDict for codeStream - // must reference parent for stringOps::expand to work nicely. - dictionary codeSubDict; - codeSubDict.add("code", "os << (" + s + ");"); - dictionary codeDict(parentDict, codeSubDict); - - codeStream::streamingFunctionType function = codeStream::getFunction - ( - parentDict, - codeDict - ); - - // use function to write stream - OStringStream os(is.format()); - (*function)(os, parentDict); - - // get the entry from this stream - IStringStream resultStream(os.str()); - parentDict.read(resultStream); - - return true; + return insert(parentDict, thisEntry, calc(parentDict, is)); } diff --git a/src/OpenFOAM/db/dictionary/functionEntries/calcEntry/calcEntry.H b/src/OpenFOAM/db/dictionary/functionEntries/calcEntry/calcEntry.H index f7d4db3205..38767c2ead 100644 --- a/src/OpenFOAM/db/dictionary/functionEntries/calcEntry/calcEntry.H +++ b/src/OpenFOAM/db/dictionary/functionEntries/calcEntry/calcEntry.H @@ -71,6 +71,15 @@ class calcEntry : public functionEntry { + // Private Member Functions + + //- Perform the calculation and return the resulting string + static string calc + ( + const dictionary& parentDict, + Istream& is + ); + public: diff --git a/src/OpenFOAM/db/dictionary/functionEntries/codeStream/codeStream.C b/src/OpenFOAM/db/dictionary/functionEntries/codeStream/codeStream.C index 1b5f2ade48..84d5d8fe02 100644 --- a/src/OpenFOAM/db/dictionary/functionEntries/codeStream/codeStream.C +++ b/src/OpenFOAM/db/dictionary/functionEntries/codeStream/codeStream.C @@ -362,12 +362,9 @@ Foam::functionEntries::codeStream::getFunction } -// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // - -bool Foam::functionEntries::codeStream::execute +Foam::string Foam::functionEntries::codeStream::run ( const dictionary& parentDict, - primitiveEntry& entry, Istream& is ) { @@ -390,44 +387,31 @@ bool Foam::functionEntries::codeStream::execute OStringStream os(is.format()); (*function)(os, parentDict); - // get the entry from this stream - IStringStream resultStream(os.str()); - entry.read(parentDict, resultStream); - - return true; + // Return the string containing the results of the code execution + return os.str(); } +// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // + bool Foam::functionEntries::codeStream::execute ( dictionary& parentDict, Istream& is ) { - Info<< "Using #codeStream at line " << is.lineNumber() - << " in file " << parentDict.name() << endl; + return insert(parentDict, run(parentDict, is)); +} - dynamicCode::checkSecurity - ( - "functionEntries::codeStream::execute(..)", - parentDict - ); - // get code dictionary - // must reference parent for stringOps::expand to work nicely - dictionary codeDict("#codeStream", parentDict, is); - - streamingFunctionType function = getFunction(parentDict, codeDict); - - // use function to write stream - OStringStream os(is.format()); - (*function)(os, parentDict); - - // get the entry from this stream - IStringStream resultStream(os.str()); - parentDict.read(resultStream); - - return true; +bool Foam::functionEntries::codeStream::execute +( + const dictionary& parentDict, + primitiveEntry& thisEntry, + Istream& is +) +{ + return insert(parentDict, thisEntry, run(parentDict, is)); } diff --git a/src/OpenFOAM/db/dictionary/functionEntries/codeStream/codeStream.H b/src/OpenFOAM/db/dictionary/functionEntries/codeStream/codeStream.H index d4754d715e..63fefe9e1c 100644 --- a/src/OpenFOAM/db/dictionary/functionEntries/codeStream/codeStream.H +++ b/src/OpenFOAM/db/dictionary/functionEntries/codeStream/codeStream.H @@ -114,9 +114,9 @@ class codeStream : public functionEntry { + //- Interpreter function type + typedef void (*streamingFunctionType)(Ostream&, const dictionary&); - //- Interpreter function type - typedef void (*streamingFunctionType)(Ostream&, const dictionary&); // Private Member Functions @@ -137,6 +137,13 @@ class codeStream const dictionary& codeDict ); + //- Compile and execute the code and return the resulting string + static string run + ( + const dictionary& parentDict, + Istream& is + ); + public: diff --git a/src/OpenFOAM/db/dictionary/functionEntries/functionEntry/functionEntry.C b/src/OpenFOAM/db/dictionary/functionEntries/functionEntry/functionEntry.C index 447f6a0ba7..d48c89fdc9 100644 --- a/src/OpenFOAM/db/dictionary/functionEntries/functionEntry/functionEntry.C +++ b/src/OpenFOAM/db/dictionary/functionEntries/functionEntry/functionEntry.C @@ -58,6 +58,32 @@ Foam::string Foam::functionEntry::readLine(const word& key, Istream& is) } + +// * * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * // + +bool Foam::functionEntry::insert +( + dictionary& parentDict, + const string& str +) +{ + parentDict.read(IStringStream(str)()); + return true; +} + + +bool Foam::functionEntry::insert +( + const dictionary& parentDict, + primitiveEntry& thisEntry, + const string& str +) +{ + thisEntry.read(parentDict, IStringStream(str)()); + return true; +} + + // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // Foam::functionEntry::functionEntry diff --git a/src/OpenFOAM/db/dictionary/functionEntries/functionEntry/functionEntry.H b/src/OpenFOAM/db/dictionary/functionEntries/functionEntry/functionEntry.H index 138876f296..de798d86f8 100644 --- a/src/OpenFOAM/db/dictionary/functionEntries/functionEntry/functionEntry.H +++ b/src/OpenFOAM/db/dictionary/functionEntries/functionEntry/functionEntry.H @@ -70,6 +70,23 @@ class functionEntry //- Read line as string token static string readLine(const word& key, Istream& is); +protected: + + //- Insert the given string in a sub-dict context + static bool insert + ( + dictionary& parentDict, + const string& + ); + + //- Insert the given string in a primitiveEntry context + static bool insert + ( + const dictionary& parentDict, + primitiveEntry& thisEntry, + const string& + ); + public: diff --git a/src/OpenFOAM/db/dictionary/functionEntries/includeEntry/includeEntry.C b/src/OpenFOAM/db/dictionary/functionEntries/includeEntry/includeEntry.C index 612d2c37f5..fff64298df 100644 --- a/src/OpenFOAM/db/dictionary/functionEntries/includeEntry/includeEntry.C +++ b/src/OpenFOAM/db/dictionary/functionEntries/includeEntry/includeEntry.C @@ -130,7 +130,6 @@ bool Foam::functionEntries::includeEntry::execute includeFileName(is.name().path(), rawFName, parentDict) ); - // IFstream ifs(fName); autoPtr ifsPtr(fileHandler().NewIFstream(fName)); ISstream& ifs = ifsPtr(); @@ -190,7 +189,6 @@ bool Foam::functionEntries::includeEntry::execute includeFileName(is.name().path(), rawFName, parentDict) ); - // IFstream ifs(fName); autoPtr ifsPtr(fileHandler().NewIFstream(fName)); ISstream& ifs = ifsPtr(); @@ -217,4 +215,5 @@ bool Foam::functionEntries::includeEntry::execute } } + // ************************************************************************* // diff --git a/src/OpenFOAM/db/dictionary/functionEntries/includeEtcEntry/includeEtcEntry.C b/src/OpenFOAM/db/dictionary/functionEntries/includeEtcEntry/includeEtcEntry.C index e668dd998c..4ce1029b4f 100644 --- a/src/OpenFOAM/db/dictionary/functionEntries/includeEtcEntry/includeEtcEntry.C +++ b/src/OpenFOAM/db/dictionary/functionEntries/includeEtcEntry/includeEtcEntry.C @@ -166,7 +166,6 @@ bool Foam::functionEntries::includeEtcEntry::execute includeEtcFileName(rawFName, parentDict) ); - // IFstream ifs(fName); autoPtr ifsPtr(fileHandler().NewIFstream(fName)); ISstream& ifs = ifsPtr(); diff --git a/src/OpenFOAM/db/dictionary/functionEntries/includeIfPresentEntry/includeIfPresentEntry.C b/src/OpenFOAM/db/dictionary/functionEntries/includeIfPresentEntry/includeIfPresentEntry.C index 4b6a111ea4..c9ff87623a 100644 --- a/src/OpenFOAM/db/dictionary/functionEntries/includeIfPresentEntry/includeIfPresentEntry.C +++ b/src/OpenFOAM/db/dictionary/functionEntries/includeIfPresentEntry/includeIfPresentEntry.C @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | Website: https://openfoam.org - \\ / A nd | Copyright (C) 2011-2018 OpenFOAM Foundation + \\ / A nd | Copyright (C) 2011-2019 OpenFOAM Foundation \\/ M anipulation | ------------------------------------------------------------------------------- License @@ -71,7 +71,6 @@ bool Foam::functionEntries::includeIfPresentEntry::execute ) { const fileName fName(includeFileName(is, parentDict)); - // IFstream ifs(fName); autoPtr ifsPtr(fileHandler().NewIFstream(fName)); ISstream& ifs = ifsPtr(); @@ -96,7 +95,6 @@ bool Foam::functionEntries::includeIfPresentEntry::execute ) { const fileName fName(includeFileName(is, parentDict)); - // IFstream ifs(fName); autoPtr ifsPtr(fileHandler().NewIFstream(fName)); ISstream& ifs = ifsPtr(); diff --git a/src/OpenFOAM/db/dictionary/functionEntries/negEntry/negEntry.C b/src/OpenFOAM/db/dictionary/functionEntries/negEntry/negEntry.C index 61417a07db..2b564473b4 100644 --- a/src/OpenFOAM/db/dictionary/functionEntries/negEntry/negEntry.C +++ b/src/OpenFOAM/db/dictionary/functionEntries/negEntry/negEntry.C @@ -53,7 +53,7 @@ namespace functionEntries } -// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // +// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * // Foam::string Foam::functionEntries::negEntry::negateVariable ( @@ -103,20 +103,7 @@ Foam::string Foam::functionEntries::negEntry::negateVariable } -bool Foam::functionEntries::negEntry::execute -( - const dictionary& parentDict, - primitiveEntry& thisEntry, - Istream& is -) -{ - // Reinsert negated variable into entry - IStringStream resultStream(negateVariable(parentDict, is)); - thisEntry.read(parentDict, resultStream); - - return true; -} - +// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // bool Foam::functionEntries::negEntry::execute ( @@ -124,11 +111,18 @@ bool Foam::functionEntries::negEntry::execute Istream& is ) { - // Reinsert negated variable into dictionary - IStringStream resultStream(negateVariable(parentDict, is)); - parentDict.read(resultStream); + return insert(parentDict, negateVariable(parentDict, is)); +} - return true; + +bool Foam::functionEntries::negEntry::execute +( + const dictionary& parentDict, + primitiveEntry& thisEntry, + Istream& is +) +{ + return insert(parentDict, thisEntry, negateVariable(parentDict, is)); }