diff --git a/src/OpenFOAM/Make/files b/src/OpenFOAM/Make/files index 39da0df0b0..3ef3831e16 100644 --- a/src/OpenFOAM/Make/files +++ b/src/OpenFOAM/Make/files @@ -139,6 +139,15 @@ $(strings)/stringOps/toScalar/evalStringToScalarDriver.C $(strings)/stringOps/toScalar/evalStringToScalarLemonParser.lyy-m4 $(strings)/stringOps/toScalar/evalStringToScalarScanner.cc +expr = expressions +$(expr)/exprEntry/expressionEntry.C +$(expr)/exprEntry/expressionEntryBool.C +$(expr)/exprEntry/expressionEntryDimensioned.C +$(expr)/exprEntry/expressionEntryStrings.C +$(expr)/exprEntry/expressionEntryVectors.C +$(expr)/exprString/exprString.C +$(expr)/exprTools/exprTools.C + ops = primitives/ops $(ops)/flipOp.C diff --git a/src/OpenFOAM/expressions/exprEntry/expressionEntry.C b/src/OpenFOAM/expressions/exprEntry/expressionEntry.C new file mode 100644 index 0000000000..09cbf9477f --- /dev/null +++ b/src/OpenFOAM/expressions/exprEntry/expressionEntry.C @@ -0,0 +1,378 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | www.openfoam.com + \\/ M anipulation | +------------------------------------------------------------------------------- + Original code Copyright (C) 2014-2018 Bernhard Gschaider + Copyright (C) 2019 OpenCFD Ltd. +------------------------------------------------------------------------------- +License + This file is part of OpenFOAM. + + OpenFOAM is free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + OpenFOAM is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + along with OpenFOAM. If not, see . + +\*---------------------------------------------------------------------------*/ + +#include "expressionEntry.H" +#include "stringOps.H" +#include "runTimeSelectionTables.H" +#include "addToRunTimeSelectionTable.H" + +// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // + +namespace Foam +{ +namespace exprTools +{ + +defineTypeName(expressionEntry); +defineRunTimeSelectionTable(expressionEntry, empty); + +// Various types can be used directly without any changes + +addNamedToRunTimeSelectionTable +( + expressionEntry, + expressionEntry, + empty, + direct +); + +addNamedToRunTimeSelectionTable +( + expressionEntry, + expressionEntry, + empty, + label +); + +addNamedToRunTimeSelectionTable +( + expressionEntry, + expressionEntry, + empty, + scalar +); + +addNamedToRunTimeSelectionTable +( + expressionEntry, + expressionEntry, + empty, + word +); + +} // End namespace exprTools +} // End namespace Foam + + +// * * * * * * * * * * * * * * * Local Functions * * * * * * * * * * * * * * // + +namespace +{ +// Same code as in stringOps.C + +// Acceptable values for $variable names. +// +// Similar to word::valid(), except we don't have the benefit of a parser +// to filter out other unacceptable entries for us. +// +// Does not currently accept '/' in a variable name. +// We would like "$file/$name" to expand as two variables. +static inline bool validVariableChar(char c) +{ + return + ( + std::isalnum(c) + || c == '.' + || c == ':' + || c == '_' + ); +} + + +// For input string of "$variable with other" return the length of +// the variable. +// +// Intentionally will not capture ':+', ':-' alterations. Use ${ .. } for that +static inline std::string::size_type findVariableLen +( + const std::string& s, + std::string::size_type pos, + const char sigil = '$' +) +{ + std::string::size_type len = 0; + + if (pos < s.length()) + { + if (s[pos] == sigil) + { + // Skip leading '$' in the count! + ++pos; + } + + for + ( + auto iter = s.cbegin() + pos; + iter != s.cend() && validVariableChar(*iter); + ++iter + ) + { + ++len; + } + } + + return len; +} + +} // End anonymous namespace + + +namespace Foam +{ + +inline static const entry* getVariableOrDie +( + const word& name, + const dictionary& dict +) +{ + const entry* eptr = dict.findScoped(name, keyType::LITERAL_RECURSIVE); + + if (!eptr) + { + FatalIOErrorInFunction(dict) + << "No dictionary entry " << name << nl + << exit(FatalIOError); + } + + if (eptr->isDict()) + { + FatalIOErrorInFunction(dict) + << "Found dictionary " << name << " instead of entry" << nl + << exit(FatalIOError); + } + + return eptr; +} + + +} // End namespace Foam + + +// * * * * * * * * * * * * * * * * Selectors * * * * * * * * * * * * * * * * // + +Foam::autoPtr +Foam::exprTools::expressionEntry::New +( + const word& name +) +{ + auto cstrIter = emptyConstructorTablePtr_->cfind(name); + + if (!cstrIter.found()) + { + FatalErrorInLookup + ( + "expressionEntry", + name, + *emptyConstructorTablePtr_ + ) << exit(FatalError); + } + + return autoPtr(cstrIter()()); +} + + +// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // + +Foam::expressions::exprString +Foam::exprTools::expressionEntry::expand +( + const std::string& orig, + const dictionary& dict +) +{ + // This is much like stringOps::inplaceExpand + constexpr const char sigil = '$'; + + // Copy to a exprString, without any validation (using assign) + expressions::exprString s; + s.assign(orig); + + std::string::size_type varBeg = 0; + + while + ( + (varBeg = s.find(sigil, varBeg)) != std::string::npos + // && varBeg < s.size()-1 + ) + { + // No handling of escape characters + + if (varBeg == s.size()-1) + { + // Die if we ended with a '$' + FatalErrorInFunction + << "'" << sigil << "' found at end of " << s + << "(originally " << orig << ')' << nl + << exit(FatalError); + } + + std::string::size_type varEnd = varBeg; + std::string::size_type delim = 0; + + word castTo, varName; + + if (s[varBeg+1] == '[') + { + // An expression pattern with $[...] + + varEnd = s.find(']', varBeg); + delim = 1; + + if (varEnd == std::string::npos) + { + FatalErrorInFunction + << "No correct terminating ']' found in " << s + << " (originally " << orig << ")" << nl + << exit(FatalError); + } + + // Look for embedded (type) cast + + const auto lparen = varBeg+2; + if (lparen < s.size() && s[lparen] == '(') + { + const auto rparen = s.find(')', lparen); + + if (rparen > varEnd) + { + // Handles both "$[( ...]" and "$[( ...])" cases + + auto& err = FatalErrorInFunction; + + if (rparen == std::string::npos) + { + err << "No closing ')' found in "; + } + else + { + err << "Closing ')' found outside of"; + } + + err << " substring " + << s.substr(varBeg, varEnd-varBeg) + << " (" << orig << ')' << nl + << exit(FatalError); + } + + castTo.assign(s.substr(lparen+1, rparen - lparen - 1)); + varName.assign(s.substr(rparen+1, varEnd - rparen - 1)); + } + else + { + varName.assign + ( + s.substr(varBeg + 1 + delim, varEnd - varBeg - 2*delim) + ); + } + + stringOps::inplaceTrim(varName); + } + else + { + if (s[varBeg+1] == '{') + { + varEnd = s.find('}', varBeg); + delim = 1; + } + else + { + // Handling regular $var construct + varEnd += findVariableLen(s, varBeg, sigil); + } + + if (varEnd == std::string::npos) + { + // Likely parsed '${...' without closing '}' - abort + break; + } + else if (varEnd == varBeg) + { + // Parsed '${}' or $badChar - skip over or die? + FatalErrorInFunction + << "No valid character after the $ in " << s + << "(originally " << orig << ")" << endl + << exit(FatalError); + } + else + { + // Assign - assumed to be validated with findVariableLen() + varName.assign + ( + s.substr(varBeg + 1 + delim, varEnd - varBeg - 2*delim) + ); + } + } + + + // Length of original text to replace (incl. decorators) + const auto replaceLen = (varEnd - varBeg + 1); + + const entry* eptr = getVariableOrDie(varName, dict); + + std::string varValue; + + if (castTo.empty()) + { + // Serialized with spaces + varValue = eptr->stream().toString(); + } + else + { + varValue = expressionEntry::New(castTo)->toExpr(*eptr); + } + + s.std::string::replace(varBeg, replaceLen, varValue); + varBeg += varValue.size(); + } + + return s; +} + + +Foam::expressions::exprString +Foam::exprTools::expressionEntry::getExpression +( + const word& name, + const dictionary& dict, + const bool removeComments +) +{ + string str(dict.get(name)); + + if (removeComments) + { + stringOps::inplaceRemoveComments(str); + } + + return expand(str, dict); +} + + +// ************************************************************************* // diff --git a/src/OpenFOAM/expressions/exprEntry/expressionEntry.H b/src/OpenFOAM/expressions/exprEntry/expressionEntry.H new file mode 100644 index 0000000000..48003ca086 --- /dev/null +++ b/src/OpenFOAM/expressions/exprEntry/expressionEntry.H @@ -0,0 +1,177 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | www.openfoam.com + \\/ M anipulation | +------------------------------------------------------------------------------- + Original code Copyright (C) 2014-2018 Bernhard Gschaider + Copyright (C) 2019 OpenCFD Ltd. +------------------------------------------------------------------------------- +License + This file is part of OpenFOAM. + + OpenFOAM is free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + OpenFOAM is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + along with OpenFOAM. If not, see . + +Class + Foam::exprTools::expressionEntry + +Description + Convert dictionary entry to a stringified expression. + + The general OpenFOAM dictionary expansions will result in + space-separated values. For example, + + \verbatim + origin (0.21 0 0.01); + + condition "mag(pos() - $centre) < 0.5"; + \endverbatim + + this will expand to the following: + + \verbatim + condition "mag(pos() - (0.21 0 0.01)) < 0.5"; + \endverbatim + + For these type of expressions, we'd would like better control. + Using instead the special expansions, we can add an effective + type cast. + + \verbatim + condition "mag(pos() - $[(vector)centre]) < 0.5"; + \endverbatim + + which will expand to the following: + + \verbatim + condition "mag(pos() - vector(0.21,0,0.01)) < 0.5"; + \endverbatim + +SourceFiles + expressionEntry.C + expressionEntryI.H + +\*---------------------------------------------------------------------------*/ + +#ifndef exprTools_expressionEntry_H +#define exprTools_expressionEntry_H + +#include "exprString.H" +#include "primitiveEntry.H" +#include "runTimeSelectionTables.H" + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +namespace Foam +{ +namespace exprTools +{ + +/*---------------------------------------------------------------------------*\ + Class expressionEntry Declaration +\*---------------------------------------------------------------------------*/ + +class expressionEntry +{ +protected: + + //- Stringified version of data with comma-separated components. + //- Uses prefix corresponding to the pTraits of Type. + template + static string toExprStr(const Type& data); + + //- Comma-separated stringified version of primitiveEntry of Type. + //- Prefix corresponding to the pTraits of Type + template + static string toExprStr(ITstream& is); + + +public: + + //- Runtime type information + TypeNameNoDebug("expressionEntry"); + + declareRunTimeSelectionTable + ( + autoPtr, + expressionEntry, + empty, + (), + () + ); + + + // Constructors + + //- Construct null + expressionEntry() = default; + + + // Selectors + + //- Return an entry to expression converter + static autoPtr New(const word& name); + + + //- Destructor + virtual ~expressionEntry() = default; + + + // Static Member Functions + + //- Generic concatenate tokens to space-separated string. + inline static string evaluate(const entry& e); + + //- Expand expression with dictionary entries + static expressions::exprString expand + ( + const std::string& str, + const dictionary& dict + ); + + //- Get and expand expression with dictionary entries + // Optionally strip C/C++ comments from the input + static expressions::exprString getExpression + ( + const word& name, + const dictionary& dict, + const bool removeComments = false + ); + + + // Member Functions + + //- To string. Normally with comma separators. + virtual string toExpr(const entry& e) const + { + return evaluate(e); + } +}; + + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +} // End namespace exprTools +} // End namespace Foam + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +#include "expressionEntryI.H" + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +#endif + +// ************************************************************************* // diff --git a/src/OpenFOAM/expressions/exprEntry/expressionEntryBool.C b/src/OpenFOAM/expressions/exprEntry/expressionEntryBool.C new file mode 100644 index 0000000000..3d5fb3b389 --- /dev/null +++ b/src/OpenFOAM/expressions/exprEntry/expressionEntryBool.C @@ -0,0 +1,59 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | www.openfoam.com + \\/ M anipulation | +------------------------------------------------------------------------------- + Copyright (C) 2019 OpenCFD Ltd. +------------------------------------------------------------------------------- +License + This file is part of OpenFOAM. + + OpenFOAM is free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + OpenFOAM is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + along with OpenFOAM. If not, see . + +\*---------------------------------------------------------------------------*/ + +#include "expressionEntryBool.H" +#include "Switch.H" +#include "addToRunTimeSelectionTable.H" + +// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // + +namespace Foam +{ +namespace exprTools +{ + +addNamedToRunTimeSelectionTable +( + expressionEntry, + boolEntry, + empty, + bool +); + +} // End namespace exprTools +} // End namespace Foam + + +// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // + +Foam::string Foam::exprTools::boolEntry::evaluate(const entry& e) +{ + return Switch::name(e.get()); +} + + +// ************************************************************************* // diff --git a/src/OpenFOAM/expressions/exprEntry/expressionEntryBool.H b/src/OpenFOAM/expressions/exprEntry/expressionEntryBool.H new file mode 100644 index 0000000000..785d907197 --- /dev/null +++ b/src/OpenFOAM/expressions/exprEntry/expressionEntryBool.H @@ -0,0 +1,77 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | www.openfoam.com + \\/ M anipulation | +------------------------------------------------------------------------------- + Copyright (C) 2019 OpenCFD Ltd. +------------------------------------------------------------------------------- +License + This file is part of OpenFOAM. + + OpenFOAM is free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + OpenFOAM is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + along with OpenFOAM. If not, see . + +Class + Foam::exprTools::boolEntry + +Description + Expression representation of a bool : (false/true) + +SourceFiles + expressionEntryBool.C + +\*---------------------------------------------------------------------------*/ + +#ifndef exprTools_expressionEntryBool_H +#define exprTools_expressionEntryBool_H + +#include "expressionEntry.H" + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +namespace Foam +{ +namespace exprTools +{ + +/*---------------------------------------------------------------------------*\ + Class boolEntry Declaration +\*---------------------------------------------------------------------------*/ + +struct boolEntry +: + public exprTools::expressionEntry +{ + //- To string + static string evaluate(const entry& e); + + //- To string + virtual string toExpr(const entry& e) const + { + return evaluate(e); + } +}; + + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +} // End namespace exprTools +} // End namespace Foam + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +#endif + +// ************************************************************************* // diff --git a/src/OpenFOAM/expressions/exprEntry/expressionEntryDimensioned.C b/src/OpenFOAM/expressions/exprEntry/expressionEntryDimensioned.C new file mode 100644 index 0000000000..4ea37686e9 --- /dev/null +++ b/src/OpenFOAM/expressions/exprEntry/expressionEntryDimensioned.C @@ -0,0 +1,114 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | www.openfoam.com + \\/ M anipulation | +------------------------------------------------------------------------------- + Original code Copyright (C) 2014-2018 Bernhard Gschaider + Copyright (C) 2019 OpenCFD Ltd. +------------------------------------------------------------------------------- +License + This file is part of OpenFOAM. + + OpenFOAM is free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + OpenFOAM is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + along with OpenFOAM. If not, see . + +\*---------------------------------------------------------------------------*/ + +#include "expressionEntryDimensioned.H" +#include "primitiveEntry.H" +#include "dimensionedScalar.H" +#include "dimensionedVector.H" +#include "dimensionedTensor.H" +#include "dimensionedSymmTensor.H" +#include "dimensionedSphericalTensor.H" +#include "addToRunTimeSelectionTable.H" + +// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // + +namespace Foam +{ +namespace exprTools +{ + +addNamedToRunTimeSelectionTable +( + expressionEntry, + dimensionedScalarEntry, + empty, + dimensionedScalar +); + +addNamedToRunTimeSelectionTable +( + expressionEntry, + dimensionedVectorEntry, + empty, + dimensionedVector +); + +addNamedToRunTimeSelectionTable +( + expressionEntry, + dimensionedTensorEntry, + empty, + dimensionedTensor +); + +addNamedToRunTimeSelectionTable +( + expressionEntry, + dimensionedSymmTensorEntry, + empty, + dimensionedSymmTensor +); + +addNamedToRunTimeSelectionTable +( + expressionEntry, + dimensionedSphericalTensorEntry, + empty, + dimensionedSphericalTensor +); + +} // End namespace exprTools +} // End namespace Foam + + +// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // + +#undef defineExpressionEntryType +#define defineExpressionEntryType(DimType) \ + Foam::string Foam::exprTools::DimType##Entry::evaluate(const entry& e) \ + { \ + DimType dt(dynamicCast(e)); \ + return toExprStr(dt.value()); \ + } + + +Foam::string Foam::exprTools::dimensionedScalarEntry::evaluate(const entry& e) +{ + dimensionedScalar dt(dynamicCast(e)); + return std::to_string(dt.value()); +} + + +defineExpressionEntryType(dimensionedVector); +defineExpressionEntryType(dimensionedTensor); +defineExpressionEntryType(dimensionedSymmTensor); +defineExpressionEntryType(dimensionedSphericalTensor); + +#undef defineExpressionEntryType + +// ************************************************************************* // diff --git a/src/OpenFOAM/expressions/exprEntry/expressionEntryDimensioned.H b/src/OpenFOAM/expressions/exprEntry/expressionEntryDimensioned.H new file mode 100644 index 0000000000..11adf6b73f --- /dev/null +++ b/src/OpenFOAM/expressions/exprEntry/expressionEntryDimensioned.H @@ -0,0 +1,80 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | www.openfoam.com + \\/ M anipulation | +------------------------------------------------------------------------------- + Original code Copyright (C) 2014-2018 Bernhard Gschaider + Copyright (C) 2019 OpenCFD Ltd. +------------------------------------------------------------------------------- +License + This file is part of OpenFOAM. + + OpenFOAM is free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + OpenFOAM is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + along with OpenFOAM. If not, see . + +Description + Creates an expression string from various standard dimensioned types. + +SourceFiles + expressionEntryDimensioned.C + +\*---------------------------------------------------------------------------*/ + +#ifndef exprTools_expressionEntryDimensioned_H +#define exprTools_expressionEntryDimensioned_H + +#include "expressionEntry.H" + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +namespace Foam +{ +namespace exprTools +{ + +#undef declareExpressionEntryType +#define declareExpressionEntryType(DimType) \ + /*! \brief Create an expression string from a DimType as its value */ \ + struct DimType##Entry : public exprTools::expressionEntry \ + { \ + /*! To string */ \ + static string evaluate(const entry& e); \ + \ + /*! To string */ \ + virtual string toExpr(const entry& e) const \ + { \ + return evaluate(e); \ + } \ + }; + + +declareExpressionEntryType(dimensionedScalar); +declareExpressionEntryType(dimensionedVector); +declareExpressionEntryType(dimensionedTensor); +declareExpressionEntryType(dimensionedSymmTensor); +declareExpressionEntryType(dimensionedSphericalTensor); + +#undef declareExpressionEntryType + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +} // End namespace exprTools +} // End namespace Foam + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +#endif + +// ************************************************************************* // diff --git a/src/OpenFOAM/expressions/exprEntry/expressionEntryI.H b/src/OpenFOAM/expressions/exprEntry/expressionEntryI.H new file mode 100644 index 0000000000..5a26b8fb6f --- /dev/null +++ b/src/OpenFOAM/expressions/exprEntry/expressionEntryI.H @@ -0,0 +1,69 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | www.openfoam.com + \\/ M anipulation | +------------------------------------------------------------------------------- + Original code Copyright (C) 2014-2018 Bernhard Gschaider + Copyright (C) 2019 OpenCFD Ltd. +------------------------------------------------------------------------------- +License + This file is part of OpenFOAM. + + OpenFOAM is free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + OpenFOAM is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + along with OpenFOAM. If not, see . + +\*---------------------------------------------------------------------------*/ + +#include "OStringStream.H" + +// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // + +inline Foam::string +Foam::exprTools::expressionEntry::evaluate(const entry& e) +{ + return e.stream().toString(); +} + + +template +inline Foam::string +Foam::exprTools::expressionEntry::toExprStr(const Type& data) +{ + OStringStream buf; + + buf << pTraits::typeName << '('; + for (direction cmpt=0; cmpt < pTraits::nComponents; ++cmpt) + { + if (cmpt) buf << ','; + buf << component(data, cmpt); + } + buf << ')'; + + return buf.str(); +} + + +template +inline Foam::string +Foam::exprTools::expressionEntry::toExprStr(ITstream& is) +{ + Type data(Zero); + is >> data; + + return toExprStr(data); +} + + +// ************************************************************************* // diff --git a/src/OpenFOAM/expressions/exprEntry/expressionEntryStrings.C b/src/OpenFOAM/expressions/exprEntry/expressionEntryStrings.C new file mode 100644 index 0000000000..2afe2d5116 --- /dev/null +++ b/src/OpenFOAM/expressions/exprEntry/expressionEntryStrings.C @@ -0,0 +1,58 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | www.openfoam.com + \\/ M anipulation | +------------------------------------------------------------------------------- + Copyright (C) 2019 OpenCFD Ltd. +------------------------------------------------------------------------------- +License + This file is part of OpenFOAM. + + OpenFOAM is free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + OpenFOAM is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + along with OpenFOAM. If not, see . + +\*---------------------------------------------------------------------------*/ + +#include "expressionEntryStrings.H" +#include "addToRunTimeSelectionTable.H" + +// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // + +namespace Foam +{ +namespace exprTools +{ + +addNamedToRunTimeSelectionTable +( + expressionEntry, + stringEntry, + empty, + string +); + +addNamedToRunTimeSelectionTable +( + expressionEntry, + stringEntry, + empty, + fileName +); + +} // End namespace exprTools +} // End namespace Foam + + +// ************************************************************************* // diff --git a/src/OpenFOAM/expressions/exprEntry/expressionEntryStrings.H b/src/OpenFOAM/expressions/exprEntry/expressionEntryStrings.H new file mode 100644 index 0000000000..b2439c5c0a --- /dev/null +++ b/src/OpenFOAM/expressions/exprEntry/expressionEntryStrings.H @@ -0,0 +1,81 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | www.openfoam.com + \\/ M anipulation | +------------------------------------------------------------------------------- + Original code Copyright (C) 2014-2018 Bernhard Gschaider + Copyright (C) 2019 OpenCFD Ltd. +------------------------------------------------------------------------------- +License + This file is part of OpenFOAM. + + OpenFOAM is free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + OpenFOAM is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + along with OpenFOAM. If not, see . + +Class + Foam::exprTools::stringEntry + +Description + Creates an expression from a string (removing surrounding quotes) + +SourceFiles + expressionEntryStrings.C + +\*---------------------------------------------------------------------------*/ + +#ifndef exprTools_expressionEntryStrings_H +#define exprTools_expressionEntryStrings_H + +#include "expressionEntry.H" + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +namespace Foam +{ +namespace exprTools +{ + +/*---------------------------------------------------------------------------*\ + Class stringEntry Declaration +\*---------------------------------------------------------------------------*/ + +struct stringEntry +: + public exprTools::expressionEntry +{ + //- To string + static string evaluate(const entry& e) + { + return e.get(); + } + + //- To string + virtual string toExpr(const entry& e) const + { + return evaluate(e); + } +}; + + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +} // End namespace exprTools +} // End namespace Foam + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +#endif + +// ************************************************************************* // diff --git a/src/OpenFOAM/expressions/exprEntry/expressionEntryVectors.C b/src/OpenFOAM/expressions/exprEntry/expressionEntryVectors.C new file mode 100644 index 0000000000..cc4af66cd9 --- /dev/null +++ b/src/OpenFOAM/expressions/exprEntry/expressionEntryVectors.C @@ -0,0 +1,95 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | www.openfoam.com + \\/ M anipulation | +------------------------------------------------------------------------------- + Copyright (C) 2019 OpenCFD Ltd. +------------------------------------------------------------------------------- +License + This file is part of OpenFOAM. + + OpenFOAM is free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + OpenFOAM is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + along with OpenFOAM. If not, see . + +\*---------------------------------------------------------------------------*/ + +#include "vector.H" +#include "tensor.H" +#include "symmTensor.H" +#include "sphericalTensor.H" +#include "expressionEntryVectors.H" +#include "addToRunTimeSelectionTable.H" + +// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // + +namespace Foam +{ +namespace exprTools +{ + +addNamedToRunTimeSelectionTable +( + expressionEntry, + vectorEntry, + empty, + vector +); + +addNamedToRunTimeSelectionTable +( + expressionEntry, + tensorEntry, + empty, + tensor +); + +addNamedToRunTimeSelectionTable +( + expressionEntry, + symmTensorEntry, + empty, + symmTensor +); + +addNamedToRunTimeSelectionTable +( + expressionEntry, + sphericalTensorEntry, + empty, + sphericalTensor +); + +} // End namespace exprTools +} // End namespace Foam + + +// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // + +#undef defineExpressionEntryType +#define defineExpressionEntryType(Type) \ + Foam::string Foam::exprTools::Type##Entry::evaluate(const entry& e) \ + { \ + return toExprStr(e.stream()); \ + } + + +defineExpressionEntryType(vector); +defineExpressionEntryType(tensor); +defineExpressionEntryType(symmTensor); +defineExpressionEntryType(sphericalTensor); + +#undef defineExpressionEntryType + +// ************************************************************************* // diff --git a/src/OpenFOAM/expressions/exprEntry/expressionEntryVectors.H b/src/OpenFOAM/expressions/exprEntry/expressionEntryVectors.H new file mode 100644 index 0000000000..781f679db3 --- /dev/null +++ b/src/OpenFOAM/expressions/exprEntry/expressionEntryVectors.H @@ -0,0 +1,79 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | www.openfoam.com + \\/ M anipulation | +------------------------------------------------------------------------------- + Original code Copyright (C) 2014-2018 Bernhard Gschaider + Copyright (C) 2019 OpenCFD Ltd. +------------------------------------------------------------------------------- +License + This file is part of OpenFOAM. + + OpenFOAM is free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + OpenFOAM is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + along with OpenFOAM. If not, see . + +Description + Creates an expression string from common VectorSpace types + +SourceFiles + expressionEntryVectors.C + +\*---------------------------------------------------------------------------*/ + +#ifndef exprTools_expressionEntryVectors_H +#define exprTools_expressionEntryVectors_H + +#include "expressionEntry.H" + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +namespace Foam +{ +namespace exprTools +{ + +#undef declareExpressionEntryType +#define declareExpressionEntryType(Type) \ + /*! \brief Create an expression string from a Type */ \ + struct Type##Entry : public exprTools::expressionEntry \ + { \ + /*! To string */ \ + static string evaluate(const entry& e); \ + \ + /*! To string */ \ + virtual string toExpr(const entry& e) const \ + { \ + return evaluate(e); \ + } \ + }; + + +declareExpressionEntryType(vector); +declareExpressionEntryType(tensor); +declareExpressionEntryType(symmTensor); +declareExpressionEntryType(sphericalTensor); + +#undef declareExpressionEntryType + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +} // End namespace exprTools +} // End namespace Foam + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +#endif + +// ************************************************************************* // diff --git a/src/OpenFOAM/expressions/exprString/exprString.C b/src/OpenFOAM/expressions/exprString/exprString.C new file mode 100644 index 0000000000..9fd594f180 --- /dev/null +++ b/src/OpenFOAM/expressions/exprString/exprString.C @@ -0,0 +1,62 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | www.openfoam.com + \\/ M anipulation | +------------------------------------------------------------------------------- + Copyright (C) 2019 OpenCFD Ltd. +------------------------------------------------------------------------------- +License + This file is part of OpenFOAM. + + OpenFOAM is free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + OpenFOAM is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + along with OpenFOAM. If not, see . + +\*---------------------------------------------------------------------------*/ + +#include "exprString.H" +#include "stringOps.H" +#include "expressionEntry.H" + +// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // + +Foam::expressions::exprString& +Foam::expressions::exprString::expand +( + const dictionary& dict, + const bool removeComments +) +{ + if (removeComments) + { + stringOps::inplaceRemoveComments(*this); + } + + // Not quite as efficient as it could be, but wish to have a copy + // of the original input for the sake of reporting errors + + if (std::string::npos != find('$')) + { + (*this) = exprTools::expressionEntry::expand(*this, dict); + + #ifdef FULLDEBUG + (void)valid(); + #endif + } + + return *this; +} + + +// ************************************************************************* // diff --git a/src/OpenFOAM/expressions/exprString/exprString.H b/src/OpenFOAM/expressions/exprString/exprString.H new file mode 100644 index 0000000000..cd7d09f87c --- /dev/null +++ b/src/OpenFOAM/expressions/exprString/exprString.H @@ -0,0 +1,172 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | www.openfoam.com + \\/ M anipulation | +------------------------------------------------------------------------------- + Original code Copyright (C) 2012-2018 Bernhard Gschaider + Copyright (C) 2019 OpenCFD Ltd. +------------------------------------------------------------------------------- +License + This file is part of OpenFOAM. + + OpenFOAM is free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + OpenFOAM is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + along with OpenFOAM. If not, see . + +Namespace + Foam::expressions::exprString + +Description + A variant of Foam::string with expansion of dictionary variables + into a comma-separated form. + +SourceFiles + exprString.C + exprStringI.H + +SeeAlso + Foam::exprTools::expressionEntry + +\*---------------------------------------------------------------------------*/ + +#ifndef expressions_exprString_H +#define expressions_exprString_H + +#include "string.H" +#include "dictionary.H" + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +namespace Foam +{ +namespace expressions +{ + +/*---------------------------------------------------------------------------*\ + Class exprString Declaration +\*---------------------------------------------------------------------------*/ + +class exprString +: + public string +{ +public: + + // Constructors + + //- Construct null + exprString() = default; + + //- Copy construct + exprString(const exprString& rhs) = default; + + //- Move construct + exprString(exprString&& rhs) = default; + + //- Copy construct from std::string + inline explicit exprString(const std::string& s, bool doValidate=true); + + //- Move construct from std::string + inline explicit exprString(std::string&& s, bool doValidate=true); + + //- Construct as copy of character array + inline explicit exprString(const char* s, bool doValidate=true); + + //- Copy construct and expand with dictionary variables + // Optionally strip C/C++ comments from the input + inline exprString + ( + const std::string& str, + const dictionary& dict, + const bool removeComments = false + ); + + //- Move construct and expand with dictionary variables + // Optionally strip C/C++ comments from the input + inline exprString + ( + std::string&& str, + const dictionary& dict, + const bool removeComments = false + ); + + //- Construct from Istream and expand with dictionary variables + // Optionally strip C/C++ comments from the input + inline exprString + ( + Istream& is, + const dictionary& dict, + const bool removeComments = false + ); + + + //- Destructor + ~exprString() = default; + + + // Member Functions + + //- Copy convert string to exprString. + // No expansions, know what you are doing. + inline static exprString toExpr(const std::string& str); + + //- Move convert string to exprString. + // No expansions, know what you are doing. + inline static exprString toExpr(std::string&& str); + + //- Inplace expansion with dictionary variables + // Optionally strip C/C++ comments from the input + exprString& expand + ( + const dictionary& dict, + const bool removeComments = false + ); + + //- Check for unexpanded '$' entries. Fatal if any exist. + inline bool valid() const; + + + // Member Operators + + //- Copy assign + exprString& operator=(const exprString& str) = default; + + //- Move assign + exprString& operator=(exprString&& str) = default; + + //- Copy assign from c-string (no expansions) + inline exprString& operator=(const char* str); + + //- Copy assign from string (no expansions) + inline exprString& operator=(const std::string& str); + + //- Move assign from string (no expansions) + inline exprString& operator=(std::string&& str); +}; + + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +} // End namespace expressions +} // End namespace Foam + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +#include "exprStringI.H" + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +#endif + +// ************************************************************************* // diff --git a/src/OpenFOAM/expressions/exprString/exprStringI.H b/src/OpenFOAM/expressions/exprString/exprStringI.H new file mode 100644 index 0000000000..3c64e31b4b --- /dev/null +++ b/src/OpenFOAM/expressions/exprString/exprStringI.H @@ -0,0 +1,205 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | www.openfoam.com + \\/ M anipulation | +------------------------------------------------------------------------------- + Original code Copyright (C) 2012-2018 Bernhard Gschaider + Copyright (C) 2019 OpenCFD Ltd. +------------------------------------------------------------------------------- +License + This file is part of OpenFOAM. + + OpenFOAM is free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + OpenFOAM is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + along with OpenFOAM. If not, see . + +\*---------------------------------------------------------------------------*/ + +#include "error.H" + +// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // + +inline Foam::expressions::exprString::exprString +( + const std::string& s, + bool doValidate +) +: + string(s) +{ + #ifdef FULLDEBUG + if (doValidate) + { + (void)valid(); + } + #endif +} + + +inline Foam::expressions::exprString::exprString +( + std::string&& s, + bool doValidate +) +: + string(std::move(s)) +{ + #ifdef FULLDEBUG + if (doValidate) + { + (void)valid(); + } + #endif +} + + +inline Foam::expressions::exprString::exprString +( + const char* s, + bool doValidate +) +: + string(s) +{ + #ifdef FULLDEBUG + if (doValidate) + { + (void)valid(); + } + #endif +} + + +inline Foam::expressions::exprString::exprString +( + const std::string& str, + const dictionary& dict, + const bool removeComments +) +: + string(str) +{ + expand(dict, removeComments); +} + + +inline Foam::expressions::exprString::exprString +( + std::string&& str, + const dictionary& dict, + const bool removeComments +) +: + string(std::move(str)) +{ + expand(dict, removeComments); +} + + +inline Foam::expressions::exprString::exprString +( + Istream& is, + const dictionary& dict, + const bool removeComments +) +: + string(is) +{ + expand(dict, removeComments); +} + + +// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // + +inline bool Foam::expressions::exprString::valid() const +{ + const bool ok = (std::string::npos == find('$')); + + #ifdef FULLDEBUG + if (!ok) + { + FatalErrorInFunction + << "Unexpanded '$' in " << *this << nl + << exit(FatalError); + } + #endif + + return ok; +} + + +inline Foam::expressions::exprString +Foam::expressions::exprString::toExpr(const std::string& str) +{ + exprString expr; + + expr.string::operator=(str); + + return expr; +} + + +inline Foam::expressions::exprString +Foam::expressions::exprString::toExpr(std::string&& str) +{ + exprString expr; + + expr.string::operator=(std::move(str)); + + return expr; +} + + +// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * // + +inline Foam::expressions::exprString& +Foam::expressions::exprString::operator=(const char* str) +{ + string::operator=(str); + + #ifdef FULLDEBUG + (void)valid(); + #endif + + return *this; +} + + +inline Foam::expressions::exprString& +Foam::expressions::exprString::operator=(const std::string& str) +{ + string::operator=(str); + + #ifdef FULLDEBUG + (void)valid(); + #endif + + return *this; +} + + +inline Foam::expressions::exprString& +Foam::expressions::exprString::operator=(std::string&& str) +{ + string::operator=(std::move(str)); + + #ifdef FULLDEBUG + (void)valid(); + #endif + + return *this; +} + + +// ************************************************************************* // diff --git a/src/OpenFOAM/expressions/exprTools/exprTools.C b/src/OpenFOAM/expressions/exprTools/exprTools.C new file mode 100644 index 0000000000..02a5ff3a07 --- /dev/null +++ b/src/OpenFOAM/expressions/exprTools/exprTools.C @@ -0,0 +1,202 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | www.openfoam.com + \\/ M anipulation | +------------------------------------------------------------------------------- + Copyright (C) 2019 OpenCFD Ltd. +------------------------------------------------------------------------------- +License + This file is part of OpenFOAM. + + OpenFOAM is free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + OpenFOAM is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + along with OpenFOAM. If not, see . + +Note + Ideas based on swak4Foam driver code (2010-2018) + from Bernhard Gschaider + +\*---------------------------------------------------------------------------*/ + +#include "exprTools.H" +#include "stringOps.H" +#include "DynamicList.H" + +// * * * * * * * * * * * * * * * Local Functions * * * * * * * * * * * * * * // + +namespace Foam +{ + +//! \cond file-scope + +// Maximum depth for recursive variable names +static constexpr label maxRecursionDepth_ = 100; + + +static List expandExprStrings +( + const UList& inputs, + const dictionary& dict, + bool mandatory, + label recursionDepth +) +{ + ///Info<< "::expandExprStrings " << inputs << endl; + + DynamicList result; + + for (const string& input : inputs) + { + // Allow inline list of semicolon-separated variables + const auto varExpressions = stringOps::split(input, ';'); + + for (const auto& subMatch : varExpressions) + { + string varExpr(stringOps::trim(subMatch.str())); + + if (varExpr.empty()) + { + continue; + } + + ///Info<< "Checking " << varExpr << endl; + + // Expand #otherVariable as dictionary lookup + if (varExpr[0] == '#') + { + ///Info<< "Expand: " << varExpr << endl; + + List expansions + ( + exprTools::getList + ( + dict, + varExpr.substr(1), + mandatory, + recursionDepth + ) + ); + + ///Info<< "Expanded " << varExpr.c_string() << ": " + /// << expansions << nl; + + result.reserve(result.size() + expansions.size()); + for (expressions::exprString& str : expansions) + { + result.append(std::move(str)); + } + } + else + { + result.append(expressions::exprString(varExpr, dict)); + } + } + } + + result.shrink(); + return result; +} + +//! \endcond + +} // End namespace Foam + + +// * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * // + +Foam::List +Foam::exprTools::getList +( + const dictionary& dict, + const word& keyword, + bool mandatory, + label recursionDepth +) +{ + List result; + + // Catch empty keyword as a no-op (eg, when called recursively) + if (keyword.empty()) + { + return result; + } + + const entry* eptr = dict.findEntry(keyword, keyType::LITERAL_RECURSIVE); + + if (!eptr) + { + if (mandatory) + { + FatalIOErrorInFunction(dict) + << "Missing mandatory entry: " << keyword << nl << nl + << exit(FatalIOError); + } + + return result; + } + + if (++recursionDepth > maxRecursionDepth_) + { + FatalIOErrorInFunction(dict) + << "Exceeded recursion depth (" << maxRecursionDepth_ + << ") while reading list " << keyword << nl + << "Likely caused by circular referencing" << nl + << exit(FatalIOError); + } + + + ITstream& is = eptr->stream(); + token firstToken(is); + + List list; + + if + ( + firstToken.isLabel() + || + ( + firstToken.type() == token::PUNCTUATION + && firstToken.pToken() == token::BEGIN_LIST + ) + ) + { + // A list of strings + is.rewind(); + is >> list; + } + else if (firstToken.isString()) + { + // A single string + list.resize(1); + list[0] = firstToken.stringToken(); + } + else + { + FatalIOErrorInFunction(dict) + << " Entry '"<< keyword + << "' not a string or list of strings" << nl + << exit(FatalError); + + return result; + } + + // Check for excess tokens + dict.checkITstream(is, keyword); + + // Expand List to List + return expandExprStrings(list, dict, mandatory, recursionDepth); +} + + +// ************************************************************************* // diff --git a/src/OpenFOAM/expressions/exprTools/exprTools.H b/src/OpenFOAM/expressions/exprTools/exprTools.H new file mode 100644 index 0000000000..d720586633 --- /dev/null +++ b/src/OpenFOAM/expressions/exprTools/exprTools.H @@ -0,0 +1,192 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | www.openfoam.com + \\/ M anipulation | +------------------------------------------------------------------------------- + Copyright (C) 2019 OpenCFD Ltd. +------------------------------------------------------------------------------- +License + This file is part of OpenFOAM. + + OpenFOAM is free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + OpenFOAM is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + along with OpenFOAM. If not, see . + +Namespace + Foam::exprTools + +Description + Helper methods for creating expressions. + +SourceFiles + exprTools.C + exprToolsTemplates.C + +\*---------------------------------------------------------------------------*/ + +#ifndef exprTools_H +#define exprTools_H + +#include "exprString.H" +#include "scalar.H" + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +namespace Foam +{ + +/*---------------------------------------------------------------------------*\ + Namespace exprTools Declaration +\*---------------------------------------------------------------------------*/ + +namespace exprTools +{ + +//- Stringified value for zero of given type +template +string zeroValue(); + +//- Stringified version of data with comma separators between components +template +string toString(const Type& data, const word& prefix); + +//- Stringified version of data with comma separators between components. +//- Uses prefix corresponding to the pTraits of Type. +template +string toString(const Type& data); + +//- Stringified version of primitiveEntry of Type. +//- Uses comma separators and prefix corresponding to the pTraits of Type +template +string toString(ITstream& is); + +//- Get an expression string list from a dictionary +// Supports list and inline specifications, as well as indirect +// inclusion of other expression string lists. +// +// Syntax examples, +// \code +// +// // Single items. Inline or list format. +// list1a "var1=p"; +// list1b ( "var1=p" ); +// +// // Multiple items. Inline or list format. +// list2a "var2=T; var3=U; var4=rho"; +// list2b ( "var2=T" "var3=U; var4=rho" ); +// +// // Indirection. Inline or list format. +// list3a "#list1a; #list2b"; +// list2b ( "var1=p" "#list2a" ); +// +// \endcode +List getList +( + const dictionary& dict, + const word& keyword, + bool mandatory = true, + label recursionDepth = 0 +); + + +// Boolean-like values + +//- Convert [0-1] values (usually scalars) as false/true +template +struct toBoolOp +{ + int operator()(const T& val) const + { + return (0.5 < Foam::mag(val)); + } +}; + +//- No conversion needed for bool +template<> +struct toBoolOp +{ + int operator()(bool val) const + { + return val; + } +}; + +//- Inverse test of toBoolOp() +template +struct toBoolNotOp +{ + int operator()(const T& val) const + { + return (Foam::mag(val) < 0.5); + } +}; + +//- Inverse test of toBoolOp, no conversion needed for bool +template<> +struct toBoolNotOp +{ + int operator()(bool val) const + { + return !val; + } +}; + +//- Logical 'and' with scalars as (false, true) values +template +struct toBoolAndOp +{ + int operator()(const T& a, const T& b) const + { + return (toBoolOp()(a) && toBoolOp()(b)); + } +}; + +//- Logical 'or' with scalars as (false, true) values +template +struct toBoolOrOp +{ + int operator()(const T& a, const T& b) const + { + return (toBoolOp()(a) || toBoolOp()(b)); + } +}; + +//- Logical 'xor' with scalars as (false, true) values +template +struct toBoolXorOp +{ + int operator()(const T& a, const T& b) const + { + return (toBoolOp()(a) ? toBoolNotOp()(b) : toBoolOp()(b)); + } +}; + + +} // End namespace exprTools + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +} // End namespace Foam + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +#ifdef NoRepository + #include "exprToolsTemplates.C" +#endif + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +#endif + +// ************************************************************************* // diff --git a/src/OpenFOAM/expressions/exprTools/exprToolsTemplates.C b/src/OpenFOAM/expressions/exprTools/exprToolsTemplates.C new file mode 100644 index 0000000000..1cebc86034 --- /dev/null +++ b/src/OpenFOAM/expressions/exprTools/exprToolsTemplates.C @@ -0,0 +1,86 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | www.openfoam.com + \\/ M anipulation | +------------------------------------------------------------------------------- + Copyright (C) 2019 OpenCFD Ltd. +------------------------------------------------------------------------------- +License + This file is part of OpenFOAM. + + OpenFOAM is free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + OpenFOAM is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + along with OpenFOAM. If not, see . +\*---------------------------------------------------------------------------*/ + +#include "OStringStream.H" + +// * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * // + +template +Foam::string Foam::exprTools::zeroValue() +{ + OStringStream buf; + + buf << pTraits::typeName << '('; + for (direction cmpt=0; cmpt < pTraits::nComponents; ++cmpt) + { + if (cmpt) buf << ','; + buf << 0; + } + buf << ')'; + + return buf.str(); +} + + +template +Foam::string Foam::exprTools::toString +( + const Type& data, + const word& prefix +) +{ + OStringStream buf; + + buf << prefix << '('; + for (direction cmpt=0; cmpt < pTraits::nComponents; ++cmpt) + { + if (cmpt) buf << ','; + buf << component(data, cmpt); + } + buf << ')'; + + return buf.str(); +} + + +template +Foam::string Foam::exprTools::toString(const Type& data) +{ + return toString(data, pTraits::typeName); +} + + +template +Foam::string Foam::exprTools::toString(ITstream& is) +{ + Type data(Zero); + is >> data; + + return toString(data, pTraits::typeName); +} + + +// ************************************************************************* //