diff --git a/applications/test/dictionary2/Test-dictionary2.C b/applications/test/dictionary2/Test-dictionary2.C index d66e0d0590074d8430e5a953e2370e9f2cbd547a..b05c18c3c34fa4c62b23b533515db887d064053d 100644 --- a/applications/test/dictionary2/Test-dictionary2.C +++ b/applications/test/dictionary2/Test-dictionary2.C @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | Copyright (C) 2017-2018 OpenCFD Ltd. + \\ / A nd | Copyright (C) 2017-2019 OpenCFD Ltd. \\/ M anipulation | ------------------------------------------------------------------------------- License @@ -34,6 +34,8 @@ Description #include "IOobject.H" #include "IFstream.H" #include "dictionary.H" +#include "ops.H" +#include "scalarRange.H" #include "stringOps.H" using namespace Foam; @@ -108,6 +110,42 @@ scalar try_getScalar(const dictionary& dict, const word& k) } +// Try with getCheck<scalar> +template<class Predicate> +scalar try_getCheckScalar +( + const dictionary& dict, + const word& k, + const Predicate& pred +) +{ + scalar val(-GREAT); + + const bool throwingIOError = FatalIOError.throwExceptions(); + const bool throwingError = FatalError.throwExceptions(); + + try + { + val = dict.getCheck<scalar>(k, pred); + Info<< "getCheck<scalar>(" << k << ") = " << val << nl; + } + catch (const Foam::IOerror& err) + { + Info<< "getCheck<scalar>(" << k << ") Caught FatalIOError " + << err << nl << endl; + } + catch (const Foam::error& err) + { + Info<< "getCheck<scalar>(" << k << ") Caught FatalError " + << err << nl << endl; + } + FatalError.throwExceptions(throwingError); + FatalIOError.throwExceptions(throwingIOError); + + return val; +} + + // Try with *entry (from findEntry) and get<scalar> scalar try_getScalar(const entry* eptr, const word& k) { @@ -311,6 +349,7 @@ int main(int argc, char *argv[]) IStringStream ( "good 3.14159;\n" + "negative -3.14159;\n" "empty;\n" // "bad text;\n" // always fails // "bad 3.14159 1234;\n" // fails for readScalar @@ -338,6 +377,26 @@ int main(int argc, char *argv[]) try_getScalar(dict2, "empty"); } + + // With getCheck<scalar> + { + Info<< nl << "Test some input with getCheck<scalar>()" << nl; + + try_getCheckScalar(dict2, "good", scalarRange::gt0()); + try_getCheckScalar(dict2, "negative", scalarRange::gt0()); + + try_getCheckScalar(dict2, "good", greaterOp1<scalar>(0)); + try_getCheckScalar(dict2, "negative", greaterOp1<scalar>(0)); + + Info<< nl << "with lambda" << nl; + try_getCheckScalar + ( + dict2, + "good", + [](const scalar x) { return x > 0; } + ); + } + // With findEntry and get<scalar> { Info<< nl diff --git a/applications/test/regex1/Test-regex1.C b/applications/test/regex1/Test-regex1.C index ca5bfb60cd4d59cc3f2611dd2b4bf44da5255815..9bc38d64b2b6e484d28651c977ce574354cd1e7b 100644 --- a/applications/test/regex1/Test-regex1.C +++ b/applications/test/regex1/Test-regex1.C @@ -27,12 +27,16 @@ Description \*---------------------------------------------------------------------------*/ #include "argList.H" +#include "IOobject.H" #include "IOstreams.H" #include "IFstream.H" #include "Switch.H" +#include "SubStrings.H" #include "regExpCxx.H" +#ifndef _WIN32 #include "regExpPosix.H" +#endif using namespace Foam; @@ -83,6 +87,7 @@ static Ostream& operator<<(Ostream& os, const regExpCxx::results_type& sm) // Simple output of match groups +#ifndef _WIN32 static Ostream& operator<<(Ostream& os, const regExpPosix::results_type& sm) { for (std::smatch::size_type i = 1; i < sm.size(); ++i) @@ -92,6 +97,7 @@ static Ostream& operator<<(Ostream& os, const regExpPosix::results_type& sm) return os; } +#endif template<class RegexType> @@ -209,7 +215,6 @@ void generalTests() } - template<class RegexType> void testExpressions(const UList<regexTest>& tests) { @@ -293,11 +298,13 @@ int main(int argc, char *argv[]) "Test C++11 regular expressions" ); + #ifndef _WIN32 argList::addBoolOption ( "posix", "Test POSIX regular expressions" ); + #endif argList::addArgument("file"); argList::addArgument("..."); @@ -306,6 +313,17 @@ int main(int argc, char *argv[]) #include "setRootCase.H" + if (std::is_same<regExp, regExpCxx>::value) + { + Info<<"Foam::regExp uses C++11 regex" << nl << nl; + } + #ifndef _WIN32 + if (std::is_same<regExp, regExpPosix>::value) + { + Info<<"Foam::regExp uses POSIX regex" << nl << nl; + } + #endif + if (!args.count({"cxx", "posix"})) { Info<< "Specified one or more of -cxx, -posix" << nl; @@ -321,10 +339,12 @@ int main(int argc, char *argv[]) generalTests<regExpCxx>(); } + #ifndef _WIN32 if (args.found("posix")) { generalTests<regExpPosix>(); } + #endif } for (label argi = 1; argi < args.size(); ++argi) @@ -339,10 +359,12 @@ int main(int argc, char *argv[]) testExpressions<regExpCxx>(tests); } + #ifndef _WIN32 if (args.found("posix")) { testExpressions<regExpPosix>(tests); } + #endif } Info<< "\nDone" << nl << endl; diff --git a/applications/test/regex1/testRegexps2 b/applications/test/regex1/testRegexps2 new file mode 100644 index 0000000000000000000000000000000000000000..207c05b0484f1167e78f1a049adfad2247898c14 --- /dev/null +++ b/applications/test/regex1/testRegexps2 @@ -0,0 +1,19 @@ +/*--------------------------------*- C++ -*----------------------------------*\ +| ========= | | +| \\ / F ield | OpenFOAM: The Open Source CFD Toolbox | +| \\ / O peration | Version: v1812 | +| \\ / A nd | Web: www.OpenFOAM.com | +| \\/ M anipulation | | +\*---------------------------------------------------------------------------*/ + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +// Pattern, String +( + ( true "(U|k|epsilon)" "U" ) + ( false "(U|k|epsilon)" "alpha" ) + ( true "ab.*" "abc" ) + ( true ".*" "abc" ) +) + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // diff --git a/applications/test/scalarPredicates/Test-scalarPredicates.C b/applications/test/scalarPredicates/Test-scalarPredicates.C index 22338e91746fcb1dde1b22d304bd9297bc9d9c9f..587ca38b0d4f83aaefa871167ca3e828fb887881 100644 --- a/applications/test/scalarPredicates/Test-scalarPredicates.C +++ b/applications/test/scalarPredicates/Test-scalarPredicates.C @@ -36,6 +36,8 @@ Description #include "FlatOutput.H" #include "Tuple2.H" #include "StringStream.H" +#include "ops.H" +#include "bitSet.H" using namespace Foam; @@ -44,7 +46,7 @@ void doTest(const scalarList& values, const predicates::scalars& accept) { // Also tests that output is suppressed Info<<"Have: " << accept.size() << " predicates" << accept << endl; - Info<<"values: " << flatOutput(values) << endl; + Info<<"values: " << flatOutput(values) << endl; for (const scalar& value : values) { @@ -60,6 +62,30 @@ void doTest(const scalarList& values, const predicates::scalars& accept) } +template<class Predicate> +void testPredicate(const scalarList& values, const Predicate& pred) +{ + bitSet matches; + + label i=0; + + for (const scalar& value : values) + { + if (pred(value)) + { + matches.set(i); + } + + ++i; + } + + IndirectList<scalar> matched(values, matches.toc()); + + Info<< "matched: " << flatOutput(matched.addressing()) + << " = " << flatOutput(matched) << nl; +} + + // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // Main program: @@ -149,6 +175,16 @@ int main(int argc, char *argv[]) } + Info<< nl << "Test with ops" << nl; + Info<<"values: " << flatOutput(values) << endl; + { + testPredicate(values, lessOp1<scalar>(10)); + testPredicate(values, greaterOp1<scalar>(100)); + + // Also with dissimilar type + testPredicate(values, lessEqOp1<label>(0)); + } + Info<< "\nEnd\n" << endl; return 0; diff --git a/applications/utilities/preProcessing/foamUpgradeCyclics/foamUpgradeCyclics.C b/applications/utilities/preProcessing/foamUpgradeCyclics/foamUpgradeCyclics.C index 5c7ea648140e2fce89841b45cf893800300d077a..c948cbf9ab0a1eb531ddcdc784d76a21805d9db8 100644 --- a/applications/utilities/preProcessing/foamUpgradeCyclics/foamUpgradeCyclics.C +++ b/applications/utilities/preProcessing/foamUpgradeCyclics/foamUpgradeCyclics.C @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | + \\ / A nd | Copyright (C) 2019 OpenCFD Ltd. \\/ M anipulation | ------------------------------------------------------------------------------- | Copyright (C) 2011-2016 OpenFOAM Foundation @@ -313,7 +313,7 @@ void rewriteField if ( boundaryField.found(patchName) - && !boundaryField.found(newName, false, false) + && !boundaryField.found(newName, keyType::LITERAL) ) { Info<< " Changing entry " << patchName << " to " << newName diff --git a/etc/controlDict b/etc/controlDict index 5e03e45e20e89419bea165788a61e10bd57f2ad7..223f28e3abd134e726ca73efdab2b8223782c6b3 100644 --- a/etc/controlDict +++ b/etc/controlDict @@ -37,6 +37,8 @@ InfoSwitches writePrecision 6; writeDictionaries 0; + + // Report optional dictionary entries. For value > 1, treat as fatal. writeOptionalEntries 0; // Write lagrangian "positions" file in v1706 format (and earlier) diff --git a/src/OSspecific/POSIX/regExp/regExp.H b/src/OSspecific/POSIX/regExp/regExp.H index eee48c78d8c2fd7e9aa8a8df2ddfce4e783d12c2..fa85bea12e14354999f214b4045013e4d77340a3 100644 --- a/src/OSspecific/POSIX/regExp/regExp.H +++ b/src/OSspecific/POSIX/regExp/regExp.H @@ -32,6 +32,7 @@ Description #ifndef regExp_H #define regExp_H +#include "regExpCxx.H" #include "regExpPosix.H" #include "regExpFwd.H" diff --git a/src/OSspecific/POSIX/regExp/regExpFwd.H b/src/OSspecific/POSIX/regExp/regExpFwd.H index 47e8c268cf0bf7e53e23003ca320a659280747a4..09838222809090471328619885d61589307ad795 100644 --- a/src/OSspecific/POSIX/regExp/regExpFwd.H +++ b/src/OSspecific/POSIX/regExp/regExpFwd.H @@ -39,7 +39,12 @@ namespace Foam class regExpCxx; class regExpPosix; + // Newer compilers support regex directly + #if (_GLIBCXX_RELEASE >= 7) || (__clang_major__ >= 7) + typedef regExpCxx regExp; + #else typedef regExpPosix regExp; + #endif } // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // diff --git a/src/OSspecific/POSIX/regExp/regExpPosix.H b/src/OSspecific/POSIX/regExp/regExpPosix.H index dbed07a4dc619fa532f1164f30c5ed308e2e2c79..86c6147538dfd427c47e2c799b9ca1b23d86cf8d 100644 --- a/src/OSspecific/POSIX/regExp/regExpPosix.H +++ b/src/OSspecific/POSIX/regExp/regExpPosix.H @@ -120,7 +120,7 @@ public: inline ~regExpPosix(); - // Member functions + // Member Functions // Access diff --git a/src/OpenFOAM/containers/LinkedLists/linkTypes/DLListBase/DLListBase.H b/src/OpenFOAM/containers/LinkedLists/linkTypes/DLListBase/DLListBase.H index 24c0b5bd8b6e7abb503313ea5bfffec878f86c28..e6bb07cd9f897032dc1cc7df7eb1baa6abb11453 100644 --- a/src/OpenFOAM/containers/LinkedLists/linkTypes/DLListBase/DLListBase.H +++ b/src/OpenFOAM/containers/LinkedLists/linkTypes/DLListBase/DLListBase.H @@ -243,8 +243,7 @@ public: //- Deprecated(2019-01) Pointing at a valid storage node // \deprecated(2019-01) - use good() method - inline bool found() const - FOAM_DEPRECATED_FOR(2019-01, "good() method") + bool FOAM_DEPRECATED_FOR(2019-01, "good() method") found() const { return this->good(); } @@ -297,8 +296,7 @@ public: //- Deprecated(2019-01) Pointing at a valid storage node // \deprecated(2019-01) - use good() method - inline bool found() const - FOAM_DEPRECATED_FOR(2019-01, "good() method") + bool FOAM_DEPRECATED_FOR(2019-01, "good() method") found() const { return this->good(); } diff --git a/src/OpenFOAM/containers/LinkedLists/linkTypes/SLListBase/SLListBase.H b/src/OpenFOAM/containers/LinkedLists/linkTypes/SLListBase/SLListBase.H index 066cce1b86a3cea36093f70baab31c3990a79acb..c48913e39916e9e20ac1097f06c7495cf28d8d3f 100644 --- a/src/OpenFOAM/containers/LinkedLists/linkTypes/SLListBase/SLListBase.H +++ b/src/OpenFOAM/containers/LinkedLists/linkTypes/SLListBase/SLListBase.H @@ -219,8 +219,7 @@ public: //- Deprecated(2019-01) Pointing at a valid storage node // \deprecated(2019-01) - use good() method - inline bool found() const - FOAM_DEPRECATED_FOR(2019-01, "good() method") + bool FOAM_DEPRECATED_FOR(2019-01, "good() method") found() const { return this->good(); } @@ -271,8 +270,7 @@ public: //- Deprecated(2019-01) Pointing at a valid storage node // \deprecated(2019-01) - use good() method - inline bool found() const - FOAM_DEPRECATED_FOR(2019-01, "good() method") + bool FOAM_DEPRECATED_FOR(2019-01, "good() method") found() const { return this->good(); } diff --git a/src/OpenFOAM/db/dictionary/dictionary.C b/src/OpenFOAM/db/dictionary/dictionary.C index 775d90ea5974dfa24ac1a26253ead15859cf3305..a15a73c8559f8734a6b6c289969acf96aa2122f3 100644 --- a/src/OpenFOAM/db/dictionary/dictionary.C +++ b/src/OpenFOAM/db/dictionary/dictionary.C @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | Copyright (C) 2015-2018 OpenCFD Ltd. + \\ / A nd | Copyright (C) 2015-2019 OpenCFD Ltd. \\/ M anipulation | ------------------------------------------------------------------------------- | Copyright (C) 2011-2017 OpenFOAM Foundation @@ -42,13 +42,12 @@ namespace Foam const Foam::dictionary Foam::dictionary::null; -bool Foam::dictionary::writeOptionalEntries +int Foam::dictionary::writeOptionalEntries ( Foam::debug::infoSwitch("writeOptionalEntries", 0) ); - // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // Foam::dictionary::dictionary() @@ -311,13 +310,30 @@ void Foam::dictionary::checkITstream } +void Foam::dictionary::raiseBadInput(const word& keyword) const +{ + // Can use FatalIOError instead of SafeFatalIOError + // since predicate checks are not used at the earliest stages + FatalIOError + ( + "", // functionName + "", // sourceFileName + 0, // sourceFileLineNumber + *this // ios + ) + << "Entry '" << keyword << "' with invalid input in dictionary " + << name() << nl << nl + << exit(FatalIOError); +} + + bool Foam::dictionary::found ( const word& keyword, enum keyType::option matchOpt ) const { - return csearch(keyword, matchOpt).found(); + return csearch(keyword, matchOpt).good(); } @@ -359,11 +375,11 @@ const Foam::entry& Foam::dictionary::lookupEntry { const const_searcher finder(csearch(keyword, matchOpt)); - if (!finder.found()) + if (!finder.good()) { FatalIOErrorInFunction(*this) << "Entry '" << keyword << "' not found in dictionary " - << name() + << name() << nl << exit(FatalIOError); } @@ -395,7 +411,7 @@ bool Foam::dictionary::substituteKeyword(const word& keyword, bool mergeEntry) const const_searcher finder(csearch(varName, keyType::REGEX_RECURSIVE)); // If defined insert its entries into this dictionary - if (finder.found()) + if (finder.good()) { for (const entry& e : finder.dict()) { @@ -427,7 +443,7 @@ bool Foam::dictionary::substituteScopedKeyword const auto finder(csearchScoped(varName, keyType::REGEX_RECURSIVE)); // If defined insert its entries into this dictionary - if (finder.found()) + if (finder.good()) { for (const entry& e : finder.dict()) { @@ -473,11 +489,11 @@ const Foam::dictionary& Foam::dictionary::subDict(const word& keyword) const // Allow patterns, non-recursive const const_searcher finder(csearch(keyword, keyType::REGEX)); - if (!finder.found()) + if (!finder.good()) { FatalIOErrorInFunction(*this) << "Entry '" << keyword << "' not found in dictionary " - << name() + << name() << nl << exit(FatalIOError); } @@ -490,11 +506,11 @@ Foam::dictionary& Foam::dictionary::subDict(const word& keyword) // Allow patterns, non-recursive searcher finder(search(keyword, keyType::REGEX)); - if (!finder.found()) + if (!finder.good()) { FatalIOErrorInFunction(*this) << "Entry '" << keyword << "' not found in dictionary " - << name() + << name() << nl << exit(FatalIOError); } @@ -526,7 +542,7 @@ Foam::dictionary Foam::dictionary::subOrEmptyDict << exit(FatalIOError); } - if (finder.found()) + if (finder.good()) { IOWarningInFunction(*this) << "Entry '" << keyword @@ -552,7 +568,7 @@ const Foam::dictionary& Foam::dictionary::optionalSubDict return finder.dict(); } - if (finder.found()) + if (finder.good()) { IOWarningInFunction(*this) << "Entry '" << keyword @@ -611,7 +627,7 @@ Foam::entry* Foam::dictionary::add(entry* entryPtr, bool mergeEntry) auto iter = hashedEntries_.find(entryPtr->keyword()); - if (mergeEntry && iter.found()) + if (mergeEntry && iter.good()) { // Merge dictionary with dictionary if (iter()->isDict() && entryPtr->isDict()) @@ -786,7 +802,7 @@ bool Foam::dictionary::merge(const dictionary& dict) { auto fnd = hashedEntries_.find(e.keyword()); - if (fnd.found()) + if (fnd.good()) { // Recursively merge sub-dictionaries // TODO: merge without copying diff --git a/src/OpenFOAM/db/dictionary/dictionary.H b/src/OpenFOAM/db/dictionary/dictionary.H index 33dc695cd55c589cef6f2a103da688883b0276f4..7200a507edbd536fb5b10bbeae99f3b401225d32 100644 --- a/src/OpenFOAM/db/dictionary/dictionary.H +++ b/src/OpenFOAM/db/dictionary/dictionary.H @@ -62,8 +62,8 @@ Note key2 $..key1; // use key1 value from parent subdict2 { - key2 val2; - key3 $...key1; // use key1 value from grandparent + key2 val2; + key3 $...key1; // use key1 value from grandparent } } @@ -285,8 +285,9 @@ private: // Private Data //- Report optional keywords and values if not present in dictionary + // For value greater than 1: fatal. // Set/unset via an InfoSwitch - static bool writeOptionalEntries; + static int writeOptionalEntries; //- The dictionary name fileName name_; @@ -359,6 +360,10 @@ private: ) const; + //- Emit IOError about bad input for the entry + void raiseBadInput(const word& keyword) const; + + public: // Declare name of the class and its debug switch @@ -532,7 +537,6 @@ public: enum keyType::option matchOpt = keyType::REGEX ); - //- Search for an entry (const access) with the given keyword. //- Find and return a sub-dictionary pointer if present // (and a sub-dictionary) otherwise return nullptr. // @@ -543,6 +547,7 @@ public: enum keyType::option matchOpt = keyType::REGEX ) const; + //- Search for an entry (const access) with the given keyword. // // \param matchOpt the default search is non-recursive with patterns // @@ -553,22 +558,22 @@ public: enum keyType::option matchOpt ) const; - //- Find and return a T. + //- Find and return an entry data stream. //- FatalIOError if not found, or if the number of tokens is incorrect. // // \param matchOpt the default search is non-recursive with patterns - template<class T> - T get + ITstream& lookup ( const word& keyword, enum keyType::option matchOpt = keyType::REGEX ) const; - //- Find and return an entry data stream. + //- Find and return a T. //- FatalIOError if not found, or if the number of tokens is incorrect. // // \param matchOpt the default search is non-recursive with patterns - ITstream& lookup + template<class T> + T get ( const word& keyword, enum keyType::option matchOpt = keyType::REGEX @@ -579,7 +584,7 @@ public: // // \param matchOpt the default search is non-recursive with patterns template<class T> - T lookupOrDefault + T getOrDefault ( const word& keyword, const T& deflt, @@ -592,7 +597,7 @@ public: // // \param matchOpt the default search is non-recursive with patterns template<class T> - T lookupOrAddDefault + T getOrAdd ( const word& keyword, const T& deflt, @@ -633,6 +638,86 @@ public: enum keyType::option matchOpt = keyType::REGEX ) const; + //- Find and return a T with additional checking + //- FatalIOError if not found, or if the number of tokens is incorrect. + // + // \param pred the value check predicate + // \param matchOpt the default search is non-recursive with patterns + template<class T, class Predicate> + T getCheck + ( + const word& keyword, + const Predicate& pred, + enum keyType::option matchOpt = keyType::REGEX + ) const; + + //- Find and return a T, or return the given default value. + //- FatalIOError if it is found and the number of tokens is incorrect. + // + // \param pred the value check predicate + // \param matchOpt the default search is non-recursive with patterns + template<class T, class Predicate> + T getCheckOrDefault + ( + const word& keyword, + const T& deflt, + const Predicate& pred, + enum keyType::option matchOpt = keyType::REGEX + ) const; + + //- Find and return a T, or return the given default value + //- and add it to dictionary. + //- FatalIOError if it is found and the number of tokens is incorrect. + // + // \param pred the value check predicate + // \param matchOpt the default search is non-recursive with patterns + template<class T, class Predicate> + T getCheckOrAdd + ( + const word& keyword, + const T& deflt, + const Predicate& pred, + enum keyType::option matchOpt = keyType::REGEX + ); + + //- Find entry and assign to T val. + //- FatalIOError if it is found and the number of tokens is incorrect, + //- or it is mandatory and not found. + // + // \param val the value to read into + // \param pred the value check predicate + // \param matchOpt the default search is non-recursive with patterns + // \param mandatory the keyword is mandatory + // + // \return true if the entry was found. + template<class T, class Predicate> + bool readCheck + ( + const word& keyword, + T& val, + const Predicate& pred, + enum keyType::option matchOpt = keyType::REGEX, + bool mandatory = true + ) const; + + //- Find an entry if present, and assign to T val. + //- FatalIOError if it is found and the number of tokens is incorrect. + // Default search: non-recursive with patterns. + // + // \param val the value to read into + // \param pred the value check predicate + // \param matchOpt the default search is non-recursive with patterns + // + // \return true if the entry was found. + template<class T, class Predicate> + bool readCheckIfPresent + ( + const word& keyword, + T& val, + const Predicate& pred, + enum keyType::option matchOpt = keyType::REGEX + ) const; + //- Check if entry is found and and is a sub-dictionary. // // Search type: non-recursive with patterns. @@ -983,32 +1068,32 @@ public: enum keyType::option ) const; - //- Find and return a T + //- Find and return an entry data stream, //- using any compatibility names if needed. - //- FatalIOError if not found, or if there are excess tokens. // Default search: non-recursive with patterns. // // \param compat list of old compatibility keywords and the last // OpenFOAM version for which they were used. // \param recursive search parent dictionaries // \param patternMatch use regular expressions - template<class T> - T getCompat + ITstream& lookupCompat ( const word& keyword, std::initializer_list<std::pair<const char*,int>> compat, enum keyType::option = keyType::REGEX ) const; - //- Find and return an entry data stream, + //- Find and return a T //- using any compatibility names if needed. + //- FatalIOError if not found, or if there are excess tokens. // Default search: non-recursive with patterns. // // \param compat list of old compatibility keywords and the last // OpenFOAM version for which they were used. // \param recursive search parent dictionaries // \param patternMatch use regular expressions - ITstream& lookupCompat + template<class T> + T getCompat ( const word& keyword, std::initializer_list<std::pair<const char*,int>> compat, @@ -1024,7 +1109,7 @@ public: // \param recursive search parent dictionaries // \param patternMatch use regular expressions template<class T> - T lookupOrDefaultCompat + T getOrDefaultCompat ( const word& keyword, std::initializer_list<std::pair<const char*,int>> compat, @@ -1105,10 +1190,63 @@ public: // Housekeeping + //- Find and return a T, or return the given default value. + //- FatalIOError if it is found and the number of tokens is incorrect. + // + // \param matchOpt the default search is non-recursive with patterns + template<class T> + T lookupOrDefault + ( + const word& keyword, + const T& deflt, + enum keyType::option matchOpt = keyType::REGEX + ) const + { + return getOrDefault<T>(keyword, deflt, matchOpt); + } + + + //- Find and return a T, or return the given default value + //- and add it to dictionary. + //- FatalIOError if it is found and the number of tokens is incorrect. + // + // \param matchOpt the default search is non-recursive with patterns + template<class T> + T lookupOrAddDefault + ( + const word& keyword, + const T& deflt, + enum keyType::option matchOpt = keyType::REGEX + ) + { + return getOrAdd<T>(keyword, deflt, matchOpt); + } + + //- Find and return a T, or return the given default value + //- using any compatibility names if needed. + // Default search: non-recursive with patterns. + // + // \param compat list of old compatibility keywords and the last + // OpenFOAM version for which they were used. + // \param recursive search parent dictionaries + // \param patternMatch use regular expressions + template<class T> + T lookupOrDefaultCompat + ( + const word& keyword, + std::initializer_list<std::pair<const char*,int>> compat, + const T& deflt, + enum keyType::option matchOpt = keyType::REGEX + ) const + { + return getOrDefaultCompat<T>(keyword, compat, deflt, matchOpt); + } + //- Deprecated(2018-07) find and return an entry data stream // // \deprecated(2018-07) - use lookup() method - ITstream& operator[](const word& keyword) const + ITstream& FOAM_DEPRECATED_FOR(2018-07, "lookup() method") + operator[](const word& keyword) const { return lookup(keyword); } @@ -1116,7 +1254,8 @@ public: //- Deprecated(2018-10) find and return a T. // \deprecated(2018-10) - use get() method template<class T> - T lookupType + T FOAM_DEPRECATED_FOR(2018-10, "get() method") + lookupType ( const word& keyword, bool recursive = false, @@ -1128,7 +1267,8 @@ public: //- Deprecated(2018-10) // \deprecated(2018-10) - use keyType::option version - bool found + bool FOAM_DEPRECATED_FOR(2018-10, "found(keyType::option)") + found ( const word& keyword, bool recursive, @@ -1140,7 +1280,9 @@ public: //- Deprecated(2018-10) // \deprecated(2018-10) - use keyType::option version - entry* lookupEntryPtr + entry* + FOAM_DEPRECATED_FOR(2018-10, "lookupEntryPtr(keyType::option)") + lookupEntryPtr ( const word& keyword, bool recursive, @@ -1152,7 +1294,9 @@ public: //- Deprecated(2018-10) // \deprecated(2018-10) - use keyType::option version - const entry* lookupEntryPtr + const entry* + FOAM_DEPRECATED_FOR(2018-10, "lookupEntryPtr(keyType::option)") + lookupEntryPtr ( const word& keyword, bool recursive, @@ -1164,7 +1308,9 @@ public: //- Deprecated(2018-10) // \deprecated(2018-10) - use keyType::option version - const entry* lookupScopedEntryPtr + const entry* + FOAM_DEPRECATED_FOR(2018-10, "lookupScopedEntryPtr(keyType::option)") + lookupScopedEntryPtr ( const word& keyword, bool recursive, @@ -1180,7 +1326,9 @@ public: // // Search type: non-recursive with patterns. // \deprecated(2018-10) - use findDict() method - const dictionary* subDictPtr(const word& keyword) const + const dictionary* + FOAM_DEPRECATED_FOR(2018-10, "findDict() method") + subDictPtr(const word& keyword) const { return findDict(keyword, keyType::REGEX); } @@ -1191,14 +1339,18 @@ public: // // Search type: non-recursive with patterns. // \deprecated(2018-10) - use findDict() method - dictionary* subDictPtr(const word& keyword) + dictionary* + FOAM_DEPRECATED_FOR(2018-10, "findDict() method") + subDictPtr(const word& keyword) { return findDict(keyword, keyType::REGEX); } //- Deprecated(2018-10) // \deprecated(2018-10) - use keyType::option version - const entry& lookupEntry + const entry& + FOAM_DEPRECATED_FOR(2018-10, "lookupEntry(keyType::option)") + lookupEntry ( const word& keyword, bool recursive, @@ -1210,7 +1362,9 @@ public: //- Deprecated(2018-10) // \deprecated(2018-10) - use keyType::option version - ITstream& lookup + ITstream& + FOAM_DEPRECATED_FOR(2018-10, "lookup(keyType::option)") + lookup ( const word& keyword, bool recursive, @@ -1223,7 +1377,8 @@ public: //- Deprecated(2018-10) // \deprecated(2018-10) - use keyType::option version template<class T> - T lookupOrDefault + T FOAM_DEPRECATED_FOR(2018-10, "lookupOrDefault(keyType::option)") + lookupOrDefault ( const word& keyword, const T& deflt, @@ -1239,7 +1394,8 @@ public: //- Deprecated(2018-10) // \deprecated(2018-10) - use keyType::option version template<class T> - T lookupOrAddDefault + T FOAM_DEPRECATED_FOR(2018-10, "lookupOrAddDefault(keyType::option)") + lookupOrAddDefault ( const word& keyword, const T& deflt, @@ -1255,7 +1411,9 @@ public: //- Deprecated(2018-10) // \deprecated(2018-10) - use keyType::option version template<class T> - bool readIfPresent + bool + FOAM_DEPRECATED_FOR(2018-10, "readIfPresent(keyType::option)") + readIfPresent ( const word& keyword, T& val, diff --git a/src/OpenFOAM/db/dictionary/dictionaryCompat.C b/src/OpenFOAM/db/dictionary/dictionaryCompat.C index c35d7f6b2344ed2cf1ab76ed3395c00f08fdd1f0..ca4a9a7cbb8f2578377e50962a98df59f769c15a 100644 --- a/src/OpenFOAM/db/dictionary/dictionaryCompat.C +++ b/src/OpenFOAM/db/dictionary/dictionaryCompat.C @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | Copyright (C) 2017-2018 OpenCFD Ltd. + \\ / A nd | Copyright (C) 2017-2019 OpenCFD Ltd. \\/ M anipulation | ------------------------------------------------------------------------------- License @@ -54,7 +54,7 @@ Foam::dictionary::const_searcher Foam::dictionary::csearchCompat { const_searcher finder(csearch(keyword, matchOpt)); - if (finder.found()) + if (finder.good()) { return finder; } @@ -63,7 +63,7 @@ Foam::dictionary::const_searcher Foam::dictionary::csearchCompat { finder = csearch(word::validate(iter.first), matchOpt); - if (finder.found()) + if (finder.good()) { // Only want a single warning (on master), but guard with a // parRun check to avoid Pstream::master() when Pstream has not @@ -101,7 +101,7 @@ bool Foam::dictionary::foundCompat enum keyType::option matchOpt ) const { - return csearchCompat(keyword, compat, matchOpt).found(); + return csearchCompat(keyword, compat, matchOpt).good(); } @@ -125,7 +125,7 @@ const Foam::entry& Foam::dictionary::lookupEntryCompat { const const_searcher finder(csearchCompat(keyword, compat, matchOpt)); - if (!finder.found()) + if (!finder.good()) { FatalIOErrorInFunction(*this) << "Entry '" << keyword << "' not found in dictionary " diff --git a/src/OpenFOAM/db/dictionary/dictionarySearch.C b/src/OpenFOAM/db/dictionary/dictionarySearch.C index d3fcee91c452fac581ab959a6759946ad0d0bb3a..59cebc461dd4fe684760f598124680b21b077b86 100644 --- a/src/OpenFOAM/db/dictionary/dictionarySearch.C +++ b/src/OpenFOAM/db/dictionary/dictionarySearch.C @@ -130,7 +130,7 @@ Foam::dictionary::const_searcher Foam::dictionary::csearchDotScoped // a.b.c.d it would try // a.b, a.b.c, a.b.c.d - if (!finder.found()) + if (!finder.good()) { while (!finder.isDict()) { @@ -225,7 +225,7 @@ Foam::dictionary::const_searcher Foam::dictionary::csearchSlashScoped auto finder = dictPtr->csearch(key, matchOpt); - if (finder.found()) + if (finder.good()) { if (remaining) { @@ -269,7 +269,7 @@ Foam::dictionary::const_searcher Foam::dictionary::csearch auto iter = hashedEntries_.cfind(keyword); - if (iter.found()) + if (iter.good()) { finder.set(iter.val()); return finder; @@ -425,7 +425,7 @@ const Foam::dictionary* Foam::dictionary::cfindScopedDict auto iter = dictPtr->hashedEntries_.cfind(cmpt); - if (iter.found()) + if (iter.good()) { const entry *eptr = iter.val(); @@ -532,7 +532,7 @@ Foam::dictionary* Foam::dictionary::makeScopedDict(const fileName& dictPath) auto iter = dictPtr->hashedEntries_.find(cmptName); - if (iter.found()) + if (iter.good()) { entry *eptr = iter.val(); @@ -581,7 +581,7 @@ bool Foam::dictionary::remove(const word& keyword) { auto iter = hashedEntries_.find(keyword); - if (iter.found()) + if (iter.good()) { // Delete from patterns auto wcLink = patterns_.begin(); @@ -621,7 +621,7 @@ bool Foam::dictionary::changeKeyword // Check that oldKeyword exists and can be changed auto iter = hashedEntries_.find(oldKeyword); - if (!iter.found()) + if (!iter.good()) { return false; } @@ -638,7 +638,7 @@ bool Foam::dictionary::changeKeyword auto iter2 = hashedEntries_.find(newKeyword); // newKeyword already exists - if (iter2.found()) + if (iter2.good()) { if (overwrite) { diff --git a/src/OpenFOAM/db/dictionary/dictionaryTemplates.C b/src/OpenFOAM/db/dictionary/dictionaryTemplates.C index 31737707232bfa40d8edf83d937104c40b84815c..1ae164b5ecabc2d280ff2db87d7fb3a3f8d25e2b 100644 --- a/src/OpenFOAM/db/dictionary/dictionaryTemplates.C +++ b/src/OpenFOAM/db/dictionary/dictionaryTemplates.C @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | Copyright (C) 2017-2018 OpenCFD Ltd. + \\ / A nd | Copyright (C) 2017-2019 OpenCFD Ltd. \\/ M anipulation | ------------------------------------------------------------------------------- | Copyright (C) 2011-2017 OpenFOAM Foundation @@ -64,6 +64,20 @@ T Foam::dictionary::get } +template<class T, class Predicate> +T Foam::dictionary::getCheck +( + const word& keyword, + const Predicate& pred, + enum keyType::option matchOpt +) const +{ + T val; + readCheck<T, Predicate>(keyword, val, pred, matchOpt); + return val; +} + + template<class T> T Foam::dictionary::getCompat ( @@ -79,49 +93,111 @@ T Foam::dictionary::getCompat template<class T> -bool Foam::dictionary::readCompat +T Foam::dictionary::getOrDefault ( const word& keyword, - std::initializer_list<std::pair<const char*,int>> compat, - T& val, - enum keyType::option matchOpt, - bool mandatory + const T& deflt, + enum keyType::option matchOpt ) const { - const const_searcher finder(csearchCompat(keyword, compat, matchOpt)); + const const_searcher finder(csearch(keyword, matchOpt)); - if (finder.found()) + if (finder.good()) { + T val; + ITstream& is = finder.ptr()->stream(); is >> val; checkITstream(is, keyword); - return true; + return val; } - else if (mandatory) + else if (writeOptionalEntries) { - FatalIOErrorInFunction(*this) - << "Entry '" << keyword << "' not found in dictionary " - << name() - << exit(FatalIOError); + if (writeOptionalEntries > 1) + { + FatalIOErrorInFunction(*this) + << "Optional entry '" << keyword + << "' not found. Default '" << deflt << "' ignored" << nl + << exit(FatalIOError); + } + else + { + IOInfoInFunction(*this) + << "Optional entry '" << keyword + << "' not found. Using default '" << deflt << "'" << nl; + } } - return false; + return deflt; } template<class T> -T Foam::dictionary::lookupOrDefault +T Foam::dictionary::getOrAdd +( + const word& keyword, + const T& deflt, + enum keyType::option matchOpt +) +{ + const const_searcher finder(csearch(keyword, matchOpt)); + + if (finder.good()) + { + T val; + + ITstream& is = finder.ptr()->stream(); + is >> val; + + checkITstream(is, keyword); + + return val; + } + else if (writeOptionalEntries) + { + if (writeOptionalEntries > 1) + { + FatalIOErrorInFunction(*this) + << "Optional entry '" << keyword + << "' not found. Default '" << deflt << "' ignored" << nl + << exit(FatalIOError); + } + else + { + IOInfoInFunction(*this) + << "Optional entry '" << keyword + << "' not found. Adding default '" << deflt << "'" << nl; + } + } + + add(new primitiveEntry(keyword, deflt)); + return deflt; +} + + +template<class T, class Predicate> +T Foam::dictionary::getCheckOrDefault ( const word& keyword, const T& deflt, + const Predicate& pred, enum keyType::option matchOpt ) const { + if (!pred(deflt)) + { + // Could be as FULLDEBUG instead? + FatalIOErrorInFunction(*this) + << "Entry '" << keyword << "' with invalid default in dictionary " + << name() + << exit(FatalIOError); + } + const const_searcher finder(csearch(keyword, matchOpt)); - if (finder.found()) + if (finder.good()) { T val; @@ -130,31 +206,55 @@ T Foam::dictionary::lookupOrDefault checkITstream(is, keyword); + if (!pred(val)) + { + raiseBadInput(keyword); + } + return val; } else if (writeOptionalEntries) { - IOInfoInFunction(*this) - << "Optional entry '" << keyword - << "' not found, using default value '" << deflt << "'" - << nl; + if (writeOptionalEntries > 1) + { + FatalIOErrorInFunction(*this) + << "Optional entry '" << keyword + << "' not found. Default '" << deflt << "' ignored" << nl + << exit(FatalIOError); + } + else + { + IOInfoInFunction(*this) + << "Optional entry '" << keyword + << "' not found. Using default '" << deflt << "'" << nl; + } } return deflt; } -template<class T> -T Foam::dictionary::lookupOrAddDefault +template<class T, class Predicate> +T Foam::dictionary::getCheckOrAdd ( const word& keyword, const T& deflt, + const Predicate& pred, enum keyType::option matchOpt ) { + if (!pred(deflt)) + { + // Could be as FULLDEBUG instead? + FatalIOErrorInFunction(*this) + << "Entry '" << keyword << "' with invalid default in dictionary " + << name() + << exit(FatalIOError); + } + const const_searcher finder(csearch(keyword, matchOpt)); - if (finder.found()) + if (finder.good()) { T val; @@ -163,14 +263,28 @@ T Foam::dictionary::lookupOrAddDefault checkITstream(is, keyword); + if (!pred(val)) + { + raiseBadInput(keyword); + } + return val; } else if (writeOptionalEntries) { - IOInfoInFunction(*this) - << "Optional entry '" << keyword - << "' not found, adding default value '" << deflt << "'" - << nl; + if (writeOptionalEntries > 1) + { + FatalIOErrorInFunction(*this) + << "Optional entry '" << keyword + << "' not found. Default '" << deflt << "' ignored" << nl + << exit(FatalIOError); + } + else + { + IOInfoInFunction(*this) + << "Optional entry '" << keyword + << "' not found. Adding default '" << deflt << "'" << nl; + } } add(new primitiveEntry(keyword, deflt)); @@ -189,7 +303,7 @@ bool Foam::dictionary::readEntry { const const_searcher finder(csearch(keyword, matchOpt)); - if (finder.found()) + if (finder.good()) { ITstream& is = finder.ptr()->stream(); is >> val; @@ -202,7 +316,78 @@ bool Foam::dictionary::readEntry { FatalIOErrorInFunction(*this) << "Entry '" << keyword << "' not found in dictionary " - << name() + << name() << nl + << exit(FatalIOError); + } + + return false; +} + + +template<class T, class Predicate> +bool Foam::dictionary::readCheck +( + const word& keyword, + T& val, + const Predicate& pred, + enum keyType::option matchOpt, + bool mandatory +) const +{ + const const_searcher finder(csearch(keyword, matchOpt)); + + if (finder.good()) + { + ITstream& is = finder.ptr()->stream(); + is >> val; + + checkITstream(is, keyword); + + if (!pred(val)) + { + raiseBadInput(keyword); + } + + return true; + } + else if (mandatory) + { + FatalIOErrorInFunction(*this) + << "Entry '" << keyword << "' not found in dictionary " + << name() << nl + << exit(FatalIOError); + } + + return false; +} + + +template<class T> +bool Foam::dictionary::readCompat +( + const word& keyword, + std::initializer_list<std::pair<const char*,int>> compat, + T& val, + enum keyType::option matchOpt, + bool mandatory +) const +{ + const const_searcher finder(csearchCompat(keyword, compat, matchOpt)); + + if (finder.good()) + { + ITstream& is = finder.ptr()->stream(); + is >> val; + + checkITstream(is, keyword); + + return true; + } + else if (mandatory) + { + FatalIOErrorInFunction(*this) + << "Entry '" << keyword << "' not found in dictionary " + << name() << nl << exit(FatalIOError); } @@ -223,8 +408,22 @@ bool Foam::dictionary::readIfPresent } +template<class T, class Predicate> +bool Foam::dictionary::readCheckIfPresent +( + const word& keyword, + T& val, + const Predicate& pred, + enum keyType::option matchOpt +) const +{ + // Read is non-mandatory + return readCheck<T, Predicate>(keyword, val, pred, matchOpt, false); +} + + template<class T> -T Foam::dictionary::lookupOrDefaultCompat +T Foam::dictionary::getOrDefaultCompat ( const word& keyword, std::initializer_list<std::pair<const char*,int>> compat, @@ -234,7 +433,7 @@ T Foam::dictionary::lookupOrDefaultCompat { const const_searcher finder(csearchCompat(keyword, compat, matchOpt)); - if (finder.found()) + if (finder.good()) { T val; @@ -247,10 +446,19 @@ T Foam::dictionary::lookupOrDefaultCompat } else if (writeOptionalEntries) { - IOInfoInFunction(*this) - << "Optional entry '" << keyword << "' not found," - << " using default value '" << deflt << "'" - << nl; + if (writeOptionalEntries > 1) + { + FatalIOErrorInFunction(*this) + << "Optional entry '" << keyword + << "' not found. Default '" << deflt << "' ignored" << nl + << exit(FatalIOError); + } + else + { + IOInfoInFunction(*this) + << "Optional entry '" << keyword + << "' not found. Using default '" << deflt << "'" << nl; + } } return deflt; diff --git a/src/OpenFOAM/db/dictionary/entry/entry.C b/src/OpenFOAM/db/dictionary/entry/entry.C index 9bd55a3e050587d76bf1417993e63ea0faa1546a..7583c7bc81f873ab7326a494750e5fec90a3f2ad 100644 --- a/src/OpenFOAM/db/dictionary/entry/entry.C +++ b/src/OpenFOAM/db/dictionary/entry/entry.C @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | + \\ / A nd | Copyright (C) 2017-2019 OpenCFD Ltd. \\/ M anipulation | ------------------------------------------------------------------------------- | Copyright (C) 2011-2015 OpenFOAM Foundation @@ -73,6 +73,25 @@ Foam::autoPtr<Foam::entry> Foam::entry::clone() const // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // +void Foam::entry::raiseBadInput(const ITstream& is) const +{ + const word& keyword = keyword_; + + // Can use FatalIOError instead of SafeFatalIOError + // since predicate checks are not used at the earliest stages + FatalIOError + ( + "", // functionName + "", // sourceFileName + 0, // sourceFileLineNumber + this->name(), // ioFileName + is.lineNumber() // ioStartLineNumber + ) + << "Entry '" << keyword << "' with invalid input" << nl << nl + << exit(FatalIOError); +} + + void Foam::entry::checkITstream(const ITstream& is) const { const word& keyword = keyword_; diff --git a/src/OpenFOAM/db/dictionary/entry/entry.H b/src/OpenFOAM/db/dictionary/entry/entry.H index 1fe2e6b2d840b301392bf0b78f7e66a898a00613..58180f5955bdc7224357ea616cb438a785db1d6a 100644 --- a/src/OpenFOAM/db/dictionary/entry/entry.H +++ b/src/OpenFOAM/db/dictionary/entry/entry.H @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | Copyright (C) 2017-2018 OpenCFD Ltd. + \\ / A nd | Copyright (C) 2017-2019 OpenCFD Ltd. \\/ M anipulation | ------------------------------------------------------------------------------- | Copyright (C) 2011-2016 OpenFOAM Foundation @@ -109,6 +109,9 @@ private: // \return True if it is a valid keyType. static bool getKeyword(keyType& keyword, Istream& is); + //- Emit IOError about bad input for the entry + void raiseBadInput(const ITstream& is) const; + public: @@ -272,6 +275,37 @@ public: checkITstream(is); } + //- Get a T from the stream, + //- FatalIOError if the number of tokens is incorrect. + // + // \param pred the value check predicate + template<class T, class Predicate> + T getCheck(const Predicate& pred) const + { + T val; + readCheck<T>(val, pred); + return val; + } + + //- Assign to T val, + //- FatalIOError if the number of tokens is incorrect. + // + // \param val the value to read into + // \param pred the value check predicate + template<class T, class Predicate> + void readCheck(T& val, const Predicate& pred) const + { + ITstream& is = this->stream(); + is >> val; + + checkITstream(is); + if (!pred(val)) + { + raiseBadInput(is); + } + } + + // Write //- Write diff --git a/src/OpenFOAM/db/dictionary/entry/entryIO.C b/src/OpenFOAM/db/dictionary/entry/entryIO.C index 60b62f60d01180dfd4a2e0cdafdea90aa0ab604c..fbd297882890921e52b22b12dcafbc91b8024cc8 100644 --- a/src/OpenFOAM/db/dictionary/entry/entryIO.C +++ b/src/OpenFOAM/db/dictionary/entry/entryIO.C @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | Copyright (C) 2018 OpenCFD Ltd. + \\ / A nd | Copyright (C) 2018-2019 OpenCFD Ltd. \\/ M anipulation | ------------------------------------------------------------------------------- | Copyright (C) 2011-2016 OpenFOAM Foundation @@ -246,7 +246,7 @@ bool Foam::entry::New const auto finder = parentDict.csearchScoped(varName, keyType::REGEX_RECURSIVE); - if (finder.found()) + if (finder.good()) { // Read as primitiveEntry const keyType newKeyword(finder.ptr()->stream()); @@ -311,7 +311,7 @@ bool Foam::entry::New // How to manage duplicate entries bool mergeEntry = false; - if (finder.found()) + if (finder.good()) { // Use keyword from the found entry (ie, eliminate scoping chars) const keyType key = finder.ref().keyword(); diff --git a/src/OpenFOAM/db/dictionary/functionEntries/removeEntry/removeEntry.C b/src/OpenFOAM/db/dictionary/functionEntries/removeEntry/removeEntry.C index 6ab5618fa7dd66614e0b33a99ee394a5e308461a..b067fb76c48bd9ef98bfe9d9de4ce8014d852fc7 100644 --- a/src/OpenFOAM/db/dictionary/functionEntries/removeEntry/removeEntry.C +++ b/src/OpenFOAM/db/dictionary/functionEntries/removeEntry/removeEntry.C @@ -65,7 +65,7 @@ bool Foam::functionEntries::removeEntry::execute // Remove scoped keyword, or keyword in the local scope auto finder(parentDict.searchScoped(key, keyType::LITERAL)); - if (finder.found()) + if (finder.good()) { finder.context().remove(finder.ptr()->keyword()); } diff --git a/src/OpenFOAM/dimensionedTypes/dimensionedType/dimensionedType.C b/src/OpenFOAM/dimensionedTypes/dimensionedType/dimensionedType.C index 20487de8c9566d048c78f0598befa2ae3bf7588c..cc0240be0ba1c2f3b2f8585b3ec60155afe5253e 100644 --- a/src/OpenFOAM/dimensionedTypes/dimensionedType/dimensionedType.C +++ b/src/OpenFOAM/dimensionedTypes/dimensionedType/dimensionedType.C @@ -274,38 +274,38 @@ Foam::dimensioned<Type>::dimensioned // * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * // template<class Type> -Foam::dimensioned<Type> Foam::dimensioned<Type>::lookupOrDefault +Foam::dimensioned<Type> Foam::dimensioned<Type>::getOrDefault ( const word& name, const dictionary& dict, const dimensionSet& dims, - const Type& defaultValue + const Type& deflt ) { // checkDims = true - return dimensioned<Type>(name, dims, defaultValue, dict); + return dimensioned<Type>(name, dims, deflt, dict); } template<class Type> -Foam::dimensioned<Type> Foam::dimensioned<Type>::lookupOrDefault +Foam::dimensioned<Type> Foam::dimensioned<Type>::getOrDefault ( const word& name, const dictionary& dict, - const Type& defaultValue + const Type& deflt ) { - return dimensioned<Type>(name, dimless, defaultValue, dict); + return dimensioned<Type>(name, dimless, deflt, dict); } template<class Type> -Foam::dimensioned<Type> Foam::dimensioned<Type>::lookupOrAddToDict +Foam::dimensioned<Type> Foam::dimensioned<Type>::getOrAddToDict ( const word& name, dictionary& dict, const dimensionSet& dims, - const Type& defaultValue + const Type& deflt ) { if (dict.found(name)) @@ -313,20 +313,20 @@ Foam::dimensioned<Type> Foam::dimensioned<Type>::lookupOrAddToDict return dimensioned<Type>(name, dims, dict); } - (void) dict.add(name, defaultValue); - return dimensioned<Type>(name, dims, defaultValue); + (void) dict.add(name, deflt); + return dimensioned<Type>(name, dims, deflt); } template<class Type> -Foam::dimensioned<Type> Foam::dimensioned<Type>::lookupOrAddToDict +Foam::dimensioned<Type> Foam::dimensioned<Type>::getOrAddToDict ( const word& name, dictionary& dict, - const Type& defaultValue + const Type& deflt ) { - return lookupOrAddToDict(name, dict, dimless, defaultValue); + return getOrAddToDict(name, dict, dimless, deflt); } diff --git a/src/OpenFOAM/dimensionedTypes/dimensionedType/dimensionedType.H b/src/OpenFOAM/dimensionedTypes/dimensionedType/dimensionedType.H index 982ab31c7df32cffcbe9b5d81119a79567844a3e..2e768cbda56c5fb7dabff8334324b0ef12aa815b 100644 --- a/src/OpenFOAM/dimensionedTypes/dimensionedType/dimensionedType.H +++ b/src/OpenFOAM/dimensionedTypes/dimensionedType/dimensionedType.H @@ -207,42 +207,42 @@ public: //- Construct dimensioned from dictionary, with default value. //- FatalIOError if there are excess tokens. - static dimensioned<Type> lookupOrDefault + static dimensioned<Type> getOrDefault ( const word& name, const dictionary& dict, const dimensionSet& dims = dimless, - const Type& defaultValue = Type(Zero) + const Type& deflt = Type(Zero) ); //- Construct dimensionless from dictionary, with default value. // FatalIOError if it is found and there are excess tokens. - static dimensioned<Type> lookupOrDefault + static dimensioned<Type> getOrDefault ( const word& name, const dictionary& dict, - const Type& defaultValue = Type(Zero) + const Type& deflt = Type(Zero) ); //- Construct dimensioned from dictionary, with default value. // If the value is not found, it is added into the dictionary. // FatalIOError if it is found and there are excess tokens. - static dimensioned<Type> lookupOrAddToDict + static dimensioned<Type> getOrAddToDict ( const word& name, dictionary& dict, const dimensionSet& dims = dimless, - const Type& defaultValue = Type(Zero) + const Type& deflt = Type(Zero) ); //- Construct dimensionless from dictionary, with default value. // If the value is not found, it is added into the dictionary. // FatalIOError if it is found and there are excess tokens. - static dimensioned<Type> lookupOrAddToDict + static dimensioned<Type> getOrAddToDict ( const word& name, dictionary& dict, - const Type& defaultValue = Type(Zero) + const Type& deflt = Type(Zero) ); @@ -361,6 +361,59 @@ public: // dictionary instead (additional checks on the input stream). dimensioned(const word& name, const dimensionSet& dims, Istream& is) FOAM_DEPRECATED(2018-11); + + + //- Construct dimensioned from dictionary, with default value. + //- FatalIOError if there are excess tokens. + static dimensioned<Type> lookupOrDefault + ( + const word& name, + const dictionary& dict, + const dimensionSet& dims = dimless, + const Type& deflt = Type(Zero) + ) + { + return getOrDefault(name, dict, dims, deflt); + } + + //- Construct dimensionless from dictionary, with default value. + // FatalIOError if it is found and there are excess tokens. + static dimensioned<Type> lookupOrDefault + ( + const word& name, + const dictionary& dict, + const Type& deflt = Type(Zero) + ) + { + return getOrDefault(name, dict, deflt); + } + + //- Construct dimensioned from dictionary, with default value. + // If the value is not found, it is added into the dictionary. + // FatalIOError if it is found and there are excess tokens. + static dimensioned<Type> lookupOrAddToDict + ( + const word& name, + dictionary& dict, + const dimensionSet& dims = dimless, + const Type& deflt = Type(Zero) + ) + { + return getOrAddToDict(name, dict, dims, deflt); + } + + //- Construct dimensionless from dictionary, with default value. + // If the value is not found, it is added into the dictionary. + // FatalIOError if it is found and there are excess tokens. + static dimensioned<Type> lookupOrAddToDict + ( + const word& name, + dictionary& dict, + const Type& deflt = Type(Zero) + ) + { + return getOrAddToDict(name, dict, deflt); + } }; diff --git a/src/OpenFOAM/global/argList/argList.C b/src/OpenFOAM/global/argList/argList.C index f9daf29dc8b9702c37222aaba57d986ab15a4b18..2ac39189fe8b9a943ef0cd540fa51f1818cae652 100644 --- a/src/OpenFOAM/global/argList/argList.C +++ b/src/OpenFOAM/global/argList/argList.C @@ -1168,7 +1168,7 @@ void Foam::argList::parse decompDict.readEntry("numberOfSubdomains", dictNProcs); - if (decompDict.lookupOrDefault("distributed", false)) + if (decompDict.getOrDefault("distributed", false)) { parRunControl_.distributed(true); decompDict.readEntry("roots", roots); diff --git a/src/OpenFOAM/global/argList/argList.H b/src/OpenFOAM/global/argList/argList.H index d505a534aef1bb1f2ada9f127edb6f383f6417e8..422818c5a28d4e4a7d519cdbc505777d91cdcdf4 100644 --- a/src/OpenFOAM/global/argList/argList.H +++ b/src/OpenFOAM/global/argList/argList.H @@ -124,7 +124,7 @@ namespace Foam class argList { - // Private data + // Private Data //- Track if command arguments are mandatory/optional static bool argsMandatory_; @@ -383,7 +383,7 @@ public: inline T opt(const word& optName) const; //- Get a value from the named option if present, or return default. - // Identical to lookupOrDefault(). + // Identical to getOrDefault(). template<class T> inline T opt(const word& optName, const T& deflt) const; @@ -405,7 +405,7 @@ public: //- Get a value from the named option if present, or return default. template<class T> - inline T lookupOrDefault + inline T getOrDefault ( const word& optName, const T& deflt @@ -590,6 +590,16 @@ public: inline const string& operator[](const word& optName) const; + // Housekeeping + + //- Get a value from the named option if present, or return default. + template<class T> + T lookupOrDefault(const word& optName, const T& deflt) const + { + return getOrDefault<T>(optName, deflt); + } + + // Older style access (including 1712 release) #ifdef Foam_argList_1712 @@ -598,7 +608,8 @@ public: // Index 1 is the first (non-option) argument. // \deprecated(2018-08) - use get() method template<class T> - inline T read(const label index) const + T FOAM_DEPRECATED_FOR(2018-08, "get() method") + read(const label index) const { return this->get<T>(index); } @@ -607,21 +618,24 @@ public: // Index 1 is the first (non-option) argument. // \deprecated(2018-01) - use get() method template<class T> - inline T argRead(const label index) const + T FOAM_DEPRECATED_FOR(2018-01, "get() method") + argRead(const label index) const { return this->get<T>(index); } //- Deprecated(2018-01) return true if the named option is found // \deprecated(2018-01) - use found() method - inline bool optionFound(const word& optName) const + bool FOAM_DEPRECATED_FOR(2018-01, "found() method") + optionFound(const word& optName) const { return found(optName); } //- Deprecated(2018-01) return an input stream from the named option // \deprecated(2018-01) - use lookup() method - inline ITstream optionLookup(const word& optName) const + ITstream FOAM_DEPRECATED_FOR(2018-01, "lookup() method") + optionLookup(const word& optName) const { return lookup(optName); } @@ -629,7 +643,8 @@ public: //- Deprecated(2018-01) read a value from the named option // \deprecated(2018-01) - use opt() method template<class T> - inline T optionRead(const word& optName) const + T FOAM_DEPRECATED_FOR(2018-01, "opt() method") + optionRead(const word& optName) const { return opt<T>(optName); } @@ -638,7 +653,8 @@ public: // Return true if the named option was found. // \deprecated(2018-01) - use readIfPresent() method template<class T> - inline bool optionReadIfPresent + bool FOAM_DEPRECATED_FOR(2018-01, "readIfPresent() method") + optionReadIfPresent ( const word& optName, T& val @@ -652,7 +668,8 @@ public: // use the supplied default and return false. // \deprecated(2018-01) - use readIfPresent() method template<class T> - inline bool optionReadIfPresent + bool FOAM_DEPRECATED_FOR(2018-01, "readIfPresent() method") + optionReadIfPresent ( const word& optName, T& val, @@ -664,21 +681,23 @@ public: //- Deprecated(2018-01) read a value from the named option if present. // Return supplied default otherwise. - // \deprecated(2018-01) - use lookupOrDefault() method + // \deprecated(2018-01) - use getOrDefault() method template<class T> - inline T optionLookupOrDefault + T FOAM_DEPRECATED_FOR(2018-01, "getOrDefault() method") + optionLookupOrDefault ( const word& optName, const T& deflt ) const { - return lookupOrDefault<T>(optName, deflt); + return getOrDefault<T>(optName, deflt); } //- Deprecated(2018-01) read a List of values from the named option // \deprecated(2018-01) - use getList() method template<class T> - inline List<T> optionReadList(const word& optName) const + List<T> FOAM_DEPRECATED_FOR(2018-01, "getList() method") + optionReadList(const word& optName) const { return this->getList<T>(optName); } diff --git a/src/OpenFOAM/global/argList/argListI.H b/src/OpenFOAM/global/argList/argListI.H index 4186d6e4af8e8b4652b0ba43b95acac01c5f9f32..54e4ac9d8e25c7062eb4a356248471d777d5e6b5 100644 --- a/src/OpenFOAM/global/argList/argListI.H +++ b/src/OpenFOAM/global/argList/argListI.H @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | Copyright (C) 2017-2018 OpenCFD Ltd. + \\ / A nd | Copyright (C) 2017-2019 OpenCFD Ltd. \\/ M anipulation | ------------------------------------------------------------------------------- | Copyright (C) 2011-2013 OpenFOAM Foundation @@ -316,7 +316,7 @@ inline bool Foam::argList::readIfPresent template<class T> -inline T Foam::argList::lookupOrDefault +inline T Foam::argList::getOrDefault ( const word& optName, const T& deflt diff --git a/src/OpenFOAM/meshes/meshShapes/face/face.H b/src/OpenFOAM/meshes/meshShapes/face/face.H index d2e78edf3e72ed6348401f128fd1f1009768d387..28817059d422b91ee04452ad94b55b655853a77f 100644 --- a/src/OpenFOAM/meshes/meshShapes/face/face.H +++ b/src/OpenFOAM/meshes/meshShapes/face/face.H @@ -202,8 +202,8 @@ public: //- Legacy name for areaNormal() // \deprecated(2018-06) Deprecated for new use - inline vector normal(const UList<point>& p) const - FOAM_DEPRECATED_FOR(2018-12, "areaNormal() or unitNormal()") + vector FOAM_DEPRECATED_FOR(2018-12, "areaNormal() or unitNormal()") + normal(const UList<point>& p) const { return areaNormal(p); // Legacy definition } diff --git a/src/OpenFOAM/meshes/meshShapes/triFace/triFace.H b/src/OpenFOAM/meshes/meshShapes/triFace/triFace.H index ea964983e3d5d40c464aa3829b3bb3c398637d4c..87622154331078c24d419428634ad125da3b0d36 100644 --- a/src/OpenFOAM/meshes/meshShapes/triFace/triFace.H +++ b/src/OpenFOAM/meshes/meshShapes/triFace/triFace.H @@ -133,8 +133,8 @@ public: //- Legacy name for areaNormal() // \deprecated(2018-06) Deprecated for new use - inline vector normal(const UList<point>& points) const - FOAM_DEPRECATED_FOR(2018-12, "areaNormal() or unitNormal()") + vector FOAM_DEPRECATED_FOR(2018-12, "areaNormal() or unitNormal()") + normal(const UList<point>& points) const { return areaNormal(points); // Legacy definition } diff --git a/src/OpenFOAM/meshes/primitiveShapes/triangle/triangle.H b/src/OpenFOAM/meshes/primitiveShapes/triangle/triangle.H index cf985f027868cdcb8eec1556ec34909efda269ac..58b90a93aac3a81f220c31a96eb5878a9d6e6f45 100644 --- a/src/OpenFOAM/meshes/primitiveShapes/triangle/triangle.H +++ b/src/OpenFOAM/meshes/primitiveShapes/triangle/triangle.H @@ -216,8 +216,8 @@ public: //- Legacy name for areaNormal(). // \deprecated(2018-06) Deprecated for new use - inline vector normal() const - FOAM_DEPRECATED_FOR(2018-12, "areaNormal() or unitNormal()") + vector FOAM_DEPRECATED_FOR(2018-12, "areaNormal() or unitNormal()") + normal() const { return areaNormal(); } diff --git a/src/OpenFOAM/primitives/bools/Switch/Switch.C b/src/OpenFOAM/primitives/bools/Switch/Switch.C index 56fc8fee687ef0cce338f50c9edc91b704896aa5..7d7e780fdf6d4b24d598770ad50106ee1fa72330 100644 --- a/src/OpenFOAM/primitives/bools/Switch/Switch.C +++ b/src/OpenFOAM/primitives/bools/Switch/Switch.C @@ -110,14 +110,14 @@ Foam::Switch::switchType Foam::Switch::parse } -Foam::Switch Foam::Switch::lookupOrAddToDict +Foam::Switch Foam::Switch::getOrAddToDict ( const word& name, dictionary& dict, - const Switch defaultValue + const Switch deflt ) { - return dict.lookupOrAddDefault<Switch>(name, defaultValue); + return dict.getOrAdd<Switch>(name, deflt); } @@ -146,10 +146,10 @@ Foam::Switch::Switch ( const word& key, const dictionary& dict, - const Switch defaultValue + const Switch deflt ) : - Switch(defaultValue) + Switch(deflt) { const entry* eptr = dict.findEntry(key, keyType::LITERAL); diff --git a/src/OpenFOAM/primitives/bools/Switch/Switch.H b/src/OpenFOAM/primitives/bools/Switch/Switch.H index 0ab2a3ae0d2bfb08bdd78eb0d2e1adffd09062a3..907238897549d50e7916fc86667101560b5b8467 100644 --- a/src/OpenFOAM/primitives/bools/Switch/Switch.H +++ b/src/OpenFOAM/primitives/bools/Switch/Switch.H @@ -169,7 +169,7 @@ public: ( const word& key, //!< Lookup key. Uses LITERAL (not REGEX) const dictionary& dict, //!< dictionary - const Switch defaultValue //!< fallback if not found + const Switch deflt //!< fallback if not found ); //- Construct from Istream @@ -180,11 +180,11 @@ public: //- Construct from dictionary, supplying default value so that if the //- value is not found, it is added into the dictionary. - static Switch lookupOrAddToDict + static Switch getOrAddToDict ( const word& name, //!< Lookup key. Uses REGEX! dictionary& dict, //!< dictionary - const Switch defaultValue = switchType::FALSE //!< default to add + const Switch deflt = switchType::FALSE //!< default to add ); @@ -235,6 +235,18 @@ public: // Housekeeping + //- Construct from dictionary, supplying default value so that if the + //- value is not found, it is added into the dictionary. + static Switch lookupOrAddToDict + ( + const word& name, //!< Lookup key. Uses REGEX! + dictionary& dict, //!< dictionary + const Switch deflt = switchType::FALSE //!< default to add + ) + { + return getOrAddToDict(name, dict, deflt); + } + //- Deprecated(2018-03) text representation of the Switch value // \deprecated(2018-03) - use c_str() method inline const char* asText() const { return c_str(); }; diff --git a/src/OpenFOAM/primitives/enums/Enum.C b/src/OpenFOAM/primitives/enums/Enum.C index 1e269ff4ede1989814249bfec1da5f2d03cdae96..1ef0c4cc51cf8d44758ab48c8a3d944a9b2ff85c 100644 --- a/src/OpenFOAM/primitives/enums/Enum.C +++ b/src/OpenFOAM/primitives/enums/Enum.C @@ -81,14 +81,14 @@ template<class EnumType> EnumType Foam::Enum<EnumType>::get ( const word& enumName, - const EnumType defaultValue + const EnumType deflt ) const { const label idx = find(enumName); if (idx < 0) { - return defaultValue; + return deflt; } return EnumType(vals_[idx]); @@ -136,11 +136,11 @@ EnumType Foam::Enum<EnumType>::get template<class EnumType> -EnumType Foam::Enum<EnumType>::lookupOrDefault +EnumType Foam::Enum<EnumType>::getOrDefault ( const word& key, const dictionary& dict, - const EnumType defaultValue, + const EnumType deflt, const bool failsafe ) const { @@ -163,8 +163,8 @@ EnumType Foam::Enum<EnumType>::lookupOrDefault { IOWarningInFunction(dict) << enumName << " is not in enumeration: " << *this << nl - << "using failsafe " << get(defaultValue) - << " (value " << int(defaultValue) << ")" << endl; + << "using failsafe " << get(deflt) + << " (value " << int(deflt) << ")" << endl; } else { @@ -174,7 +174,7 @@ EnumType Foam::Enum<EnumType>::lookupOrDefault } } - return defaultValue; + return deflt; } diff --git a/src/OpenFOAM/primitives/enums/Enum.H b/src/OpenFOAM/primitives/enums/Enum.H index b5ef2a362444626456533d27cf0aca02878b6b1e..ede847bfc9064c545e0d04038374c9288ac6e858 100644 --- a/src/OpenFOAM/primitives/enums/Enum.H +++ b/src/OpenFOAM/primitives/enums/Enum.H @@ -132,7 +132,7 @@ public: //- The enumeration corresponding to the given name. // \return The enumeration or default if not found. - EnumType get(const word& enumName, const EnumType defaultValue) const; + EnumType get(const word& enumName, const EnumType deflt) const; //- The name corresponding to the given enumeration. // Return an empty word if not found. @@ -155,11 +155,11 @@ public: // // \return The value found or default if not found in dictionary. // FatalError (or Warning) if the enumerated name was incorrect. - EnumType lookupOrDefault + EnumType getOrDefault ( const word& key, //!< Lookup key. Uses LITERAL (not REGEX) const dictionary& dict, //!< dictionary - const EnumType defaultValue, //!< fallback if not found + const EnumType deflt, //!< fallback if not found const bool failsafe = false //!< Warn only on bad enumeration ) const; @@ -216,33 +216,51 @@ public: inline const word& operator[](const EnumType e) const; //- Return the enumeration corresponding to the given name or - //- defaultValue if the name is not found. + //- deflt if the name is not found. inline EnumType operator() ( const word& enumName, - const EnumType defaultValue + const EnumType deflt ) const; // Housekeeping + //- Find the key in the dictionary and return the corresponding + //- enumeration element based on its name. + // + // \return The value found or default if not found in dictionary. + // FatalError (or Warning) if the enumerated name was incorrect. + EnumType lookupOrDefault + ( + const word& key, //!< Lookup key. Uses LITERAL (not REGEX) + const dictionary& dict, //!< dictionary + const EnumType deflt, //!< fallback if not found + const bool failsafe = false //!< Warn only on bad enumeration + ) const + { + return getOrDefault(key, dict, deflt, failsafe); + } + //- Deprecated(2018-10) same as two-parameter get() // \deprecated(2018-10) - use two-parameter get() method - inline EnumType lookup(const word& key, const dictionary& dict) const + EnumType FOAM_DEPRECATED_FOR(2018-10, "get() method") + lookup(const word& key, const dictionary& dict) const { return get(key, dict); } //- Deprecated(2018-10) lookupOrDefault with warnings instead of error. - // \deprecated(2018-10) - use lookupOrDefault() with failsafe option - EnumType lookupOrFailsafe + // \deprecated(2018-10) - use getOrDefault() with failsafe option + EnumType FOAM_DEPRECATED_FOR(2018-10, "getOrDefault() method") + lookupOrFailsafe ( const word& key, const dictionary& dict, - const EnumType defaultValue + const EnumType deflt ) const { - return lookupOrDefault(key, dict, defaultValue, true); + return getOrDefault(key, dict, deflt, true); } }; diff --git a/src/OpenFOAM/primitives/ops/ops.H b/src/OpenFOAM/primitives/ops/ops.H index c47888e8d78c78f064778b07c861f9e5dc5a4b51..2692eff18f6a9449c5913c6a846ec90c070bb01b 100644 --- a/src/OpenFOAM/primitives/ops/ops.H +++ b/src/OpenFOAM/primitives/ops/ops.H @@ -27,9 +27,9 @@ InNamespace Foam Description - Various function objects for unary and binary operations. + Various functors for unary and binary operations. Can be used for parallel combine-reduce operations or other places - requiring a function object. + requiring a functor. \*---------------------------------------------------------------------------*/ @@ -159,6 +159,25 @@ EqOp(nopEq, (void)x) }; +// Operations taking one parameter, returning bool. +// The comparison value is defined during construction + +#define Bool1Op(opName, op) \ + \ + template<class T> \ + struct opName##Op1 \ + { \ + const T& value; \ + \ + opName##Op1(const T& v) : value(v) {} \ + \ + bool operator()(const T& x) const WARNRETURN \ + { \ + return op; \ + } \ + }; + + // Weighting operations #define WeightedOp(opName, op) \ @@ -216,10 +235,18 @@ BoolOp(lessEq, x <= y) BoolOp(greater, x > y) BoolOp(greaterEq, x >= y) +Bool1Op(equal, x == value) +Bool1Op(notEqual, x != value) +Bool1Op(less, x < value) +Bool1Op(lessEq, x <= value) +Bool1Op(greater, x > value) +Bool1Op(greaterEq, x >= value) + WeightedOp(multiply, (weight*y)) #undef Op #undef BoolOp +#undef Bool1Op #undef WeightedOp // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // diff --git a/src/OpenFOAM/primitives/ranges/MinMax/MinMax.H b/src/OpenFOAM/primitives/ranges/MinMax/MinMax.H index 59b1f16201068dc9d0fac04724837ef87e9a10a9..6c78dfccc9d3a58434e40222bbf936b8654b895c 100644 --- a/src/OpenFOAM/primitives/ranges/MinMax/MinMax.H +++ b/src/OpenFOAM/primitives/ranges/MinMax/MinMax.H @@ -161,6 +161,12 @@ public: // Static Member Functions + //- A semi-infinite range from minVal to the type max + inline static MinMax<T> ge(const T& minVal); + + //- A semi-infinite range from type min to maxVal + inline static MinMax<T> le(const T& maxVal); + //- A 0-1 range corresponding to the pTraits zero, one inline static MinMax<T> zero_one(); diff --git a/src/OpenFOAM/primitives/ranges/MinMax/MinMaxI.H b/src/OpenFOAM/primitives/ranges/MinMax/MinMaxI.H index afdf34916d467a36443c04fe7a86b6ecf1f8f350..d6ef1ab38e198811face2901e38de4f7f49665da 100644 --- a/src/OpenFOAM/primitives/ranges/MinMax/MinMaxI.H +++ b/src/OpenFOAM/primitives/ranges/MinMax/MinMaxI.H @@ -25,6 +25,20 @@ License // * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * // +template<class T> +inline Foam::MinMax<T> Foam::MinMax<T>::ge(const T& minVal) +{ + return MinMax<T>(minVal, pTraits<T>::max); +} + + +template<class T> +inline Foam::MinMax<T> Foam::MinMax<T>::le(const T& maxVal) +{ + return MinMax<T>(pTraits<T>::min, maxVal); +} + + template<class T> inline Foam::MinMax<T> Foam::MinMax<T>::zero_one() { diff --git a/src/OpenFOAM/primitives/ranges/scalarRange/scalarRange.C b/src/OpenFOAM/primitives/ranges/scalarRange/scalarRange.C index 221101fcf0313260b9ea022e2aa5546d6a1882ff..0786ed5ce73b235175d9d58dfba656c1f4aa83d1 100644 --- a/src/OpenFOAM/primitives/ranges/scalarRange/scalarRange.C +++ b/src/OpenFOAM/primitives/ranges/scalarRange/scalarRange.C @@ -36,8 +36,8 @@ const Foam::scalarRange Foam::scalarRange::null; const Foam::scalarRange Foam::scalarRange::always ( scalarRange::ALWAYS, - -Foam::GREAT, - Foam::GREAT + -GREAT, + GREAT ); @@ -158,21 +158,21 @@ Foam::Ostream& Foam::operator<<(Ostream& os, const scalarRange& range) switch (range.type_) { case scalarRange::EQ: - os << range.min_; + os << range.min(); break; case scalarRange::GE: case scalarRange::GT: - os << range.min_ << ":Inf"; + os << range.min() << ":Inf"; break; case scalarRange::LE: case scalarRange::LT: - os << "-Inf:" << range.max_; + os << "-Inf:" << range.max(); break; case scalarRange::GE_LE: - os << range.min_ << ':' << range.max_; + os << range.min() << ':' << range.max(); break; case scalarRange::ALWAYS: diff --git a/src/OpenFOAM/primitives/ranges/scalarRange/scalarRange.H b/src/OpenFOAM/primitives/ranges/scalarRange/scalarRange.H index 973f446de2d7f032886376cf4bad03060b8c6033..f71c97cf49365e3952c5c4e407cd36257959977b 100644 --- a/src/OpenFOAM/primitives/ranges/scalarRange/scalarRange.H +++ b/src/OpenFOAM/primitives/ranges/scalarRange/scalarRange.H @@ -34,6 +34,7 @@ Description used to define an empty (inverse) range. SeeAlso + Foam::MinMax Foam::predicates::scalars SourceFiles @@ -154,18 +155,27 @@ public: static scalarRange parse(const std::string& str); - //- Construct a greater-than-equals bound + //- Construct a greater-equals bound inline static scalarRange ge(const scalar minVal) noexcept; //- Construct a greater-than bound inline static scalarRange gt(const scalar minVal) noexcept; - //- Construct a less-than-equals bound + //- Construct a greater-equals zero bound + inline static scalarRange ge0() noexcept; + + //- Construct a greater-than zero bound + inline static scalarRange gt0() noexcept; + + //- Construct a less-equals bound inline static scalarRange le(const scalar maxVal) noexcept; //- Construct a less-than bound inline static scalarRange lt(const scalar maxVal) noexcept; + //- Construct a greater-equals 0, less-equals 1 bound + inline static scalarRange zero_one() noexcept; + // Member Functions @@ -192,13 +202,13 @@ public: inline scalar value() const; //- True if the value matches the condition. - inline bool match(const scalar& value) const; + inline bool match(const scalar& val) const; // Member Operators //- Identical to match(), for use as a predicate. - inline bool operator()(const scalar& value) const; + inline bool operator()(const scalar& val) const; inline bool operator==(const scalarRange& rhs) const noexcept; inline bool operator!=(const scalarRange& rhs) const noexcept; diff --git a/src/OpenFOAM/primitives/ranges/scalarRange/scalarRangeI.H b/src/OpenFOAM/primitives/ranges/scalarRange/scalarRangeI.H index 7de2ee93b769ec1e12cd6dbf9601f17b1e9a6122..42081f162720bc5829522efc3989cabd9c2e6251 100644 --- a/src/OpenFOAM/primitives/ranges/scalarRange/scalarRangeI.H +++ b/src/OpenFOAM/primitives/ranges/scalarRange/scalarRangeI.H @@ -40,7 +40,7 @@ inline Foam::scalarRange::scalarRange inline Foam::scalarRange::scalarRange() noexcept : - scalarRange(scalarRange::NONE, Foam::GREAT, -Foam::GREAT) + scalarRange(scalarRange::NONE, GREAT, -GREAT) {} @@ -62,7 +62,7 @@ inline Foam::scalarRange::scalarRange { clear(); // Inverted - explicitly mark as such } - else if (minVal == maxVal) + else if (equal(minVal, maxVal)) { type_ = EQ; } @@ -71,24 +71,42 @@ inline Foam::scalarRange::scalarRange inline Foam::scalarRange Foam::scalarRange::ge(const scalar minVal) noexcept { - return scalarRange(GE, minVal, Foam::VGREAT); + return scalarRange(scalarRange::GE, minVal, GREAT); } inline Foam::scalarRange Foam::scalarRange::gt(const scalar minVal) noexcept { - return scalarRange(GT, minVal, Foam::VGREAT); + return scalarRange(scalarRange::GT, minVal, GREAT); +} + + +inline Foam::scalarRange Foam::scalarRange::ge0() noexcept +{ + return scalarRange(scalarRange::GE, 0, GREAT); +} + + +inline Foam::scalarRange Foam::scalarRange::gt0() noexcept +{ + return scalarRange(scalarRange::GT, 0, GREAT); } inline Foam::scalarRange Foam::scalarRange::le(const scalar maxVal) noexcept { - return scalarRange(LE, -Foam::VGREAT, maxVal); + return scalarRange(scalarRange::LE, -GREAT, maxVal); } inline Foam::scalarRange Foam::scalarRange::lt(const scalar maxVal) noexcept { - return scalarRange(LT, -Foam::VGREAT, maxVal); + return scalarRange(scalarRange::LT, -GREAT, maxVal); +} + + +inline Foam::scalarRange Foam::scalarRange::zero_one() noexcept +{ + return scalarRange(scalarRange::GE_LE, 0, 1); } @@ -96,8 +114,8 @@ inline Foam::scalarRange Foam::scalarRange::lt(const scalar maxVal) noexcept inline void Foam::scalarRange::clear() noexcept { - min_ = Foam::GREAT; - max_ = -Foam::GREAT; + min_ = GREAT; + max_ = -GREAT; type_ = scalarRange::NONE; } @@ -136,16 +154,16 @@ inline Foam::scalar Foam::scalarRange::value() const { switch (type_) { - case EQ: // For equals, min and max are identical - case GE: - case GT: + case scalarRange::EQ: // For equals, min and max are identical + case scalarRange::GE: + case scalarRange::GT: return min_; - case LE: - case LT: + case scalarRange::LE: + case scalarRange::LT: return max_; - case GE_LE: + case scalarRange::GE_LE: // Multiply before adding to avoid possible overflow return (0.5 * min_) + (0.5 * max_); @@ -155,16 +173,16 @@ inline Foam::scalar Foam::scalarRange::value() const } -inline bool Foam::scalarRange::match(const scalar& value) const +inline bool Foam::scalarRange::match(const scalar& val) const { switch (type_) { - case EQ: return equal(value, min_); - case GE: return (value >= min_); - case GT: return (value > min_); - case LE: return (value <= max_); - case LT: return (value < max_); - case GE_LE: return (value >= min_ && value <= max_); + case EQ: return equal(val, min_); + case GE: return (val >= min_); + case GT: return (val > min_); + case LE: return (val <= max_); + case LT: return (val < max_); + case GE_LE: return (val >= min_ && val <= max_); case ALWAYS: return true; default: return false; } @@ -173,9 +191,9 @@ inline bool Foam::scalarRange::match(const scalar& value) const // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * // -inline bool Foam::scalarRange::operator()(const scalar& value) const +inline bool Foam::scalarRange::operator()(const scalar& val) const { - return match(value); + return match(val); } diff --git a/src/OpenFOAM/primitives/ranges/scalarRange/scalarRanges.H b/src/OpenFOAM/primitives/ranges/scalarRange/scalarRanges.H index 1de90bb0f5d4bf308c8be91f116d444a76956b12..d94795b4308c8711a83e84ac49ba6f29014e96c5 100644 --- a/src/OpenFOAM/primitives/ranges/scalarRange/scalarRanges.H +++ b/src/OpenFOAM/primitives/ranges/scalarRange/scalarRanges.H @@ -67,7 +67,11 @@ public: //- Construct by parsing string for scalar ranges // The individual items are space, comma or semicolon delimited. // Optionally report when any range failed to parse - inline scalarRanges(const std::string& str, bool verbose = true); + inline explicit scalarRanges + ( + const std::string& str, + bool verbose = true + ); // Static Constructors diff --git a/src/OpenFOAM/primitives/strings/lists/hashedWordList.H b/src/OpenFOAM/primitives/strings/lists/hashedWordList.H index cd401dbb5afc0b2f2b94a24a09e78e2e48a8fb44..f58fc24838c32c5a2d6d5435a74fc7088968d827 100644 --- a/src/OpenFOAM/primitives/strings/lists/hashedWordList.H +++ b/src/OpenFOAM/primitives/strings/lists/hashedWordList.H @@ -182,10 +182,10 @@ public: //- Deprecated(2019-01) Is the specified name found in the list? // \deprecated(2019-01) - use found() method - inline bool contains(const word& name) const - FOAM_DEPRECATED_FOR(2019-01, "found() method") + bool FOAM_DEPRECATED_FOR(2019-01, "found() method") + contains(const word& name) const { - return this-found(name); + return this->found(name); } }; diff --git a/src/OpenFOAM/primitives/strings/regex/regExpCxx.C b/src/OpenFOAM/primitives/strings/regex/regExpCxx.C index 0f99c82dc1a976d03ae6002f99232bd541c59906..f5dfe943c49ca5494686643be8e1c012e1aec47e 100644 --- a/src/OpenFOAM/primitives/strings/regex/regExpCxx.C +++ b/src/OpenFOAM/primitives/strings/regex/regExpCxx.C @@ -108,7 +108,7 @@ static std::string error_string(const std::regex_error& err) bool Foam::regExpCxx::set(const char* pattern, bool ignoreCase) { - clear(); + clear(); // Also sets ok_ = false size_t len = (pattern ? strlen(pattern) : 0); @@ -139,7 +139,7 @@ bool Foam::regExpCxx::set(const char* pattern, bool ignoreCase) try { re_.assign(pat, flags); - return true; + ok_ = true; } catch (const std::regex_error& err) { @@ -151,13 +151,13 @@ bool Foam::regExpCxx::set(const char* pattern, bool ignoreCase) } } - return false; + return ok_; } bool Foam::regExpCxx::set(const std::string& pattern, bool ignoreCase) { - clear(); + clear(); // Also sets ok_ = false auto len = pattern.size(); @@ -188,7 +188,7 @@ bool Foam::regExpCxx::set(const std::string& pattern, bool ignoreCase) try { re_.assign(pat, pattern.end(), flags); - return true; + ok_ = true; } catch (const std::regex_error& err) { @@ -200,7 +200,7 @@ bool Foam::regExpCxx::set(const std::string& pattern, bool ignoreCase) } } - return false; + return ok_; } diff --git a/src/OpenFOAM/primitives/strings/regex/regExpCxx.H b/src/OpenFOAM/primitives/strings/regex/regExpCxx.H index a96ad8e84aa6f2223c98c3b1572dda322896d891..d56d16f927bd8a122334180b0d82bca85e9ec6f2 100644 --- a/src/OpenFOAM/primitives/strings/regex/regExpCxx.H +++ b/src/OpenFOAM/primitives/strings/regex/regExpCxx.H @@ -41,7 +41,7 @@ Description Note The C++11 regular expressions may be broken on some compilers. For example, gcc 4.8 is known to fail. - For these systems the POSIX implementation should be used. + For these systems the POSIX implementation or alternative must be used. SourceFiles regExpCxxI.H @@ -66,7 +66,7 @@ namespace Foam class regExpCxx { - // Private data + // Private Data //- Regular expression (using char type) std::regex re_; @@ -132,7 +132,7 @@ public: ~regExpCxx() = default; - // Member functions + // Member Functions // Access diff --git a/src/OpenFOAM/primitives/strings/regex/regExpCxxI.H b/src/OpenFOAM/primitives/strings/regex/regExpCxxI.H index 92d84a003782ac92ba5cb54858fb41301e6801c5..b60cbf1e8c9cfa2f571e316670d85673cb35a5a5 100644 --- a/src/OpenFOAM/primitives/strings/regex/regExpCxxI.H +++ b/src/OpenFOAM/primitives/strings/regex/regExpCxxI.H @@ -157,7 +157,8 @@ inline void Foam::regExpCxx::swap(regExpCxx& rgx) } -inline std::string::size_type Foam::regExpCxx::find(const std::string& text) const +inline std::string::size_type +Foam::regExpCxx::find(const std::string& text) const { std::smatch mat; if (!text.empty() && std::regex_search(text, mat, re_))