From e8a3587df4650b8a76a4fd57acc96d428403ad29 Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Tue, 8 Mar 2011 11:08:42 +0100 Subject: [PATCH] ENH: support file-scope 'localCode' in dynamicCode --- applications/test/codeStream/codeStreamDict1 | 12 +++++++++- .../dynamicCode/codeStreamTemplate.C | 23 +++++++++++++++---- .../fixedValueFvPatchScalarFieldTemplate.C | 9 +++++++- .../dynamicLibrary/dynamicCode/dynamicCode.C | 1 + .../dynamicCode/dynamicCodeContext.C | 12 ++++++++-- .../dynamicCode/dynamicCodeContext.H | 9 ++++++++ 6 files changed, 58 insertions(+), 8 deletions(-) diff --git a/applications/test/codeStream/codeStreamDict1 b/applications/test/codeStream/codeStreamDict1 index ec94ddb173..59eb3d43f6 100644 --- a/applications/test/codeStream/codeStreamDict1 +++ b/applications/test/codeStream/codeStreamDict1 @@ -33,10 +33,20 @@ writeInterval #codeStream -I$(LIB_SRC)/finiteVolume/lnInclude #}; + localCode + #{ + static int someCode() + { + Info<<"called someCode\n"; + return 10; + } + #}; + code #{ label interval = ($endIter - $begIter); - label nDumps = $nDumps; + // label nDumps = $nDumps; + label nDumps = someCode(); os << (interval / nDumps); #}; }; diff --git a/etc/codeTemplates/dynamicCode/codeStreamTemplate.C b/etc/codeTemplates/dynamicCode/codeStreamTemplate.C index 95430364aa..f742a2b230 100644 --- a/etc/codeTemplates/dynamicCode/codeStreamTemplate.C +++ b/etc/codeTemplates/dynamicCode/codeStreamTemplate.C @@ -34,22 +34,37 @@ Description ${codeInclude} //}}} end codeInclude -using namespace Foam; - // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // +namespace Foam +{ + +// * * * * * * * * * * * * * * * Local Functions * * * * * * * * * * * * * * // + +//{{{ begin localCode +${localCode} +//}}} end localCode + + // * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * // extern "C" { - void ${typeName}(Ostream& os, const dictionary& dict) + void ${typeName} + ( + Ostream& os, + const dictionary& dict + ) { //{{{ begin code - ${code}; + ${code} //}}} end code } } + // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // +} // End namespace Foam + // ************************************************************************* // diff --git a/etc/codeTemplates/dynamicCode/fixedValueFvPatchScalarFieldTemplate.C b/etc/codeTemplates/dynamicCode/fixedValueFvPatchScalarFieldTemplate.C index d9626d0a69..280ee4810d 100644 --- a/etc/codeTemplates/dynamicCode/fixedValueFvPatchScalarFieldTemplate.C +++ b/etc/codeTemplates/dynamicCode/fixedValueFvPatchScalarFieldTemplate.C @@ -38,6 +38,13 @@ ${codeInclude} namespace Foam { +// * * * * * * * * * * * * * * * Local Functions * * * * * * * * * * * * * * // + +//{{{ begin localCode +${localCode} +//}}} end localCode + + // * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * // extern "C" @@ -188,7 +195,7 @@ void ${typeName}FixedValueFvPatchScalarField::updateCoeffs() } //{{{ begin code - ${code}; + ${code} //}}} end code fixedValueFvPatchScalarField::updateCoeffs(); diff --git a/src/OpenFOAM/db/dynamicLibrary/dynamicCode/dynamicCode.C b/src/OpenFOAM/db/dynamicLibrary/dynamicCode/dynamicCode.C index 0cefb5fb0e..00b94cb426 100644 --- a/src/OpenFOAM/db/dynamicLibrary/dynamicCode/dynamicCode.C +++ b/src/OpenFOAM/db/dynamicLibrary/dynamicCode/dynamicCode.C @@ -373,6 +373,7 @@ void Foam::dynamicCode::setFilterContext const dynamicCodeContext& context ) { + filterVars_.set("localCode", context.localCode()); filterVars_.set("code", context.code()); filterVars_.set("codeInclude", context.include()); filterVars_.set("SHA1sum", context.sha1().str()); diff --git a/src/OpenFOAM/db/dynamicLibrary/dynamicCode/dynamicCodeContext.C b/src/OpenFOAM/db/dynamicLibrary/dynamicCode/dynamicCodeContext.C index 44ca8f6fda..4031f92144 100644 --- a/src/OpenFOAM/db/dynamicLibrary/dynamicCode/dynamicCodeContext.C +++ b/src/OpenFOAM/db/dynamicLibrary/dynamicCode/dynamicCodeContext.C @@ -35,6 +35,7 @@ Foam::dynamicCodeContext::dynamicCodeContext(const dictionary& dict) : dict_(dict), code_(stringOps::trim(dict["code"])), + localCode_(), include_(), options_() { @@ -45,6 +46,13 @@ Foam::dynamicCodeContext::dynamicCodeContext(const dictionary& dict) // - necessary for compilation options, convenient for includes // and body. + // optional + if (dict.found("localCode")) + { + localCode_ = stringOps::trim(dict["localCode"]); + stringOps::inplaceExpand(localCode_, dict); + } + // optional if (dict.found("codeInclude")) { @@ -59,9 +67,9 @@ Foam::dynamicCodeContext::dynamicCodeContext(const dictionary& dict) stringOps::inplaceExpand(options_, dict); } - // calculate SHA1 digest from include, options, code + // calculate SHA1 digest from include, options, localCode, code OSHA1stream os; - os << include_ << options_ << code_; + os << include_ << options_ << localCode_ << code_; sha1_ = os.digest(); } diff --git a/src/OpenFOAM/db/dynamicLibrary/dynamicCode/dynamicCodeContext.H b/src/OpenFOAM/db/dynamicLibrary/dynamicCode/dynamicCodeContext.H index 2aa248c20f..01aa7e2b05 100644 --- a/src/OpenFOAM/db/dynamicLibrary/dynamicCode/dynamicCodeContext.H +++ b/src/OpenFOAM/db/dynamicLibrary/dynamicCode/dynamicCodeContext.H @@ -57,6 +57,9 @@ class dynamicCodeContext //- Mandatory "code" entry string code_; + //- Optional "localCode" entry + string localCode_; + //- Optional "codeInclude" entry string include_; @@ -99,6 +102,12 @@ public: return code_; } + //- Return the local (file-scope) code + const string& localCode() const + { + return localCode_; + } + //- Return SHA1 digest calculated from include, options, code const SHA1Digest& sha1() const {