Merge branch 'feature-dictionary-checking' into 'develop'

Feature dictionary checking

See merge request Development/OpenFOAM-plus!259
This commit is contained in:
Andrew Heather
2019-05-22 10:29:41 +01:00
committed by Andrew Heather
43 changed files with 1023 additions and 266 deletions

View File

@ -2,7 +2,7 @@
========= | ========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | \\ / O peration |
\\ / A nd | Copyright (C) 2017-2018 OpenCFD Ltd. \\ / A nd | Copyright (C) 2017-2019 OpenCFD Ltd.
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
@ -34,6 +34,8 @@ Description
#include "IOobject.H" #include "IOobject.H"
#include "IFstream.H" #include "IFstream.H"
#include "dictionary.H" #include "dictionary.H"
#include "ops.H"
#include "scalarRange.H"
#include "stringOps.H" #include "stringOps.H"
using namespace Foam; 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> // Try with *entry (from findEntry) and get<scalar>
scalar try_getScalar(const entry* eptr, const word& k) scalar try_getScalar(const entry* eptr, const word& k)
{ {
@ -311,6 +349,7 @@ int main(int argc, char *argv[])
IStringStream IStringStream
( (
"good 3.14159;\n" "good 3.14159;\n"
"negative -3.14159;\n"
"empty;\n" "empty;\n"
// "bad text;\n" // always fails // "bad text;\n" // always fails
// "bad 3.14159 1234;\n" // fails for readScalar // "bad 3.14159 1234;\n" // fails for readScalar
@ -338,6 +377,26 @@ int main(int argc, char *argv[])
try_getScalar(dict2, "empty"); 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> // With findEntry and get<scalar>
{ {
Info<< nl Info<< nl

View File

@ -27,12 +27,16 @@ Description
\*---------------------------------------------------------------------------*/ \*---------------------------------------------------------------------------*/
#include "argList.H" #include "argList.H"
#include "IOobject.H"
#include "IOstreams.H" #include "IOstreams.H"
#include "IFstream.H" #include "IFstream.H"
#include "Switch.H" #include "Switch.H"
#include "SubStrings.H"
#include "regExpCxx.H" #include "regExpCxx.H"
#ifndef _WIN32
#include "regExpPosix.H" #include "regExpPosix.H"
#endif
using namespace Foam; using namespace Foam;
@ -83,6 +87,7 @@ static Ostream& operator<<(Ostream& os, const regExpCxx::results_type& sm)
// Simple output of match groups // Simple output of match groups
#ifndef _WIN32
static Ostream& operator<<(Ostream& os, const regExpPosix::results_type& sm) static Ostream& operator<<(Ostream& os, const regExpPosix::results_type& sm)
{ {
for (std::smatch::size_type i = 1; i < sm.size(); ++i) 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; return os;
} }
#endif
template<class RegexType> template<class RegexType>
@ -209,7 +215,6 @@ void generalTests()
} }
template<class RegexType> template<class RegexType>
void testExpressions(const UList<regexTest>& tests) void testExpressions(const UList<regexTest>& tests)
{ {
@ -293,11 +298,13 @@ int main(int argc, char *argv[])
"Test C++11 regular expressions" "Test C++11 regular expressions"
); );
#ifndef _WIN32
argList::addBoolOption argList::addBoolOption
( (
"posix", "posix",
"Test POSIX regular expressions" "Test POSIX regular expressions"
); );
#endif
argList::addArgument("file"); argList::addArgument("file");
argList::addArgument("..."); argList::addArgument("...");
@ -306,6 +313,17 @@ int main(int argc, char *argv[])
#include "setRootCase.H" #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"})) if (!args.count({"cxx", "posix"}))
{ {
Info<< "Specified one or more of -cxx, -posix" << nl; Info<< "Specified one or more of -cxx, -posix" << nl;
@ -321,10 +339,12 @@ int main(int argc, char *argv[])
generalTests<regExpCxx>(); generalTests<regExpCxx>();
} }
#ifndef _WIN32
if (args.found("posix")) if (args.found("posix"))
{ {
generalTests<regExpPosix>(); generalTests<regExpPosix>();
} }
#endif
} }
for (label argi = 1; argi < args.size(); ++argi) for (label argi = 1; argi < args.size(); ++argi)
@ -339,10 +359,12 @@ int main(int argc, char *argv[])
testExpressions<regExpCxx>(tests); testExpressions<regExpCxx>(tests);
} }
#ifndef _WIN32
if (args.found("posix")) if (args.found("posix"))
{ {
testExpressions<regExpPosix>(tests); testExpressions<regExpPosix>(tests);
} }
#endif
} }
Info<< "\nDone" << nl << endl; Info<< "\nDone" << nl << endl;

View File

@ -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" )
)
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

View File

@ -36,6 +36,8 @@ Description
#include "FlatOutput.H" #include "FlatOutput.H"
#include "Tuple2.H" #include "Tuple2.H"
#include "StringStream.H" #include "StringStream.H"
#include "ops.H"
#include "bitSet.H"
using namespace Foam; using namespace Foam;
@ -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: // 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; Info<< "\nEnd\n" << endl;
return 0; return 0;

View File

@ -2,7 +2,7 @@
========= | ========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | \\ / O peration |
\\ / A nd | \\ / A nd | Copyright (C) 2019 OpenCFD Ltd.
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
| Copyright (C) 2011-2016 OpenFOAM Foundation | Copyright (C) 2011-2016 OpenFOAM Foundation
@ -313,7 +313,7 @@ void rewriteField
if if
( (
boundaryField.found(patchName) boundaryField.found(patchName)
&& !boundaryField.found(newName, false, false) && !boundaryField.found(newName, keyType::LITERAL)
) )
{ {
Info<< " Changing entry " << patchName << " to " << newName Info<< " Changing entry " << patchName << " to " << newName

View File

@ -37,6 +37,8 @@ InfoSwitches
writePrecision 6; writePrecision 6;
writeDictionaries 0; writeDictionaries 0;
// Report optional dictionary entries. For value > 1, treat as fatal.
writeOptionalEntries 0; writeOptionalEntries 0;
// Write lagrangian "positions" file in v1706 format (and earlier) // Write lagrangian "positions" file in v1706 format (and earlier)

View File

@ -32,6 +32,7 @@ Description
#ifndef regExp_H #ifndef regExp_H
#define regExp_H #define regExp_H
#include "regExpCxx.H"
#include "regExpPosix.H" #include "regExpPosix.H"
#include "regExpFwd.H" #include "regExpFwd.H"

View File

@ -39,7 +39,12 @@ namespace Foam
class regExpCxx; class regExpCxx;
class regExpPosix; class regExpPosix;
// Newer compilers support regex directly
#if (_GLIBCXX_RELEASE >= 7) || (__clang_major__ >= 7)
typedef regExpCxx regExp;
#else
typedef regExpPosix regExp; typedef regExpPosix regExp;
#endif
} }
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

View File

@ -120,7 +120,7 @@ public:
inline ~regExpPosix(); inline ~regExpPosix();
// Member functions // Member Functions
// Access // Access

View File

@ -243,8 +243,7 @@ public:
//- Deprecated(2019-01) Pointing at a valid storage node //- Deprecated(2019-01) Pointing at a valid storage node
// \deprecated(2019-01) - use good() method // \deprecated(2019-01) - use good() method
inline bool found() const bool FOAM_DEPRECATED_FOR(2019-01, "good() method") found() const
FOAM_DEPRECATED_FOR(2019-01, "good() method")
{ {
return this->good(); return this->good();
} }
@ -297,8 +296,7 @@ public:
//- Deprecated(2019-01) Pointing at a valid storage node //- Deprecated(2019-01) Pointing at a valid storage node
// \deprecated(2019-01) - use good() method // \deprecated(2019-01) - use good() method
inline bool found() const bool FOAM_DEPRECATED_FOR(2019-01, "good() method") found() const
FOAM_DEPRECATED_FOR(2019-01, "good() method")
{ {
return this->good(); return this->good();
} }

View File

@ -219,8 +219,7 @@ public:
//- Deprecated(2019-01) Pointing at a valid storage node //- Deprecated(2019-01) Pointing at a valid storage node
// \deprecated(2019-01) - use good() method // \deprecated(2019-01) - use good() method
inline bool found() const bool FOAM_DEPRECATED_FOR(2019-01, "good() method") found() const
FOAM_DEPRECATED_FOR(2019-01, "good() method")
{ {
return this->good(); return this->good();
} }
@ -271,8 +270,7 @@ public:
//- Deprecated(2019-01) Pointing at a valid storage node //- Deprecated(2019-01) Pointing at a valid storage node
// \deprecated(2019-01) - use good() method // \deprecated(2019-01) - use good() method
inline bool found() const bool FOAM_DEPRECATED_FOR(2019-01, "good() method") found() const
FOAM_DEPRECATED_FOR(2019-01, "good() method")
{ {
return this->good(); return this->good();
} }

View File

@ -2,7 +2,7 @@
========= | ========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | \\ / O peration |
\\ / A nd | Copyright (C) 2015-2018 OpenCFD Ltd. \\ / A nd | Copyright (C) 2015-2019 OpenCFD Ltd.
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
| Copyright (C) 2011-2017 OpenFOAM Foundation | Copyright (C) 2011-2017 OpenFOAM Foundation
@ -42,13 +42,12 @@ namespace Foam
const Foam::dictionary Foam::dictionary::null; const Foam::dictionary Foam::dictionary::null;
bool Foam::dictionary::writeOptionalEntries int Foam::dictionary::writeOptionalEntries
( (
Foam::debug::infoSwitch("writeOptionalEntries", 0) Foam::debug::infoSwitch("writeOptionalEntries", 0)
); );
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
Foam::dictionary::dictionary() 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 bool Foam::dictionary::found
( (
const word& keyword, const word& keyword,
enum keyType::option matchOpt enum keyType::option matchOpt
) const ) 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)); const const_searcher finder(csearch(keyword, matchOpt));
if (!finder.found()) if (!finder.good())
{ {
FatalIOErrorInFunction(*this) FatalIOErrorInFunction(*this)
<< "Entry '" << keyword << "' not found in dictionary " << "Entry '" << keyword << "' not found in dictionary "
<< name() << name() << nl
<< exit(FatalIOError); << exit(FatalIOError);
} }
@ -395,7 +411,7 @@ bool Foam::dictionary::substituteKeyword(const word& keyword, bool mergeEntry)
const const_searcher finder(csearch(varName, keyType::REGEX_RECURSIVE)); const const_searcher finder(csearch(varName, keyType::REGEX_RECURSIVE));
// If defined insert its entries into this dictionary // If defined insert its entries into this dictionary
if (finder.found()) if (finder.good())
{ {
for (const entry& e : finder.dict()) for (const entry& e : finder.dict())
{ {
@ -427,7 +443,7 @@ bool Foam::dictionary::substituteScopedKeyword
const auto finder(csearchScoped(varName, keyType::REGEX_RECURSIVE)); const auto finder(csearchScoped(varName, keyType::REGEX_RECURSIVE));
// If defined insert its entries into this dictionary // If defined insert its entries into this dictionary
if (finder.found()) if (finder.good())
{ {
for (const entry& e : finder.dict()) for (const entry& e : finder.dict())
{ {
@ -473,11 +489,11 @@ const Foam::dictionary& Foam::dictionary::subDict(const word& keyword) const
// Allow patterns, non-recursive // Allow patterns, non-recursive
const const_searcher finder(csearch(keyword, keyType::REGEX)); const const_searcher finder(csearch(keyword, keyType::REGEX));
if (!finder.found()) if (!finder.good())
{ {
FatalIOErrorInFunction(*this) FatalIOErrorInFunction(*this)
<< "Entry '" << keyword << "' not found in dictionary " << "Entry '" << keyword << "' not found in dictionary "
<< name() << name() << nl
<< exit(FatalIOError); << exit(FatalIOError);
} }
@ -490,11 +506,11 @@ Foam::dictionary& Foam::dictionary::subDict(const word& keyword)
// Allow patterns, non-recursive // Allow patterns, non-recursive
searcher finder(search(keyword, keyType::REGEX)); searcher finder(search(keyword, keyType::REGEX));
if (!finder.found()) if (!finder.good())
{ {
FatalIOErrorInFunction(*this) FatalIOErrorInFunction(*this)
<< "Entry '" << keyword << "' not found in dictionary " << "Entry '" << keyword << "' not found in dictionary "
<< name() << name() << nl
<< exit(FatalIOError); << exit(FatalIOError);
} }
@ -526,7 +542,7 @@ Foam::dictionary Foam::dictionary::subOrEmptyDict
<< exit(FatalIOError); << exit(FatalIOError);
} }
if (finder.found()) if (finder.good())
{ {
IOWarningInFunction(*this) IOWarningInFunction(*this)
<< "Entry '" << keyword << "Entry '" << keyword
@ -552,7 +568,7 @@ const Foam::dictionary& Foam::dictionary::optionalSubDict
return finder.dict(); return finder.dict();
} }
if (finder.found()) if (finder.good())
{ {
IOWarningInFunction(*this) IOWarningInFunction(*this)
<< "Entry '" << keyword << "Entry '" << keyword
@ -611,7 +627,7 @@ Foam::entry* Foam::dictionary::add(entry* entryPtr, bool mergeEntry)
auto iter = hashedEntries_.find(entryPtr->keyword()); auto iter = hashedEntries_.find(entryPtr->keyword());
if (mergeEntry && iter.found()) if (mergeEntry && iter.good())
{ {
// Merge dictionary with dictionary // Merge dictionary with dictionary
if (iter()->isDict() && entryPtr->isDict()) if (iter()->isDict() && entryPtr->isDict())
@ -786,7 +802,7 @@ bool Foam::dictionary::merge(const dictionary& dict)
{ {
auto fnd = hashedEntries_.find(e.keyword()); auto fnd = hashedEntries_.find(e.keyword());
if (fnd.found()) if (fnd.good())
{ {
// Recursively merge sub-dictionaries // Recursively merge sub-dictionaries
// TODO: merge without copying // TODO: merge without copying

View File

@ -285,8 +285,9 @@ private:
// Private Data // Private Data
//- Report optional keywords and values if not present in dictionary //- Report optional keywords and values if not present in dictionary
// For value greater than 1: fatal.
// Set/unset via an InfoSwitch // Set/unset via an InfoSwitch
static bool writeOptionalEntries; static int writeOptionalEntries;
//- The dictionary name //- The dictionary name
fileName name_; fileName name_;
@ -359,6 +360,10 @@ private:
) const; ) const;
//- Emit IOError about bad input for the entry
void raiseBadInput(const word& keyword) const;
public: public:
// Declare name of the class and its debug switch // Declare name of the class and its debug switch
@ -532,7 +537,6 @@ public:
enum keyType::option matchOpt = keyType::REGEX 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 //- Find and return a sub-dictionary pointer if present
// (and a sub-dictionary) otherwise return nullptr. // (and a sub-dictionary) otherwise return nullptr.
// //
@ -543,6 +547,7 @@ public:
enum keyType::option matchOpt = keyType::REGEX enum keyType::option matchOpt = keyType::REGEX
) const; ) const;
//- Search for an entry (const access) with the given keyword.
// //
// \param matchOpt the default search is non-recursive with patterns // \param matchOpt the default search is non-recursive with patterns
// //
@ -553,6 +558,16 @@ public:
enum keyType::option matchOpt enum keyType::option matchOpt
) const; ) const;
//- 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
ITstream& lookup
(
const word& keyword,
enum keyType::option matchOpt = keyType::REGEX
) const;
//- Find and return a T. //- Find and return a T.
//- FatalIOError if not found, or if the number of tokens is incorrect. //- FatalIOError if not found, or if the number of tokens is incorrect.
// //
@ -564,22 +579,12 @@ public:
enum keyType::option matchOpt = keyType::REGEX enum keyType::option matchOpt = keyType::REGEX
) const; ) const;
//- 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
ITstream& lookup
(
const word& keyword,
enum keyType::option matchOpt = keyType::REGEX
) const;
//- Find and return a T, or return the given default value. //- Find and return a T, or return the given default value.
//- FatalIOError if it is found and the number of tokens is incorrect. //- FatalIOError if it is found and the number of tokens is incorrect.
// //
// \param matchOpt the default search is non-recursive with patterns // \param matchOpt the default search is non-recursive with patterns
template<class T> template<class T>
T lookupOrDefault T getOrDefault
( (
const word& keyword, const word& keyword,
const T& deflt, const T& deflt,
@ -592,7 +597,7 @@ public:
// //
// \param matchOpt the default search is non-recursive with patterns // \param matchOpt the default search is non-recursive with patterns
template<class T> template<class T>
T lookupOrAddDefault T getOrAdd
( (
const word& keyword, const word& keyword,
const T& deflt, const T& deflt,
@ -633,6 +638,86 @@ public:
enum keyType::option matchOpt = keyType::REGEX enum keyType::option matchOpt = keyType::REGEX
) const; ) 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. //- Check if entry is found and and is a sub-dictionary.
// //
// Search type: non-recursive with patterns. // Search type: non-recursive with patterns.
@ -983,6 +1068,21 @@ public:
enum keyType::option enum keyType::option
) const; ) const;
//- Find and return an entry data stream,
//- 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
ITstream& lookupCompat
(
const word& keyword,
std::initializer_list<std::pair<const char*,int>> compat,
enum keyType::option = keyType::REGEX
) const;
//- Find and return a T //- Find and return a T
//- using any compatibility names if needed. //- using any compatibility names if needed.
//- FatalIOError if not found, or if there are excess tokens. //- FatalIOError if not found, or if there are excess tokens.
@ -1000,21 +1100,6 @@ public:
enum keyType::option = keyType::REGEX enum keyType::option = keyType::REGEX
) const; ) const;
//- Find and return an entry data stream,
//- 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
ITstream& lookupCompat
(
const word& keyword,
std::initializer_list<std::pair<const char*,int>> compat,
enum keyType::option = keyType::REGEX
) const;
//- Find and return a T, or return the given default value //- Find and return a T, or return the given default value
//- using any compatibility names if needed. //- using any compatibility names if needed.
// Default search: non-recursive with patterns. // Default search: non-recursive with patterns.
@ -1024,7 +1109,7 @@ public:
// \param recursive search parent dictionaries // \param recursive search parent dictionaries
// \param patternMatch use regular expressions // \param patternMatch use regular expressions
template<class T> template<class T>
T lookupOrDefaultCompat T getOrDefaultCompat
( (
const word& keyword, const word& keyword,
std::initializer_list<std::pair<const char*,int>> compat, std::initializer_list<std::pair<const char*,int>> compat,
@ -1105,10 +1190,63 @@ public:
// Housekeeping // 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) find and return an entry data stream
// //
// \deprecated(2018-07) - use lookup() method // \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); return lookup(keyword);
} }
@ -1116,7 +1254,8 @@ public:
//- Deprecated(2018-10) find and return a T. //- Deprecated(2018-10) find and return a T.
// \deprecated(2018-10) - use get() method // \deprecated(2018-10) - use get() method
template<class T> template<class T>
T lookupType T FOAM_DEPRECATED_FOR(2018-10, "get() method")
lookupType
( (
const word& keyword, const word& keyword,
bool recursive = false, bool recursive = false,
@ -1128,7 +1267,8 @@ public:
//- Deprecated(2018-10) //- Deprecated(2018-10)
// \deprecated(2018-10) - use keyType::option version // \deprecated(2018-10) - use keyType::option version
bool found bool FOAM_DEPRECATED_FOR(2018-10, "found(keyType::option)")
found
( (
const word& keyword, const word& keyword,
bool recursive, bool recursive,
@ -1140,7 +1280,9 @@ public:
//- Deprecated(2018-10) //- Deprecated(2018-10)
// \deprecated(2018-10) - use keyType::option version // \deprecated(2018-10) - use keyType::option version
entry* lookupEntryPtr entry*
FOAM_DEPRECATED_FOR(2018-10, "lookupEntryPtr(keyType::option)")
lookupEntryPtr
( (
const word& keyword, const word& keyword,
bool recursive, bool recursive,
@ -1152,7 +1294,9 @@ public:
//- Deprecated(2018-10) //- Deprecated(2018-10)
// \deprecated(2018-10) - use keyType::option version // \deprecated(2018-10) - use keyType::option version
const entry* lookupEntryPtr const entry*
FOAM_DEPRECATED_FOR(2018-10, "lookupEntryPtr(keyType::option)")
lookupEntryPtr
( (
const word& keyword, const word& keyword,
bool recursive, bool recursive,
@ -1164,7 +1308,9 @@ public:
//- Deprecated(2018-10) //- Deprecated(2018-10)
// \deprecated(2018-10) - use keyType::option version // \deprecated(2018-10) - use keyType::option version
const entry* lookupScopedEntryPtr const entry*
FOAM_DEPRECATED_FOR(2018-10, "lookupScopedEntryPtr(keyType::option)")
lookupScopedEntryPtr
( (
const word& keyword, const word& keyword,
bool recursive, bool recursive,
@ -1180,7 +1326,9 @@ public:
// //
// Search type: non-recursive with patterns. // Search type: non-recursive with patterns.
// \deprecated(2018-10) - use findDict() method // \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); return findDict(keyword, keyType::REGEX);
} }
@ -1191,14 +1339,18 @@ public:
// //
// Search type: non-recursive with patterns. // Search type: non-recursive with patterns.
// \deprecated(2018-10) - use findDict() method // \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); return findDict(keyword, keyType::REGEX);
} }
//- Deprecated(2018-10) //- Deprecated(2018-10)
// \deprecated(2018-10) - use keyType::option version // \deprecated(2018-10) - use keyType::option version
const entry& lookupEntry const entry&
FOAM_DEPRECATED_FOR(2018-10, "lookupEntry(keyType::option)")
lookupEntry
( (
const word& keyword, const word& keyword,
bool recursive, bool recursive,
@ -1210,7 +1362,9 @@ public:
//- Deprecated(2018-10) //- Deprecated(2018-10)
// \deprecated(2018-10) - use keyType::option version // \deprecated(2018-10) - use keyType::option version
ITstream& lookup ITstream&
FOAM_DEPRECATED_FOR(2018-10, "lookup(keyType::option)")
lookup
( (
const word& keyword, const word& keyword,
bool recursive, bool recursive,
@ -1223,7 +1377,8 @@ public:
//- Deprecated(2018-10) //- Deprecated(2018-10)
// \deprecated(2018-10) - use keyType::option version // \deprecated(2018-10) - use keyType::option version
template<class T> template<class T>
T lookupOrDefault T FOAM_DEPRECATED_FOR(2018-10, "lookupOrDefault(keyType::option)")
lookupOrDefault
( (
const word& keyword, const word& keyword,
const T& deflt, const T& deflt,
@ -1239,7 +1394,8 @@ public:
//- Deprecated(2018-10) //- Deprecated(2018-10)
// \deprecated(2018-10) - use keyType::option version // \deprecated(2018-10) - use keyType::option version
template<class T> template<class T>
T lookupOrAddDefault T FOAM_DEPRECATED_FOR(2018-10, "lookupOrAddDefault(keyType::option)")
lookupOrAddDefault
( (
const word& keyword, const word& keyword,
const T& deflt, const T& deflt,
@ -1255,7 +1411,9 @@ public:
//- Deprecated(2018-10) //- Deprecated(2018-10)
// \deprecated(2018-10) - use keyType::option version // \deprecated(2018-10) - use keyType::option version
template<class T> template<class T>
bool readIfPresent bool
FOAM_DEPRECATED_FOR(2018-10, "readIfPresent(keyType::option)")
readIfPresent
( (
const word& keyword, const word& keyword,
T& val, T& val,

View File

@ -2,7 +2,7 @@
========= | ========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | \\ / O peration |
\\ / A nd | Copyright (C) 2017-2018 OpenCFD Ltd. \\ / A nd | Copyright (C) 2017-2019 OpenCFD Ltd.
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
@ -54,7 +54,7 @@ Foam::dictionary::const_searcher Foam::dictionary::csearchCompat
{ {
const_searcher finder(csearch(keyword, matchOpt)); const_searcher finder(csearch(keyword, matchOpt));
if (finder.found()) if (finder.good())
{ {
return finder; return finder;
} }
@ -63,7 +63,7 @@ Foam::dictionary::const_searcher Foam::dictionary::csearchCompat
{ {
finder = csearch(word::validate(iter.first), matchOpt); finder = csearch(word::validate(iter.first), matchOpt);
if (finder.found()) if (finder.good())
{ {
// Only want a single warning (on master), but guard with a // Only want a single warning (on master), but guard with a
// parRun check to avoid Pstream::master() when Pstream has not // parRun check to avoid Pstream::master() when Pstream has not
@ -101,7 +101,7 @@ bool Foam::dictionary::foundCompat
enum keyType::option matchOpt enum keyType::option matchOpt
) const ) 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)); const const_searcher finder(csearchCompat(keyword, compat, matchOpt));
if (!finder.found()) if (!finder.good())
{ {
FatalIOErrorInFunction(*this) FatalIOErrorInFunction(*this)
<< "Entry '" << keyword << "' not found in dictionary " << "Entry '" << keyword << "' not found in dictionary "

View File

@ -130,7 +130,7 @@ Foam::dictionary::const_searcher Foam::dictionary::csearchDotScoped
// a.b.c.d it would try // a.b.c.d it would try
// a.b, a.b.c, a.b.c.d // a.b, a.b.c, a.b.c.d
if (!finder.found()) if (!finder.good())
{ {
while (!finder.isDict()) while (!finder.isDict())
{ {
@ -225,7 +225,7 @@ Foam::dictionary::const_searcher Foam::dictionary::csearchSlashScoped
auto finder = dictPtr->csearch(key, matchOpt); auto finder = dictPtr->csearch(key, matchOpt);
if (finder.found()) if (finder.good())
{ {
if (remaining) if (remaining)
{ {
@ -269,7 +269,7 @@ Foam::dictionary::const_searcher Foam::dictionary::csearch
auto iter = hashedEntries_.cfind(keyword); auto iter = hashedEntries_.cfind(keyword);
if (iter.found()) if (iter.good())
{ {
finder.set(iter.val()); finder.set(iter.val());
return finder; return finder;
@ -425,7 +425,7 @@ const Foam::dictionary* Foam::dictionary::cfindScopedDict
auto iter = dictPtr->hashedEntries_.cfind(cmpt); auto iter = dictPtr->hashedEntries_.cfind(cmpt);
if (iter.found()) if (iter.good())
{ {
const entry *eptr = iter.val(); const entry *eptr = iter.val();
@ -532,7 +532,7 @@ Foam::dictionary* Foam::dictionary::makeScopedDict(const fileName& dictPath)
auto iter = dictPtr->hashedEntries_.find(cmptName); auto iter = dictPtr->hashedEntries_.find(cmptName);
if (iter.found()) if (iter.good())
{ {
entry *eptr = iter.val(); entry *eptr = iter.val();
@ -581,7 +581,7 @@ bool Foam::dictionary::remove(const word& keyword)
{ {
auto iter = hashedEntries_.find(keyword); auto iter = hashedEntries_.find(keyword);
if (iter.found()) if (iter.good())
{ {
// Delete from patterns // Delete from patterns
auto wcLink = patterns_.begin(); auto wcLink = patterns_.begin();
@ -621,7 +621,7 @@ bool Foam::dictionary::changeKeyword
// Check that oldKeyword exists and can be changed // Check that oldKeyword exists and can be changed
auto iter = hashedEntries_.find(oldKeyword); auto iter = hashedEntries_.find(oldKeyword);
if (!iter.found()) if (!iter.good())
{ {
return false; return false;
} }
@ -638,7 +638,7 @@ bool Foam::dictionary::changeKeyword
auto iter2 = hashedEntries_.find(newKeyword); auto iter2 = hashedEntries_.find(newKeyword);
// newKeyword already exists // newKeyword already exists
if (iter2.found()) if (iter2.good())
{ {
if (overwrite) if (overwrite)
{ {

View File

@ -2,7 +2,7 @@
========= | ========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | \\ / O peration |
\\ / A nd | Copyright (C) 2017-2018 OpenCFD Ltd. \\ / A nd | Copyright (C) 2017-2019 OpenCFD Ltd.
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
| Copyright (C) 2011-2017 OpenFOAM Foundation | 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> template<class T>
T Foam::dictionary::getCompat T Foam::dictionary::getCompat
( (
@ -79,40 +93,7 @@ T Foam::dictionary::getCompat
template<class T> 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
{
const const_searcher finder(csearchCompat(keyword, compat, matchOpt));
if (finder.found())
{
ITstream& is = finder.ptr()->stream();
is >> val;
checkITstream(is, keyword);
return true;
}
else if (mandatory)
{
FatalIOErrorInFunction(*this)
<< "Entry '" << keyword << "' not found in dictionary "
<< name()
<< exit(FatalIOError);
}
return false;
}
template<class T>
T Foam::dictionary::lookupOrDefault
( (
const word& keyword, const word& keyword,
const T& deflt, const T& deflt,
@ -121,7 +102,7 @@ T Foam::dictionary::lookupOrDefault
{ {
const const_searcher finder(csearch(keyword, matchOpt)); const const_searcher finder(csearch(keyword, matchOpt));
if (finder.found()) if (finder.good())
{ {
T val; T val;
@ -133,11 +114,20 @@ T Foam::dictionary::lookupOrDefault
return val; return val;
} }
else if (writeOptionalEntries) else if (writeOptionalEntries)
{
if (writeOptionalEntries > 1)
{
FatalIOErrorInFunction(*this)
<< "Optional entry '" << keyword
<< "' not found. Default '" << deflt << "' ignored" << nl
<< exit(FatalIOError);
}
else
{ {
IOInfoInFunction(*this) IOInfoInFunction(*this)
<< "Optional entry '" << keyword << "Optional entry '" << keyword
<< "' not found, using default value '" << deflt << "'" << "' not found. Using default '" << deflt << "'" << nl;
<< nl; }
} }
return deflt; return deflt;
@ -145,7 +135,7 @@ T Foam::dictionary::lookupOrDefault
template<class T> template<class T>
T Foam::dictionary::lookupOrAddDefault T Foam::dictionary::getOrAdd
( (
const word& keyword, const word& keyword,
const T& deflt, const T& deflt,
@ -154,7 +144,7 @@ T Foam::dictionary::lookupOrAddDefault
{ {
const const_searcher finder(csearch(keyword, matchOpt)); const const_searcher finder(csearch(keyword, matchOpt));
if (finder.found()) if (finder.good())
{ {
T val; T val;
@ -166,11 +156,135 @@ T Foam::dictionary::lookupOrAddDefault
return val; return val;
} }
else if (writeOptionalEntries) else if (writeOptionalEntries)
{
if (writeOptionalEntries > 1)
{
FatalIOErrorInFunction(*this)
<< "Optional entry '" << keyword
<< "' not found. Default '" << deflt << "' ignored" << nl
<< exit(FatalIOError);
}
else
{ {
IOInfoInFunction(*this) IOInfoInFunction(*this)
<< "Optional entry '" << keyword << "Optional entry '" << keyword
<< "' not found, adding default value '" << deflt << "'" << "' not found. Adding default '" << deflt << "'" << nl;
<< 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.good())
{
T val;
ITstream& is = finder.ptr()->stream();
is >> val;
checkITstream(is, keyword);
if (!pred(val))
{
raiseBadInput(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. Using default '" << deflt << "'" << nl;
}
}
return deflt;
}
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.good())
{
T val;
ITstream& is = finder.ptr()->stream();
is >> val;
checkITstream(is, keyword);
if (!pred(val))
{
raiseBadInput(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)); add(new primitiveEntry(keyword, deflt));
@ -189,7 +303,7 @@ bool Foam::dictionary::readEntry
{ {
const const_searcher finder(csearch(keyword, matchOpt)); const const_searcher finder(csearch(keyword, matchOpt));
if (finder.found()) if (finder.good())
{ {
ITstream& is = finder.ptr()->stream(); ITstream& is = finder.ptr()->stream();
is >> val; is >> val;
@ -202,7 +316,78 @@ bool Foam::dictionary::readEntry
{ {
FatalIOErrorInFunction(*this) FatalIOErrorInFunction(*this)
<< "Entry '" << keyword << "' not found in dictionary " << "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); << 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> template<class T>
T Foam::dictionary::lookupOrDefaultCompat T Foam::dictionary::getOrDefaultCompat
( (
const word& keyword, const word& keyword,
std::initializer_list<std::pair<const char*,int>> compat, 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)); const const_searcher finder(csearchCompat(keyword, compat, matchOpt));
if (finder.found()) if (finder.good())
{ {
T val; T val;
@ -246,11 +445,20 @@ T Foam::dictionary::lookupOrDefaultCompat
return val; return val;
} }
else if (writeOptionalEntries) else if (writeOptionalEntries)
{
if (writeOptionalEntries > 1)
{
FatalIOErrorInFunction(*this)
<< "Optional entry '" << keyword
<< "' not found. Default '" << deflt << "' ignored" << nl
<< exit(FatalIOError);
}
else
{ {
IOInfoInFunction(*this) IOInfoInFunction(*this)
<< "Optional entry '" << keyword << "' not found," << "Optional entry '" << keyword
<< " using default value '" << deflt << "'" << "' not found. Using default '" << deflt << "'" << nl;
<< nl; }
} }
return deflt; return deflt;

View File

@ -2,7 +2,7 @@
========= | ========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | \\ / O peration |
\\ / A nd | \\ / A nd | Copyright (C) 2017-2019 OpenCFD Ltd.
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
| Copyright (C) 2011-2015 OpenFOAM Foundation | Copyright (C) 2011-2015 OpenFOAM Foundation
@ -73,6 +73,25 @@ Foam::autoPtr<Foam::entry> Foam::entry::clone() const
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * 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 void Foam::entry::checkITstream(const ITstream& is) const
{ {
const word& keyword = keyword_; const word& keyword = keyword_;

View File

@ -2,7 +2,7 @@
========= | ========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | \\ / O peration |
\\ / A nd | Copyright (C) 2017-2018 OpenCFD Ltd. \\ / A nd | Copyright (C) 2017-2019 OpenCFD Ltd.
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
| Copyright (C) 2011-2016 OpenFOAM Foundation | Copyright (C) 2011-2016 OpenFOAM Foundation
@ -109,6 +109,9 @@ private:
// \return True if it is a valid keyType. // \return True if it is a valid keyType.
static bool getKeyword(keyType& keyword, Istream& is); static bool getKeyword(keyType& keyword, Istream& is);
//- Emit IOError about bad input for the entry
void raiseBadInput(const ITstream& is) const;
public: public:
@ -272,6 +275,37 @@ public:
checkITstream(is); 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
//- Write //- Write

View File

@ -2,7 +2,7 @@
========= | ========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | \\ / O peration |
\\ / A nd | Copyright (C) 2018 OpenCFD Ltd. \\ / A nd | Copyright (C) 2018-2019 OpenCFD Ltd.
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
| Copyright (C) 2011-2016 OpenFOAM Foundation | Copyright (C) 2011-2016 OpenFOAM Foundation
@ -246,7 +246,7 @@ bool Foam::entry::New
const auto finder = const auto finder =
parentDict.csearchScoped(varName, keyType::REGEX_RECURSIVE); parentDict.csearchScoped(varName, keyType::REGEX_RECURSIVE);
if (finder.found()) if (finder.good())
{ {
// Read as primitiveEntry // Read as primitiveEntry
const keyType newKeyword(finder.ptr()->stream()); const keyType newKeyword(finder.ptr()->stream());
@ -311,7 +311,7 @@ bool Foam::entry::New
// How to manage duplicate entries // How to manage duplicate entries
bool mergeEntry = false; bool mergeEntry = false;
if (finder.found()) if (finder.good())
{ {
// Use keyword from the found entry (ie, eliminate scoping chars) // Use keyword from the found entry (ie, eliminate scoping chars)
const keyType key = finder.ref().keyword(); const keyType key = finder.ref().keyword();

View File

@ -65,7 +65,7 @@ bool Foam::functionEntries::removeEntry::execute
// Remove scoped keyword, or keyword in the local scope // Remove scoped keyword, or keyword in the local scope
auto finder(parentDict.searchScoped(key, keyType::LITERAL)); auto finder(parentDict.searchScoped(key, keyType::LITERAL));
if (finder.found()) if (finder.good())
{ {
finder.context().remove(finder.ptr()->keyword()); finder.context().remove(finder.ptr()->keyword());
} }

View File

@ -274,38 +274,38 @@ Foam::dimensioned<Type>::dimensioned
// * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * // // * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
template<class Type> template<class Type>
Foam::dimensioned<Type> Foam::dimensioned<Type>::lookupOrDefault Foam::dimensioned<Type> Foam::dimensioned<Type>::getOrDefault
( (
const word& name, const word& name,
const dictionary& dict, const dictionary& dict,
const dimensionSet& dims, const dimensionSet& dims,
const Type& defaultValue const Type& deflt
) )
{ {
// checkDims = true // checkDims = true
return dimensioned<Type>(name, dims, defaultValue, dict); return dimensioned<Type>(name, dims, deflt, dict);
} }
template<class Type> template<class Type>
Foam::dimensioned<Type> Foam::dimensioned<Type>::lookupOrDefault Foam::dimensioned<Type> Foam::dimensioned<Type>::getOrDefault
( (
const word& name, const word& name,
const dictionary& dict, 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> template<class Type>
Foam::dimensioned<Type> Foam::dimensioned<Type>::lookupOrAddToDict Foam::dimensioned<Type> Foam::dimensioned<Type>::getOrAddToDict
( (
const word& name, const word& name,
dictionary& dict, dictionary& dict,
const dimensionSet& dims, const dimensionSet& dims,
const Type& defaultValue const Type& deflt
) )
{ {
if (dict.found(name)) if (dict.found(name))
@ -313,20 +313,20 @@ Foam::dimensioned<Type> Foam::dimensioned<Type>::lookupOrAddToDict
return dimensioned<Type>(name, dims, dict); return dimensioned<Type>(name, dims, dict);
} }
(void) dict.add(name, defaultValue); (void) dict.add(name, deflt);
return dimensioned<Type>(name, dims, defaultValue); return dimensioned<Type>(name, dims, deflt);
} }
template<class Type> template<class Type>
Foam::dimensioned<Type> Foam::dimensioned<Type>::lookupOrAddToDict Foam::dimensioned<Type> Foam::dimensioned<Type>::getOrAddToDict
( (
const word& name, const word& name,
dictionary& dict, dictionary& dict,
const Type& defaultValue const Type& deflt
) )
{ {
return lookupOrAddToDict(name, dict, dimless, defaultValue); return getOrAddToDict(name, dict, dimless, deflt);
} }

View File

@ -207,42 +207,42 @@ public:
//- Construct dimensioned from dictionary, with default value. //- Construct dimensioned from dictionary, with default value.
//- FatalIOError if there are excess tokens. //- FatalIOError if there are excess tokens.
static dimensioned<Type> lookupOrDefault static dimensioned<Type> getOrDefault
( (
const word& name, const word& name,
const dictionary& dict, const dictionary& dict,
const dimensionSet& dims = dimless, const dimensionSet& dims = dimless,
const Type& defaultValue = Type(Zero) const Type& deflt = Type(Zero)
); );
//- Construct dimensionless from dictionary, with default value. //- Construct dimensionless from dictionary, with default value.
// FatalIOError if it is found and there are excess tokens. // FatalIOError if it is found and there are excess tokens.
static dimensioned<Type> lookupOrDefault static dimensioned<Type> getOrDefault
( (
const word& name, const word& name,
const dictionary& dict, const dictionary& dict,
const Type& defaultValue = Type(Zero) const Type& deflt = Type(Zero)
); );
//- Construct dimensioned from dictionary, with default value. //- Construct dimensioned from dictionary, with default value.
// If the value is not found, it is added into the dictionary. // If the value is not found, it is added into the dictionary.
// FatalIOError if it is found and there are excess tokens. // FatalIOError if it is found and there are excess tokens.
static dimensioned<Type> lookupOrAddToDict static dimensioned<Type> getOrAddToDict
( (
const word& name, const word& name,
dictionary& dict, dictionary& dict,
const dimensionSet& dims = dimless, const dimensionSet& dims = dimless,
const Type& defaultValue = Type(Zero) const Type& deflt = Type(Zero)
); );
//- Construct dimensionless from dictionary, with default value. //- Construct dimensionless from dictionary, with default value.
// If the value is not found, it is added into the dictionary. // If the value is not found, it is added into the dictionary.
// FatalIOError if it is found and there are excess tokens. // FatalIOError if it is found and there are excess tokens.
static dimensioned<Type> lookupOrAddToDict static dimensioned<Type> getOrAddToDict
( (
const word& name, const word& name,
dictionary& dict, 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). // dictionary instead (additional checks on the input stream).
dimensioned(const word& name, const dimensionSet& dims, Istream& is) dimensioned(const word& name, const dimensionSet& dims, Istream& is)
FOAM_DEPRECATED(2018-11); 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);
}
}; };

View File

@ -1168,7 +1168,7 @@ void Foam::argList::parse
decompDict.readEntry("numberOfSubdomains", dictNProcs); decompDict.readEntry("numberOfSubdomains", dictNProcs);
if (decompDict.lookupOrDefault("distributed", false)) if (decompDict.getOrDefault("distributed", false))
{ {
parRunControl_.distributed(true); parRunControl_.distributed(true);
decompDict.readEntry("roots", roots); decompDict.readEntry("roots", roots);

View File

@ -124,7 +124,7 @@ namespace Foam
class argList class argList
{ {
// Private data // Private Data
//- Track if command arguments are mandatory/optional //- Track if command arguments are mandatory/optional
static bool argsMandatory_; static bool argsMandatory_;
@ -383,7 +383,7 @@ public:
inline T opt(const word& optName) const; inline T opt(const word& optName) const;
//- Get a value from the named option if present, or return default. //- Get a value from the named option if present, or return default.
// Identical to lookupOrDefault(). // Identical to getOrDefault().
template<class T> template<class T>
inline T opt(const word& optName, const T& deflt) const; 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. //- Get a value from the named option if present, or return default.
template<class T> template<class T>
inline T lookupOrDefault inline T getOrDefault
( (
const word& optName, const word& optName,
const T& deflt const T& deflt
@ -590,6 +590,16 @@ public:
inline const string& operator[](const word& optName) const; 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) // Older style access (including 1712 release)
#ifdef Foam_argList_1712 #ifdef Foam_argList_1712
@ -598,7 +608,8 @@ public:
// Index 1 is the first (non-option) argument. // Index 1 is the first (non-option) argument.
// \deprecated(2018-08) - use get() method // \deprecated(2018-08) - use get() method
template<class T> 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); return this->get<T>(index);
} }
@ -607,21 +618,24 @@ public:
// Index 1 is the first (non-option) argument. // Index 1 is the first (non-option) argument.
// \deprecated(2018-01) - use get() method // \deprecated(2018-01) - use get() method
template<class T> 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); return this->get<T>(index);
} }
//- Deprecated(2018-01) return true if the named option is found //- Deprecated(2018-01) return true if the named option is found
// \deprecated(2018-01) - use found() method // \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); return found(optName);
} }
//- Deprecated(2018-01) return an input stream from the named option //- Deprecated(2018-01) return an input stream from the named option
// \deprecated(2018-01) - use lookup() method // \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); return lookup(optName);
} }
@ -629,7 +643,8 @@ public:
//- Deprecated(2018-01) read a value from the named option //- Deprecated(2018-01) read a value from the named option
// \deprecated(2018-01) - use opt() method // \deprecated(2018-01) - use opt() method
template<class T> 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); return opt<T>(optName);
} }
@ -638,7 +653,8 @@ public:
// Return true if the named option was found. // Return true if the named option was found.
// \deprecated(2018-01) - use readIfPresent() method // \deprecated(2018-01) - use readIfPresent() method
template<class T> template<class T>
inline bool optionReadIfPresent bool FOAM_DEPRECATED_FOR(2018-01, "readIfPresent() method")
optionReadIfPresent
( (
const word& optName, const word& optName,
T& val T& val
@ -652,7 +668,8 @@ public:
// use the supplied default and return false. // use the supplied default and return false.
// \deprecated(2018-01) - use readIfPresent() method // \deprecated(2018-01) - use readIfPresent() method
template<class T> template<class T>
inline bool optionReadIfPresent bool FOAM_DEPRECATED_FOR(2018-01, "readIfPresent() method")
optionReadIfPresent
( (
const word& optName, const word& optName,
T& val, T& val,
@ -664,21 +681,23 @@ public:
//- Deprecated(2018-01) read a value from the named option if present. //- Deprecated(2018-01) read a value from the named option if present.
// Return supplied default otherwise. // Return supplied default otherwise.
// \deprecated(2018-01) - use lookupOrDefault() method // \deprecated(2018-01) - use getOrDefault() method
template<class T> template<class T>
inline T optionLookupOrDefault T FOAM_DEPRECATED_FOR(2018-01, "getOrDefault() method")
optionLookupOrDefault
( (
const word& optName, const word& optName,
const T& deflt const T& deflt
) const ) 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) read a List of values from the named option
// \deprecated(2018-01) - use getList() method // \deprecated(2018-01) - use getList() method
template<class T> 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); return this->getList<T>(optName);
} }

View File

@ -2,7 +2,7 @@
========= | ========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | \\ / O peration |
\\ / A nd | Copyright (C) 2017-2018 OpenCFD Ltd. \\ / A nd | Copyright (C) 2017-2019 OpenCFD Ltd.
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
| Copyright (C) 2011-2013 OpenFOAM Foundation | Copyright (C) 2011-2013 OpenFOAM Foundation
@ -316,7 +316,7 @@ inline bool Foam::argList::readIfPresent
template<class T> template<class T>
inline T Foam::argList::lookupOrDefault inline T Foam::argList::getOrDefault
( (
const word& optName, const word& optName,
const T& deflt const T& deflt

View File

@ -202,8 +202,8 @@ public:
//- Legacy name for areaNormal() //- Legacy name for areaNormal()
// \deprecated(2018-06) Deprecated for new use // \deprecated(2018-06) Deprecated for new use
inline vector normal(const UList<point>& p) const vector FOAM_DEPRECATED_FOR(2018-12, "areaNormal() or unitNormal()")
FOAM_DEPRECATED_FOR(2018-12, "areaNormal() or unitNormal()") normal(const UList<point>& p) const
{ {
return areaNormal(p); // Legacy definition return areaNormal(p); // Legacy definition
} }

View File

@ -133,8 +133,8 @@ public:
//- Legacy name for areaNormal() //- Legacy name for areaNormal()
// \deprecated(2018-06) Deprecated for new use // \deprecated(2018-06) Deprecated for new use
inline vector normal(const UList<point>& points) const vector FOAM_DEPRECATED_FOR(2018-12, "areaNormal() or unitNormal()")
FOAM_DEPRECATED_FOR(2018-12, "areaNormal() or unitNormal()") normal(const UList<point>& points) const
{ {
return areaNormal(points); // Legacy definition return areaNormal(points); // Legacy definition
} }

View File

@ -216,8 +216,8 @@ public:
//- Legacy name for areaNormal(). //- Legacy name for areaNormal().
// \deprecated(2018-06) Deprecated for new use // \deprecated(2018-06) Deprecated for new use
inline vector normal() const vector FOAM_DEPRECATED_FOR(2018-12, "areaNormal() or unitNormal()")
FOAM_DEPRECATED_FOR(2018-12, "areaNormal() or unitNormal()") normal() const
{ {
return areaNormal(); return areaNormal();
} }

View File

@ -110,14 +110,14 @@ Foam::Switch::switchType Foam::Switch::parse
} }
Foam::Switch Foam::Switch::lookupOrAddToDict Foam::Switch Foam::Switch::getOrAddToDict
( (
const word& name, const word& name,
dictionary& dict, 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 word& key,
const dictionary& dict, const dictionary& dict,
const Switch defaultValue const Switch deflt
) )
: :
Switch(defaultValue) Switch(deflt)
{ {
const entry* eptr = dict.findEntry(key, keyType::LITERAL); const entry* eptr = dict.findEntry(key, keyType::LITERAL);

View File

@ -169,7 +169,7 @@ public:
( (
const word& key, //!< Lookup key. Uses LITERAL (not REGEX) const word& key, //!< Lookup key. Uses LITERAL (not REGEX)
const dictionary& dict, //!< dictionary const dictionary& dict, //!< dictionary
const Switch defaultValue //!< fallback if not found const Switch deflt //!< fallback if not found
); );
//- Construct from Istream //- Construct from Istream
@ -180,11 +180,11 @@ public:
//- Construct from dictionary, supplying default value so that if the //- Construct from dictionary, supplying default value so that if the
//- value is not found, it is added into the dictionary. //- value is not found, it is added into the dictionary.
static Switch lookupOrAddToDict static Switch getOrAddToDict
( (
const word& name, //!< Lookup key. Uses REGEX! const word& name, //!< Lookup key. Uses REGEX!
dictionary& dict, //!< dictionary 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 // 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) text representation of the Switch value
// \deprecated(2018-03) - use c_str() method // \deprecated(2018-03) - use c_str() method
inline const char* asText() const { return c_str(); }; inline const char* asText() const { return c_str(); };

View File

@ -81,14 +81,14 @@ template<class EnumType>
EnumType Foam::Enum<EnumType>::get EnumType Foam::Enum<EnumType>::get
( (
const word& enumName, const word& enumName,
const EnumType defaultValue const EnumType deflt
) const ) const
{ {
const label idx = find(enumName); const label idx = find(enumName);
if (idx < 0) if (idx < 0)
{ {
return defaultValue; return deflt;
} }
return EnumType(vals_[idx]); return EnumType(vals_[idx]);
@ -136,11 +136,11 @@ EnumType Foam::Enum<EnumType>::get
template<class EnumType> template<class EnumType>
EnumType Foam::Enum<EnumType>::lookupOrDefault EnumType Foam::Enum<EnumType>::getOrDefault
( (
const word& key, const word& key,
const dictionary& dict, const dictionary& dict,
const EnumType defaultValue, const EnumType deflt,
const bool failsafe const bool failsafe
) const ) const
{ {
@ -163,8 +163,8 @@ EnumType Foam::Enum<EnumType>::lookupOrDefault
{ {
IOWarningInFunction(dict) IOWarningInFunction(dict)
<< enumName << " is not in enumeration: " << *this << nl << enumName << " is not in enumeration: " << *this << nl
<< "using failsafe " << get(defaultValue) << "using failsafe " << get(deflt)
<< " (value " << int(defaultValue) << ")" << endl; << " (value " << int(deflt) << ")" << endl;
} }
else else
{ {
@ -174,7 +174,7 @@ EnumType Foam::Enum<EnumType>::lookupOrDefault
} }
} }
return defaultValue; return deflt;
} }

View File

@ -132,7 +132,7 @@ public:
//- The enumeration corresponding to the given name. //- The enumeration corresponding to the given name.
// \return The enumeration or default if not found. // \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. //- The name corresponding to the given enumeration.
// Return an empty word if not found. // Return an empty word if not found.
@ -155,11 +155,11 @@ public:
// //
// \return The value found or default if not found in dictionary. // \return The value found or default if not found in dictionary.
// FatalError (or Warning) if the enumerated name was incorrect. // FatalError (or Warning) if the enumerated name was incorrect.
EnumType lookupOrDefault EnumType getOrDefault
( (
const word& key, //!< Lookup key. Uses LITERAL (not REGEX) const word& key, //!< Lookup key. Uses LITERAL (not REGEX)
const dictionary& dict, //!< dictionary 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 bool failsafe = false //!< Warn only on bad enumeration
) const; ) const;
@ -216,33 +216,51 @@ public:
inline const word& operator[](const EnumType e) const; inline const word& operator[](const EnumType e) const;
//- Return the enumeration corresponding to the given name or //- 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() inline EnumType operator()
( (
const word& enumName, const word& enumName,
const EnumType defaultValue const EnumType deflt
) const; ) const;
// Housekeeping // 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) same as two-parameter get()
// \deprecated(2018-10) - use two-parameter get() method // \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); return get(key, dict);
} }
//- Deprecated(2018-10) lookupOrDefault with warnings instead of error. //- Deprecated(2018-10) lookupOrDefault with warnings instead of error.
// \deprecated(2018-10) - use lookupOrDefault() with failsafe option // \deprecated(2018-10) - use getOrDefault() with failsafe option
EnumType lookupOrFailsafe EnumType FOAM_DEPRECATED_FOR(2018-10, "getOrDefault() method")
lookupOrFailsafe
( (
const word& key, const word& key,
const dictionary& dict, const dictionary& dict,
const EnumType defaultValue const EnumType deflt
) const ) const
{ {
return lookupOrDefault(key, dict, defaultValue, true); return getOrDefault(key, dict, deflt, true);
} }
}; };

View File

@ -27,9 +27,9 @@ InNamespace
Foam Foam
Description 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 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 // Weighting operations
#define WeightedOp(opName, op) \ #define WeightedOp(opName, op) \
@ -216,10 +235,18 @@ BoolOp(lessEq, x <= y)
BoolOp(greater, x > y) BoolOp(greater, x > y)
BoolOp(greaterEq, 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)) WeightedOp(multiply, (weight*y))
#undef Op #undef Op
#undef BoolOp #undef BoolOp
#undef Bool1Op
#undef WeightedOp #undef WeightedOp
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

View File

@ -161,6 +161,12 @@ public:
// Static Member Functions // 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 //- A 0-1 range corresponding to the pTraits zero, one
inline static MinMax<T> zero_one(); inline static MinMax<T> zero_one();

View File

@ -25,6 +25,20 @@ License
// * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * // // * * * * * * * * * * * * * 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> template<class T>
inline Foam::MinMax<T> Foam::MinMax<T>::zero_one() inline Foam::MinMax<T> Foam::MinMax<T>::zero_one()
{ {

View File

@ -36,8 +36,8 @@ const Foam::scalarRange Foam::scalarRange::null;
const Foam::scalarRange Foam::scalarRange::always const Foam::scalarRange Foam::scalarRange::always
( (
scalarRange::ALWAYS, scalarRange::ALWAYS,
-Foam::GREAT, -GREAT,
Foam::GREAT GREAT
); );
@ -158,21 +158,21 @@ Foam::Ostream& Foam::operator<<(Ostream& os, const scalarRange& range)
switch (range.type_) switch (range.type_)
{ {
case scalarRange::EQ: case scalarRange::EQ:
os << range.min_; os << range.min();
break; break;
case scalarRange::GE: case scalarRange::GE:
case scalarRange::GT: case scalarRange::GT:
os << range.min_ << ":Inf"; os << range.min() << ":Inf";
break; break;
case scalarRange::LE: case scalarRange::LE:
case scalarRange::LT: case scalarRange::LT:
os << "-Inf:" << range.max_; os << "-Inf:" << range.max();
break; break;
case scalarRange::GE_LE: case scalarRange::GE_LE:
os << range.min_ << ':' << range.max_; os << range.min() << ':' << range.max();
break; break;
case scalarRange::ALWAYS: case scalarRange::ALWAYS:

View File

@ -34,6 +34,7 @@ Description
used to define an empty (inverse) range. used to define an empty (inverse) range.
SeeAlso SeeAlso
Foam::MinMax
Foam::predicates::scalars Foam::predicates::scalars
SourceFiles SourceFiles
@ -154,18 +155,27 @@ public:
static scalarRange parse(const std::string& str); 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; inline static scalarRange ge(const scalar minVal) noexcept;
//- Construct a greater-than bound //- Construct a greater-than bound
inline static scalarRange gt(const scalar minVal) noexcept; 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; inline static scalarRange le(const scalar maxVal) noexcept;
//- Construct a less-than bound //- Construct a less-than bound
inline static scalarRange lt(const scalar maxVal) noexcept; 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 // Member Functions
@ -192,13 +202,13 @@ public:
inline scalar value() const; inline scalar value() const;
//- True if the value matches the condition. //- True if the value matches the condition.
inline bool match(const scalar& value) const; inline bool match(const scalar& val) const;
// Member Operators // Member Operators
//- Identical to match(), for use as a predicate. //- 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;
inline bool operator!=(const scalarRange& rhs) const noexcept; inline bool operator!=(const scalarRange& rhs) const noexcept;

View File

@ -40,7 +40,7 @@ inline Foam::scalarRange::scalarRange
inline Foam::scalarRange::scalarRange() noexcept 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 clear(); // Inverted - explicitly mark as such
} }
else if (minVal == maxVal) else if (equal(minVal, maxVal))
{ {
type_ = EQ; type_ = EQ;
} }
@ -71,24 +71,42 @@ inline Foam::scalarRange::scalarRange
inline Foam::scalarRange Foam::scalarRange::ge(const scalar minVal) noexcept 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 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 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 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 inline void Foam::scalarRange::clear() noexcept
{ {
min_ = Foam::GREAT; min_ = GREAT;
max_ = -Foam::GREAT; max_ = -GREAT;
type_ = scalarRange::NONE; type_ = scalarRange::NONE;
} }
@ -136,16 +154,16 @@ inline Foam::scalar Foam::scalarRange::value() const
{ {
switch (type_) switch (type_)
{ {
case EQ: // For equals, min and max are identical case scalarRange::EQ: // For equals, min and max are identical
case GE: case scalarRange::GE:
case GT: case scalarRange::GT:
return min_; return min_;
case LE: case scalarRange::LE:
case LT: case scalarRange::LT:
return max_; return max_;
case GE_LE: case scalarRange::GE_LE:
// Multiply before adding to avoid possible overflow // Multiply before adding to avoid possible overflow
return (0.5 * min_) + (0.5 * max_); 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_) switch (type_)
{ {
case EQ: return equal(value, min_); case EQ: return equal(val, min_);
case GE: return (value >= min_); case GE: return (val >= min_);
case GT: return (value > min_); case GT: return (val > min_);
case LE: return (value <= max_); case LE: return (val <= max_);
case LT: return (value < max_); case LT: return (val < max_);
case GE_LE: return (value >= min_ && value <= max_); case GE_LE: return (val >= min_ && val <= max_);
case ALWAYS: return true; case ALWAYS: return true;
default: return false; default: return false;
} }
@ -173,9 +191,9 @@ inline bool Foam::scalarRange::match(const scalar& value) const
// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * 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);
} }

View File

@ -67,7 +67,11 @@ public:
//- Construct by parsing string for scalar ranges //- Construct by parsing string for scalar ranges
// The individual items are space, comma or semicolon delimited. // The individual items are space, comma or semicolon delimited.
// Optionally report when any range failed to parse // 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 // Static Constructors

View File

@ -182,10 +182,10 @@ public:
//- Deprecated(2019-01) Is the specified name found in the list? //- Deprecated(2019-01) Is the specified name found in the list?
// \deprecated(2019-01) - use found() method // \deprecated(2019-01) - use found() method
inline bool contains(const word& name) const bool FOAM_DEPRECATED_FOR(2019-01, "found() method")
FOAM_DEPRECATED_FOR(2019-01, "found() method") contains(const word& name) const
{ {
return this-found(name); return this->found(name);
} }
}; };

View File

@ -108,7 +108,7 @@ static std::string error_string(const std::regex_error& err)
bool Foam::regExpCxx::set(const char* pattern, bool ignoreCase) bool Foam::regExpCxx::set(const char* pattern, bool ignoreCase)
{ {
clear(); clear(); // Also sets ok_ = false
size_t len = (pattern ? strlen(pattern) : 0); size_t len = (pattern ? strlen(pattern) : 0);
@ -139,7 +139,7 @@ bool Foam::regExpCxx::set(const char* pattern, bool ignoreCase)
try try
{ {
re_.assign(pat, flags); re_.assign(pat, flags);
return true; ok_ = true;
} }
catch (const std::regex_error& err) 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) bool Foam::regExpCxx::set(const std::string& pattern, bool ignoreCase)
{ {
clear(); clear(); // Also sets ok_ = false
auto len = pattern.size(); auto len = pattern.size();
@ -188,7 +188,7 @@ bool Foam::regExpCxx::set(const std::string& pattern, bool ignoreCase)
try try
{ {
re_.assign(pat, pattern.end(), flags); re_.assign(pat, pattern.end(), flags);
return true; ok_ = true;
} }
catch (const std::regex_error& err) catch (const std::regex_error& err)
{ {
@ -200,7 +200,7 @@ bool Foam::regExpCxx::set(const std::string& pattern, bool ignoreCase)
} }
} }
return false; return ok_;
} }

View File

@ -41,7 +41,7 @@ Description
Note Note
The C++11 regular expressions may be broken on some compilers. The C++11 regular expressions may be broken on some compilers.
For example, gcc 4.8 is known to fail. 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 SourceFiles
regExpCxxI.H regExpCxxI.H
@ -66,7 +66,7 @@ namespace Foam
class regExpCxx class regExpCxx
{ {
// Private data // Private Data
//- Regular expression (using char type) //- Regular expression (using char type)
std::regex re_; std::regex re_;
@ -132,7 +132,7 @@ public:
~regExpCxx() = default; ~regExpCxx() = default;
// Member functions // Member Functions
// Access // Access

View File

@ -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; std::smatch mat;
if (!text.empty() && std::regex_search(text, mat, re_)) if (!text.empty() && std::regex_search(text, mat, re_))