From a5a222f7cff3894fb2f7d8e0fea53de767123e43 Mon Sep 17 00:00:00 2001 From: Mark Olesen <Mark.Olesen@esi-group.com> Date: Tue, 19 Nov 2019 09:00:00 +0100 Subject: [PATCH] ENH: support scalar tokens when testing #if, #ifeq - this allows use of #eval results in the tests --- .../functionEntries/ifEntry/ifEntry.C | 26 +++++++++++++-- .../functionEntries/ifEntry/ifEntry.H | 5 ++- .../functionEntries/ifeqEntry/ifeqEntry.C | 6 ++-- .../functionEntries/ifeqEntry/ifeqEntry.H | 4 +-- tutorials/IO/dictionary/good-if.dict | 14 ++++---- tutorials/IO/dictionary/good-if2.dict | 32 +++++++++++++++++++ 6 files changed, 71 insertions(+), 16 deletions(-) create mode 100644 tutorials/IO/dictionary/good-if2.dict diff --git a/src/OpenFOAM/db/dictionary/functionEntries/ifEntry/ifEntry.C b/src/OpenFOAM/db/dictionary/functionEntries/ifEntry/ifEntry.C index 18a18b2f32f..a0757799485 100644 --- a/src/OpenFOAM/db/dictionary/functionEntries/ifEntry/ifEntry.C +++ b/src/OpenFOAM/db/dictionary/functionEntries/ifEntry/ifEntry.C @@ -6,6 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2018 OpenFOAM Foundation + Copyright (C) 2019 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -51,6 +52,24 @@ namespace functionEntries // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * // +bool Foam::functionEntries::ifEntry::isTrue(ITstream& its) +{ + Switch logic; + + if (its.size() && its.first().isScalar()) + { + // Use default rounding tolerance + logic = Switch(its.first().scalarToken()); + } + else + { + its >> logic; + } + + return logic; +} + + bool Foam::functionEntries::ifEntry::execute ( DynamicList<filePos>& stack, @@ -68,13 +87,14 @@ bool Foam::functionEntries::ifEntry::execute line += ';'; IStringStream lineStream(line); const primitiveEntry e("ifEntry", parentDict, lineStream); - const Switch doIf(e.stream()); - // Info<< "Using #" << typeName << " " << doIf + const bool doIf = ifEntry::isTrue(e.stream()); + + // Info<< "Using #" << typeName << " " << Switch::name(doIf) // << " at line " << stack.last().second() // << " in file " << stack.last().first() << endl; - bool ok = ifeqEntry::execute(doIf, stack, parentDict, is); + const bool ok = ifeqEntry::execute(doIf, stack, parentDict, is); if (stack.size() != nNested) { diff --git a/src/OpenFOAM/db/dictionary/functionEntries/ifEntry/ifEntry.H b/src/OpenFOAM/db/dictionary/functionEntries/ifEntry/ifEntry.H index 769d63e580a..364247dad9b 100644 --- a/src/OpenFOAM/db/dictionary/functionEntries/ifEntry/ifEntry.H +++ b/src/OpenFOAM/db/dictionary/functionEntries/ifEntry/ifEntry.H @@ -43,7 +43,7 @@ Description Note: - only supports single line, '\' is not supported - condition should be readable as a \c Switch - (supports 0,1, true, false, etc.) + (0,1, true, false, etc.) or a scalar (0.0, ...) See also Foam::functionEntries::ifeqEntry @@ -78,6 +78,9 @@ class ifEntry // Private Member Functions + //- Test first token (label, scalar, word) for true/false + static bool isTrue(ITstream& is); + //- Execute the functionEntry in a sub-dict context static bool execute ( diff --git a/src/OpenFOAM/db/dictionary/functionEntries/ifeqEntry/ifeqEntry.C b/src/OpenFOAM/db/dictionary/functionEntries/ifeqEntry/ifeqEntry.C index 154b1a543bb..a4dd8edfab3 100644 --- a/src/OpenFOAM/db/dictionary/functionEntries/ifeqEntry/ifeqEntry.C +++ b/src/OpenFOAM/db/dictionary/functionEntries/ifeqEntry/ifeqEntry.C @@ -27,9 +27,8 @@ License \*---------------------------------------------------------------------------*/ #include "ifeqEntry.H" -#include "stringOps.H" #include "ifEntry.H" -#include "Switch.H" +#include "stringOps.H" #include "addToMemberFunctionSelectionTable.H" // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // @@ -367,9 +366,8 @@ bool Foam::functionEntries::ifeqEntry::execute line += ';'; IStringStream lineStream(line); const primitiveEntry e("ifEntry", parentDict, lineStream); - const Switch doIf(e.stream()); - if (doIf) + if (ifEntry::isTrue(e.stream())) { // Info<< "Using #elif " << doIf << " at line " << lineNo // << " in file " << is.name() << endl; diff --git a/src/OpenFOAM/db/dictionary/functionEntries/ifeqEntry/ifeqEntry.H b/src/OpenFOAM/db/dictionary/functionEntries/ifeqEntry/ifeqEntry.H index 60b23f66bf0..a04b91fc817 100644 --- a/src/OpenFOAM/db/dictionary/functionEntries/ifeqEntry/ifeqEntry.H +++ b/src/OpenFOAM/db/dictionary/functionEntries/ifeqEntry/ifeqEntry.H @@ -139,7 +139,7 @@ protected: ); //- Main driver: depending on 'equal' starts evaluating or - // skips forward to #else + //- skips forward to #else static bool execute ( const bool equal, @@ -149,7 +149,7 @@ protected: ); //- Main driver: depending on 'equal' starts evaluating or - // skips forward to #else + //- skips forward to #else static bool execute ( DynamicList<filePos>& stack, diff --git a/tutorials/IO/dictionary/good-if.dict b/tutorials/IO/dictionary/good-if.dict index 833cd203602..0b7068a9c1d 100644 --- a/tutorials/IO/dictionary/good-if.dict +++ b/tutorials/IO/dictionary/good-if.dict @@ -1,16 +1,16 @@ /*--------------------------------*- C++ -*----------------------------------*\ | ========= | | | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox | -| \\ / O peration | Version: v1906 | -| \\ / A nd | Web: www.OpenFOAM.com | +| \\ / O peration | Version: v1912 | +| \\ / A nd | Website: www.openfoam.com | | \\/ M anipulation | | \*---------------------------------------------------------------------------*/ FoamFile { - version 2; - format ascii; - class dictionary; - object dictionary; + version 2.0; + format ascii; + class dictionary; + object dictionary; } // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // @@ -26,3 +26,5 @@ FoamFile #else version "other"; #endif + +// ************************************************************************* // diff --git a/tutorials/IO/dictionary/good-if2.dict b/tutorials/IO/dictionary/good-if2.dict new file mode 100644 index 00000000000..7b51cf6f32d --- /dev/null +++ b/tutorials/IO/dictionary/good-if2.dict @@ -0,0 +1,32 @@ +/*--------------------------------*- C++ -*----------------------------------*\ +| ========= | | +| \\ / F ield | OpenFOAM: The Open Source CFD Toolbox | +| \\ / O peration | Version: v1912 | +| \\ / A nd | Website: www.openfoam.com | +| \\/ M anipulation | | +\*---------------------------------------------------------------------------*/ +FoamFile +{ + version 2.0; + format ascii; + class dictionary; + object dictionary; +} +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +// Do comparison + +#if #eval "${FOAM_API:-0}" + foamApi nonZero; +#else + foamApi zeroValue; +#endif + +#if #eval "${XX_XXX_FOAM_API:-1000}" + other "some entry"; +#else + other "unexpected"; +#endif + + +// ************************************************************************* // -- GitLab