ENH: use readOption to fine-tune dictionary reading

- previously had 'mandatory' (bool) for advanced control of reading
  dictionary entries but its meaning was unclear in the calling code
  without extra code comments.

  Now use IOobjectOption::readOption instead, which allows further
  options (ie, NO_READ) and is more transparent as to its purpose in
  the code than a true/false bool flag was.

  This is a minor breaking change (infrequent, advanced usage only)

- minor code cleanup in dictionary lookup methods
This commit is contained in:
Mark Olesen
2022-09-30 15:43:01 +02:00
parent d938e01d7a
commit 7eda6de6f4
21 changed files with 279 additions and 222 deletions

View File

@ -368,9 +368,9 @@ const Foam::entry& Foam::dictionary::lookupEntry
enum keyType::option matchOpt enum keyType::option matchOpt
) const ) const
{ {
const const_searcher finder(csearch(keyword, matchOpt)); const entry* eptr = findEntry(keyword, matchOpt);
if (!finder.good()) if (!eptr)
{ {
FatalIOErrorInFunction(*this) FatalIOErrorInFunction(*this)
<< "Entry '" << keyword << "' not found in dictionary " << "Entry '" << keyword << "' not found in dictionary "
@ -378,7 +378,7 @@ const Foam::entry& Foam::dictionary::lookupEntry
<< exit(FatalIOError); << exit(FatalIOError);
} }
return finder.ref(); return *eptr;
} }
@ -504,12 +504,12 @@ Foam::dictionary& Foam::dictionary::subDictOrAdd
{ {
searcher finder(search(keyword, matchOpt)); searcher finder(search(keyword, matchOpt));
dictionary* ptr = finder.dictPtr(); dictionary* dictPtr = finder.dictPtr();
if (ptr) if (dictPtr)
{ {
// Found and a sub-dictionary // Found and a sub-dictionary
return *ptr; return *dictPtr;
} }
if (finder.good()) if (finder.good())
@ -521,9 +521,9 @@ Foam::dictionary& Foam::dictionary::subDictOrAdd
<< exit(FatalIOError); << exit(FatalIOError);
} }
ptr = this->set(keyword, dictionary())->dictPtr(); dictPtr = this->set(keyword, dictionary())->dictPtr();
if (!ptr) if (!dictPtr)
{ {
FatalIOErrorInFunction(*this) FatalIOErrorInFunction(*this)
<< "Failed to insert sub-dictionary '" << keyword << "Failed to insert sub-dictionary '" << keyword
@ -532,7 +532,7 @@ Foam::dictionary& Foam::dictionary::subDictOrAdd
<< exit(FatalIOError); << exit(FatalIOError);
} }
return *ptr; return *dictPtr;
} }
@ -545,10 +545,12 @@ Foam::dictionary Foam::dictionary::subOrEmptyDict
{ {
const const_searcher finder(csearch(keyword, matchOpt)); const const_searcher finder(csearch(keyword, matchOpt));
if (finder.isDict()) const dictionary* dictPtr = finder.dictPtr();
if (dictPtr)
{ {
// Found and a sub-dictionary // Found and a sub-dictionary
return finder.dict(); return *dictPtr;
} }
if (mandatory) if (mandatory)
@ -581,10 +583,12 @@ const Foam::dictionary& Foam::dictionary::optionalSubDict
{ {
const const_searcher finder(csearch(keyword, matchOpt)); const const_searcher finder(csearch(keyword, matchOpt));
if (finder.isDict()) const dictionary* dictPtr = finder.dictPtr();
if (dictPtr)
{ {
// Found and a sub-dictionary // Found and a sub-dictionary
return finder.dict(); return *dictPtr;
} }
if (finder.good()) if (finder.good())
@ -787,10 +791,12 @@ Foam::entry* Foam::dictionary::set(entry* entryPtr)
// Find non-recursive with patterns // Find non-recursive with patterns
searcher finder(search(entryPtr->keyword(), keyType::REGEX)); searcher finder(search(entryPtr->keyword(), keyType::REGEX));
dictionary* dictPtr = finder.dictPtr();
// Clear dictionary so merge acts like overwrite // Clear dictionary so merge acts like overwrite
if (finder.isDict()) if (dictPtr)
{ {
finder.dict().clear(); dictPtr->clear();
} }
return add(entryPtr, true); return add(entryPtr, true);

View File

@ -98,6 +98,7 @@ SeeAlso
#include "wordList.H" #include "wordList.H"
#include "className.H" #include "className.H"
#include "refPtr.H" #include "refPtr.H"
#include "IOobjectOption.H"
// Some common data types // Some common data types
#include "label.H" #include "label.H"
@ -175,14 +176,14 @@ public:
//- Construct for the given dictionary context. //- Construct for the given dictionary context.
// Allow implicit conversion // Allow implicit conversion
Searcher(dict_pointer dict) Searcher(dict_pointer dict) noexcept
: :
dict_(dict), dict_(dict),
eptr_(nullptr) eptr_(nullptr)
{} {}
//- Assign the entry //- Assign the entry
void set(pointer eptr) void set(pointer eptr) noexcept
{ {
eptr_ = eptr; eptr_ = eptr;
} }
@ -191,7 +192,7 @@ public:
public: public:
//- Default construct //- Default construct
Searcher() constexpr Searcher() noexcept
: :
dict_(nullptr), dict_(nullptr),
eptr_(nullptr) eptr_(nullptr)
@ -199,34 +200,19 @@ public:
//- True if entry was found //- True if entry was found
bool good() const noexcept bool good() const noexcept { return eptr_; }
{
return eptr_;
}
//- True if entry was found //- True if entry was found
bool found() const noexcept bool found() const noexcept { return eptr_; }
{
return eptr_;
}
//- The containing dictionary context //- The containing dictionary context
dict_reference context() const dict_reference context() const { return *dict_; }
{
return *dict_;
}
//- A pointer to the entry (nullptr if not found) //- A pointer to the entry (nullptr if not found)
pointer ptr() const noexcept pointer ptr() const noexcept { return eptr_; }
{
return eptr_;
}
//- A reference to the entry (Error if not found) //- A reference to the entry (Error if not found)
reference ref() const reference ref() const { return *eptr_; }
{
return *eptr_;
}
//- True if found entry is a dictionary. //- True if found entry is a dictionary.
bool isDict() const noexcept bool isDict() const noexcept
@ -507,26 +493,26 @@ public:
tokenList tokens() const; tokenList tokens() const;
// Search and lookup // Searching
//- Search for an entry (const access) with the given keyword. //- Find an entry (const access) with the given keyword.
// //
// \param keyword the keyword to search for // \param keyword the keyword to search for
// \param matchOpt search mode (default: non-recursive with patterns) // \param matchOpt search mode (default: non-recursive with patterns)
// //
// \return True if entry was found // \return pointer to the entry found or a nullptr.
inline bool found inline const entry* findEntry
( (
const word& keyword, const word& keyword,
enum keyType::option matchOpt = keyType::REGEX enum keyType::option matchOpt = keyType::REGEX
) const; ) const;
//- Find for an entry (non-const access) with the given keyword. //- Find an entry (non-const access) with the given keyword.
// //
// \param keyword the keyword to search for // \param keyword the keyword to search for
// \param matchOpt search mode (default: non-recursive with patterns) // \param matchOpt search mode (default: non-recursive with patterns)
// //
// \return the entry pointer found or a nullptr. // \return pointer to the entry found or a nullptr.
inline entry* findEntry inline entry* findEntry
( (
const word& keyword, const word& keyword,
@ -538,8 +524,8 @@ public:
// \param keyword the keyword to search for // \param keyword the keyword to search for
// \param matchOpt search mode (default: non-recursive with patterns) // \param matchOpt search mode (default: non-recursive with patterns)
// //
// \return the entry pointer found or a nullptr. // \return True if entry was found
inline const entry* findEntry inline bool found
( (
const word& keyword, const word& keyword,
enum keyType::option matchOpt = keyType::REGEX enum keyType::option matchOpt = keyType::REGEX
@ -553,7 +539,7 @@ public:
// \param keyword the keyword to search for // \param keyword the keyword to search for
// \param matchOpt search mode (default: non-recursive with patterns) // \param matchOpt search mode (default: non-recursive with patterns)
// //
// \return the entry pointer found or a nullptr. // \return pointer to the entry found or a nullptr.
inline const entry* findScoped inline const entry* findScoped
( (
const word& keyword, const word& keyword,
@ -561,7 +547,20 @@ public:
) const; ) const;
//- 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.
//
// \param keyword the keyword to search for
// \param matchOpt search mode (default: non-recursive with patterns)
//
// \return pointer to sub-dictionary found or a nullptr.
inline const dictionary* findDict
(
const word& keyword,
enum keyType::option matchOpt = keyType::REGEX
) const;
//- Find and return a sub-dictionary pointer if present
//- (and a sub-dictionary) otherwise return nullptr.
// //
// \param keyword the keyword to search for // \param keyword the keyword to search for
// \param matchOpt search mode (default: non-recursive with patterns) // \param matchOpt search mode (default: non-recursive with patterns)
@ -573,19 +572,21 @@ public:
enum keyType::option matchOpt = keyType::REGEX enum keyType::option matchOpt = keyType::REGEX
); );
//- Find and return a sub-dictionary pointer if present //- Find a sub-dictionary.
// (and a sub-dictionary) otherwise return nullptr.
// //
// \param keyword the keyword to search for // \param keyword the keyword to search for
// \param matchOpt search mode (default: non-recursive with patterns) // \param matchOpt search mode (default: non-recursive with patterns)
// //
// \return pointer to sub-dictionary found or a nullptr. // \return true if the sub-dictionary was found
inline const dictionary* findDict inline bool isDict
( (
const word& keyword, const word& keyword,
enum keyType::option matchOpt = keyType::REGEX enum keyType::option matchOpt = keyType::REGEX
) const; ) const;
// Lookup
//- Search for an entry (const access) with the given keyword. //- Search for an entry (const access) with the given keyword.
// //
// \param keyword the keyword to search for // \param keyword the keyword to search for
@ -659,16 +660,16 @@ public:
// \param keyword the keyword to search for // \param keyword the keyword to search for
// \param val the value to read into // \param val the value to read into
// \param matchOpt search mode (default: non-recursive with patterns) // \param matchOpt search mode (default: non-recursive with patterns)
// \param mandatory the keyword is mandatory (default: true) // \param readOpt the entry is required/optional (default: MUST_READ)
// //
// \return true if the entry was found. // \return true if the entry was read
template<class T> template<class T>
bool readEntry bool readEntry
( (
const word& keyword, const word& keyword,
T& val, T& val,
enum keyType::option matchOpt = keyType::REGEX, enum keyType::option matchOpt = keyType::REGEX,
bool mandatory = true IOobjectOption::readOption readOpt = IOobjectOption::MUST_READ
) const; ) const;
//- Find an entry if present, and assign to T val. //- Find an entry if present, and assign to T val.
@ -678,7 +679,7 @@ public:
// \param val the value to read into // \param val the value to read into
// \param matchOpt search mode (default: non-recursive with patterns) // \param matchOpt search mode (default: non-recursive with patterns)
// //
// \return true if the entry was found. // \return true if the entry was read
template<class T> template<class T>
bool readIfPresent bool readIfPresent
( (
@ -742,9 +743,9 @@ public:
// \param val the value to read into // \param val the value to read into
// \param pred the value check predicate // \param pred the value check predicate
// \param matchOpt search mode (default: non-recursive with patterns) // \param matchOpt search mode (default: non-recursive with patterns)
// \param mandatory the keyword is mandatory (default: true) // \param readOpt the entry is required/optional (default: MUST_READ)
// //
// \return true if the entry was found. // \return true if the entry was read
template<class T, class Predicate> template<class T, class Predicate>
bool readCheck bool readCheck
( (
@ -752,7 +753,7 @@ public:
T& val, T& val,
const Predicate& pred, const Predicate& pred,
enum keyType::option matchOpt = keyType::REGEX, enum keyType::option matchOpt = keyType::REGEX,
bool mandatory = true IOobjectOption::readOption readOpt = IOobjectOption::MUST_READ
) const; ) const;
//- Find an entry if present, and assign to T val. //- Find an entry if present, and assign to T val.
@ -763,7 +764,7 @@ public:
// \param pred the value check predicate // \param pred the value check predicate
// \param matchOpt search mode (default: non-recursive with patterns) // \param matchOpt search mode (default: non-recursive with patterns)
// //
// \return true if the entry was found. // \return true if the entry read
template<class T, class Predicate> template<class T, class Predicate>
bool readCheckIfPresent bool readCheckIfPresent
( (
@ -773,18 +774,6 @@ public:
enum keyType::option matchOpt = keyType::REGEX enum keyType::option matchOpt = keyType::REGEX
) const; ) const;
//- Check if entry is found and is a sub-dictionary.
//
// \param keyword the keyword to search for
// \param matchOpt search mode (default: non-recursive with patterns)
//
// \return true if the entry was found.
inline bool isDict
(
const word& keyword,
enum keyType::option matchOpt = keyType::REGEX
) const;
//- Find and return a sub-dictionary. //- Find and return a sub-dictionary.
// Fatal if the entry does not exist or is not a sub-dictionary. // Fatal if the entry does not exist or is not a sub-dictionary.
// //
@ -1120,19 +1109,6 @@ public:
enum keyType::option matchOpt = keyType::REGEX enum keyType::option matchOpt = keyType::REGEX
) const; ) const;
//- Search dictionary for given keyword and any compatibility names
//
// \param keyword the keyword to search for
// \param compat list of old compatibility keywords and the last
// OpenFOAM version for which they were used.
// \param matchOpt search mode (default: non-recursive with patterns)
bool foundCompat
(
const word& keyword,
std::initializer_list<std::pair<const char*,int>> compat,
enum keyType::option matchOpt = keyType::REGEX
) const;
//- Find and return an entry pointer if present, or return a nullptr, //- Find and return an entry pointer if present, or return a nullptr,
//- using any compatibility names if needed. //- using any compatibility names if needed.
// //
@ -1147,6 +1123,19 @@ public:
enum keyType::option matchOpt enum keyType::option matchOpt
) const; ) const;
//- Search dictionary for given keyword and any compatibility names
//
// \param keyword the keyword to search for
// \param compat list of old compatibility keywords and the last
// OpenFOAM version for which they were used.
// \param matchOpt search mode (default: non-recursive with patterns)
bool foundCompat
(
const word& keyword,
std::initializer_list<std::pair<const char*,int>> compat,
enum keyType::option matchOpt = keyType::REGEX
) const;
//- Find and return an entry if present, otherwise FatalIOError, //- Find and return an entry if present, otherwise FatalIOError,
//- using any compatibility names if needed. //- using any compatibility names if needed.
// //
@ -1217,9 +1206,9 @@ public:
// OpenFOAM version for which they were used. // OpenFOAM version for which they were used.
// \param val the value to read // \param val the value to read
// \param matchOpt search mode (default: non-recursive with patterns) // \param matchOpt search mode (default: non-recursive with patterns)
// \param mandatory the keyword is mandatory (default: true) // \param readOpt the entry is required/optional (default: MUST_READ)
// //
// \return true if the entry was found. // \return true if the entry was read
template<class T> template<class T>
bool readCompat bool readCompat
( (
@ -1227,7 +1216,7 @@ public:
std::initializer_list<std::pair<const char*,int>> compat, std::initializer_list<std::pair<const char*,int>> compat,
T& val, T& val,
enum keyType::option matchOpt = keyType::REGEX, enum keyType::option matchOpt = keyType::REGEX,
bool mandatory = true IOobjectOption::readOption readOpt = IOobjectOption::MUST_READ
) const; ) const;
//- Find an entry if present, and assign to T val //- Find an entry if present, and assign to T val

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com \\ / A nd | www.openfoam.com
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2017-2021 OpenCFD Ltd. Copyright (C) 2017-2022 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -71,17 +71,6 @@ Foam::dictionary::const_searcher Foam::dictionary::csearchCompat
} }
bool Foam::dictionary::foundCompat
(
const word& keyword,
std::initializer_list<std::pair<const char*,int>> compat,
enum keyType::option matchOpt
) const
{
return csearchCompat(keyword, compat, matchOpt).good();
}
const Foam::entry* Foam::dictionary::findCompat const Foam::entry* Foam::dictionary::findCompat
( (
const word& keyword, const word& keyword,
@ -93,6 +82,17 @@ const Foam::entry* Foam::dictionary::findCompat
} }
bool Foam::dictionary::foundCompat
(
const word& keyword,
std::initializer_list<std::pair<const char*,int>> compat,
enum keyType::option matchOpt
) const
{
return static_cast<bool>(findCompat(keyword, compat, matchOpt));
}
const Foam::entry& Foam::dictionary::lookupEntryCompat const Foam::entry& Foam::dictionary::lookupEntryCompat
( (
const word& keyword, const word& keyword,
@ -100,9 +100,9 @@ const Foam::entry& Foam::dictionary::lookupEntryCompat
enum keyType::option matchOpt enum keyType::option matchOpt
) const ) const
{ {
const const_searcher finder(csearchCompat(keyword, compat, matchOpt)); const entry* eptr = findCompat(keyword, compat, matchOpt);
if (!finder.good()) if (!eptr)
{ {
FatalIOErrorInFunction(*this) FatalIOErrorInFunction(*this)
<< "Entry '" << keyword << "' not found in dictionary " << "Entry '" << keyword << "' not found in dictionary "
@ -110,7 +110,7 @@ const Foam::entry& Foam::dictionary::lookupEntryCompat
<< exit(FatalIOError); << exit(FatalIOError);
} }
return finder.ref(); return *eptr;
} }

View File

@ -83,13 +83,13 @@ inline const Foam::dictionary& Foam::dictionary::parent() const noexcept
} }
inline bool Foam::dictionary::found inline const Foam::entry* Foam::dictionary::findEntry
( (
const word& keyword, const word& keyword,
enum keyType::option matchOpt enum keyType::option matchOpt
) const ) const
{ {
return csearch(keyword, matchOpt).good(); return csearch(keyword, matchOpt).ptr();
} }
@ -99,17 +99,17 @@ inline Foam::entry* Foam::dictionary::findEntry
enum keyType::option matchOpt enum keyType::option matchOpt
) )
{ {
return search(keyword, matchOpt).ptr(); return const_cast<entry*>(csearch(keyword, matchOpt).ptr());
} }
inline const Foam::entry* Foam::dictionary::findEntry inline bool Foam::dictionary::found
( (
const word& keyword, const word& keyword,
enum keyType::option matchOpt enum keyType::option matchOpt
) const ) const
{ {
return csearch(keyword, matchOpt).ptr(); return static_cast<bool>(findEntry(keyword, matchOpt));
} }
@ -123,16 +123,6 @@ inline const Foam::entry* Foam::dictionary::findScoped
} }
inline Foam::dictionary* Foam::dictionary::findDict
(
const word& keyword,
enum keyType::option matchOpt
)
{
return search(keyword, matchOpt).dictPtr();
}
inline const Foam::dictionary* Foam::dictionary::findDict inline const Foam::dictionary* Foam::dictionary::findDict
( (
const word& keyword, const word& keyword,
@ -143,13 +133,23 @@ inline const Foam::dictionary* Foam::dictionary::findDict
} }
inline Foam::dictionary* Foam::dictionary::findDict
(
const word& keyword,
enum keyType::option matchOpt
)
{
return const_cast<dictionary*>(csearch(keyword, matchOpt).dictPtr());
}
inline bool Foam::dictionary::isDict inline bool Foam::dictionary::isDict
( (
const word& keyword, const word& keyword,
enum keyType::option matchOpt enum keyType::option matchOpt
) const ) const
{ {
return csearch(keyword, matchOpt).isDict(); return static_cast<bool>(findDict(keyword, matchOpt));
} }

View File

@ -151,13 +151,13 @@ T Foam::dictionary::getOrDefault
enum keyType::option matchOpt enum keyType::option matchOpt
) const ) const
{ {
const const_searcher finder(csearch(keyword, matchOpt)); const entry* eptr = csearch(keyword, matchOpt).ptr();
if (finder.good()) if (eptr)
{ {
T val; T val;
ITstream& is = finder.ptr()->stream(); ITstream& is = eptr->stream();
is >> val; is >> val;
checkITstream(is, keyword); checkITstream(is, keyword);
@ -181,13 +181,13 @@ T Foam::dictionary::getOrAdd
enum keyType::option matchOpt enum keyType::option matchOpt
) )
{ {
const const_searcher finder(csearch(keyword, matchOpt)); const entry* eptr = csearch(keyword, matchOpt).ptr();
if (finder.good()) if (eptr)
{ {
T val; T val;
ITstream& is = finder.ptr()->stream(); ITstream& is = eptr->stream();
is >> val; is >> val;
checkITstream(is, keyword); checkITstream(is, keyword);
@ -223,13 +223,13 @@ T Foam::dictionary::getCheckOrDefault
} }
#endif #endif
const const_searcher finder(csearch(keyword, matchOpt)); const entry* eptr = csearch(keyword, matchOpt).ptr();
if (finder.good()) if (eptr)
{ {
T val; T val;
ITstream& is = finder.ptr()->stream(); ITstream& is = eptr->stream();
is >> val; is >> val;
checkITstream(is, keyword); checkITstream(is, keyword);
@ -269,13 +269,13 @@ T Foam::dictionary::getCheckOrAdd
} }
#endif #endif
const const_searcher finder(csearch(keyword, matchOpt)); const entry* eptr = csearch(keyword, matchOpt).ptr();
if (finder.good()) if (eptr)
{ {
T val; T val;
ITstream& is = finder.ptr()->stream(); ITstream& is = eptr->stream();
is >> val; is >> val;
checkITstream(is, keyword); checkITstream(is, keyword);
@ -303,21 +303,26 @@ bool Foam::dictionary::readEntry
const word& keyword, const word& keyword,
T& val, T& val,
enum keyType::option matchOpt, enum keyType::option matchOpt,
bool mandatory IOobjectOption::readOption readOpt
) const ) const
{ {
const const_searcher finder(csearch(keyword, matchOpt)); if (readOpt == IOobjectOption::NO_READ)
if (finder.good())
{ {
ITstream& is = finder.ptr()->stream(); return false;
}
const entry* eptr = csearch(keyword, matchOpt).ptr();
if (eptr)
{
ITstream& is = eptr->stream();
is >> val; is >> val;
checkITstream(is, keyword); checkITstream(is, keyword);
return true; return true;
} }
else if (mandatory) else if (IOobjectOption::isReadRequired(readOpt))
{ {
FatalIOErrorInFunction(*this) FatalIOErrorInFunction(*this)
<< "Entry '" << keyword << "' not found in dictionary " << "Entry '" << keyword << "' not found in dictionary "
@ -336,14 +341,19 @@ bool Foam::dictionary::readCheck
T& val, T& val,
const Predicate& pred, const Predicate& pred,
enum keyType::option matchOpt, enum keyType::option matchOpt,
bool mandatory IOobjectOption::readOption readOpt
) const ) const
{ {
const const_searcher finder(csearch(keyword, matchOpt)); if (readOpt == IOobjectOption::NO_READ)
if (finder.good())
{ {
ITstream& is = finder.ptr()->stream(); return false;
}
const entry* eptr = csearch(keyword, matchOpt).ptr();
if (eptr)
{
ITstream& is = eptr->stream();
is >> val; is >> val;
checkITstream(is, keyword); checkITstream(is, keyword);
@ -355,7 +365,7 @@ bool Foam::dictionary::readCheck
return true; return true;
} }
else if (mandatory) else if (IOobjectOption::isReadRequired(readOpt))
{ {
FatalIOErrorInFunction(*this) FatalIOErrorInFunction(*this)
<< "Entry '" << keyword << "' not found in dictionary " << "Entry '" << keyword << "' not found in dictionary "
@ -374,21 +384,26 @@ bool Foam::dictionary::readCompat
std::initializer_list<std::pair<const char*,int>> compat, std::initializer_list<std::pair<const char*,int>> compat,
T& val, T& val,
enum keyType::option matchOpt, enum keyType::option matchOpt,
bool mandatory IOobjectOption::readOption readOpt
) const ) const
{ {
const const_searcher finder(csearchCompat(keyword, compat, matchOpt)); if (readOpt == IOobjectOption::NO_READ)
if (finder.good())
{ {
ITstream& is = finder.ptr()->stream(); return false;
}
const entry* eptr = csearchCompat(keyword, compat, matchOpt).ptr();
if (eptr)
{
ITstream& is = eptr->stream();
is >> val; is >> val;
checkITstream(is, keyword); checkITstream(is, keyword);
return true; return true;
} }
else if (mandatory) else if (IOobjectOption::isReadRequired(readOpt))
{ {
FatalIOErrorInFunction(*this) FatalIOErrorInFunction(*this)
<< "Entry '" << keyword << "' not found in dictionary " << "Entry '" << keyword << "' not found in dictionary "
@ -408,8 +423,14 @@ bool Foam::dictionary::readIfPresent
enum keyType::option matchOpt enum keyType::option matchOpt
) const ) const
{ {
// Read is non-mandatory // Reading is optional
return readEntry<T>(keyword, val, matchOpt, false); return readEntry<T>
(
keyword,
val,
matchOpt,
IOobjectOption::READ_IF_PRESENT
);
} }
@ -422,8 +443,15 @@ bool Foam::dictionary::readCheckIfPresent
enum keyType::option matchOpt enum keyType::option matchOpt
) const ) const
{ {
// Read is non-mandatory // Reading is optional
return readCheck<T, Predicate>(keyword, val, pred, matchOpt, false); return readCheck<T, Predicate>
(
keyword,
val,
pred,
matchOpt,
IOobjectOption::READ_IF_PRESENT
);
} }
@ -436,13 +464,13 @@ T Foam::dictionary::getOrDefaultCompat
enum keyType::option matchOpt enum keyType::option matchOpt
) const ) const
{ {
const const_searcher finder(csearchCompat(keyword, compat, matchOpt)); const entry* eptr = csearchCompat(keyword, compat, matchOpt).ptr();
if (finder.good()) if (eptr)
{ {
T val; T val;
ITstream& is = finder.ptr()->stream(); ITstream& is = eptr->stream();
is >> val; is >> val;
checkITstream(is, keyword); checkITstream(is, keyword);
@ -467,8 +495,15 @@ bool Foam::dictionary::readIfPresentCompat
enum keyType::option matchOpt enum keyType::option matchOpt
) const ) const
{ {
// Read is non-mandatory // Reading is optional
return readCompat<T>(keyword, compat, val, matchOpt, false); return readCompat<T>
(
keyword,
compat,
val,
matchOpt,
IOobjectOption::READ_IF_PRESENT
);
} }

View File

@ -74,10 +74,10 @@ bool Foam::expressions::exprString::readEntry
( (
const word& keyword, const word& keyword,
const dictionary& dict, const dictionary& dict,
bool mandatory IOobjectOption::readOption readOpt
) )
{ {
const bool ok = dict.readEntry(keyword, *this, keyType::LITERAL, mandatory); const bool ok = dict.readEntry(keyword, *this, keyType::LITERAL, readOpt);
if (ok && !empty()) if (ok && !empty())
{ {

View File

@ -175,7 +175,7 @@ public:
( (
const word& keyword, //!< Lookup key. Uses LITERAL (not REGEX) const word& keyword, //!< Lookup key. Uses LITERAL (not REGEX)
const dictionary& dict, const dictionary& dict,
bool mandatory = true IOobjectOption::readOption readOpt = IOobjectOption::MUST_READ
); );
//- Read/expand optional entry with dictionary variables, //- Read/expand optional entry with dictionary variables,

View File

@ -195,7 +195,7 @@ inline bool Foam::expressions::exprString::readIfPresent
const dictionary& dict const dictionary& dict
) )
{ {
return readEntry(key, dict, false); // non-mandatory return readEntry(key, dict, IOobjectOption::READ_IF_PRESENT);
} }

View File

@ -1373,12 +1373,17 @@ void Foam::argList::parse
if (dictStream && dictStream->good()) if (dictStream && dictStream->good())
{ {
// Get numberOfSubdomains if it exists.
// - mandatory when running with distributed roots
IOobjectOption::readOption nDomainsReadOpt
= IOobjectOption::READ_IF_PRESENT;
dictionary decompDict(*dictStream); dictionary decompDict(*dictStream);
bool nDomainsMandatory = false;
if (decompDict.getOrDefault("distributed", false)) if (decompDict.getOrDefault("distributed", false))
{ {
nDomainsMandatory = true; nDomainsReadOpt = IOobjectOption::MUST_READ;
runControl_.distributed(true); runControl_.distributed(true);
decompDict.readEntry("roots", roots); decompDict.readEntry("roots", roots);
@ -1390,14 +1395,12 @@ void Foam::argList::parse
} }
} }
// Get numberOfSubdomains if it exists.
// - mandatory when running with distributed roots
decompDict.readEntry decompDict.readEntry
( (
"numberOfSubdomains", "numberOfSubdomains",
dictNProcs, dictNProcs,
keyType::LITERAL, keyType::LITERAL,
nDomainsMandatory nDomainsReadOpt
); );
} }
else else

View File

@ -54,12 +54,16 @@ Foam::Function1<Type>::New
<< "For " << entryName << " with dictionary entries: " << "For " << entryName << " with dictionary entries: "
<< flatOutput(coeffs->toc()) << nl; << flatOutput(coeffs->toc()) << nl;
// The "type" entry - mandatory if no redirectType provided
coeffs->readEntry coeffs->readEntry
( (
"type", "type",
modelType, modelType,
keyType::LITERAL, keyType::LITERAL,
modelType.empty() // "type" entry is mandatory if no 'redirect' (
modelType.empty()
? IOobjectOption::MUST_READ : IOobjectOption::READ_IF_PRESENT
)
); );
// Fallthrough // Fallthrough

View File

@ -300,18 +300,16 @@ Type Foam::motionSmootherAlgo::get
const Type& defaultValue const Type& defaultValue
) )
{ {
// If noExit, emit FatalIOError message without exiting
IOobjectOption::readOption readOpt
(
noExit
? IOobjectOption::READ_IF_PRESENT : IOobjectOption::MUST_READ
);
Type val(defaultValue); Type val(defaultValue);
if if (!dict.readEntry(keyword, val, matchOpt, readOpt))
(
!dict.readEntry
(
keyword,
val,
matchOpt,
!noExit
)
)
{ {
FatalIOError FatalIOError
<< "Entry '" << keyword << "' not found in dictionary " << "Entry '" << keyword << "' not found in dictionary "

View File

@ -62,12 +62,17 @@ Foam::patchDistMethod::New
) )
{ {
word modelType(defaultPatchDistMethod); word modelType(defaultPatchDistMethod);
// The "method" entry - mandatory if no default was provided
dict.readEntry dict.readEntry
( (
"method", "method",
modelType, modelType,
keyType::REGEX, keyType::LITERAL,
modelType.empty() // Mandatory if no default was provided (
modelType.empty()
? IOobjectOption::MUST_READ : IOobjectOption::READ_IF_PRESENT
)
); );
Info<< "Selecting patchDistMethod " << modelType << endl; Info<< "Selecting patchDistMethod " << modelType << endl;

View File

@ -1038,13 +1038,17 @@ bool Foam::functionObjects::fieldValues::surfaceFieldValue::read
} }
} }
// Mandatory if we didn't pick up a value from selectionNames_ // The "name" entry
// - mandatory if we didn't pick up a value from selectionNames_
dict.readEntry dict.readEntry
( (
"name", "name",
regionName_, regionName_,
keyType::LITERAL, keyType::LITERAL,
regionName_.empty() (
regionName_.empty()
? IOobjectOption::MUST_READ : IOobjectOption::READ_IF_PRESENT
)
); );
// Ensure there is always content for selectionNames_ // Ensure there is always content for selectionNames_

View File

@ -148,26 +148,15 @@ void Foam::medialAxisMeshMover::update(const dictionary& coeffDict)
) )
); );
// Note: parameter name changed const scalar minMedialAxisAngle
// "minMedianAxisAngle" -> "minMedialAxisAngle" (DEC-2013)
// but not previously reported.
scalar minMedialAxisAngle(Zero);
if
( (
!coeffDict.readCompat meshRefinement::get<scalar>
( (
coeffDict,
"minMedialAxisAngle", "minMedialAxisAngle",
{{ "minMedianAxisAngle", 1712 }}, dryRun_
minMedialAxisAngle,
keyType::REGEX,
!dryRun_
) )
) );
{
FatalIOError
<< "Entry '" << "minMedialAxisAngle"
<< "' not found in dictionary " << coeffDict.name() << endl;
}
const scalar minMedialAxisAngleCos(Foam::cos(degToRad(minMedialAxisAngle))); const scalar minMedialAxisAngleCos(Foam::cos(degToRad(minMedialAxisAngle)));

View File

@ -3803,7 +3803,7 @@ const Foam::dictionary& Foam::meshRefinement::subDict
auto& err = FatalIOErrorInFunction(dict); auto& err = FatalIOErrorInFunction(dict);
err << "Entry '" << keyword << "' not found in dictionary " err << "Entry '" << keyword << "' not found in dictionary "
<< dict.name() << nl; << dict.relativeName() << nl;
if (noExit) if (noExit)
{ {
@ -3827,14 +3827,14 @@ Foam::ITstream& Foam::meshRefinement::lookup
enum keyType::option matchOpt enum keyType::option matchOpt
) )
{ {
const auto finder(dict.csearch(keyword, matchOpt)); const entry* eptr = dict.findEntry(keyword, matchOpt);
if (!finder.good()) if (!eptr)
{ {
auto& err = FatalIOErrorInFunction(dict); auto& err = FatalIOErrorInFunction(dict);
err << "Entry '" << keyword << "' not found in dictionary " err << "Entry '" << keyword << "' not found in dictionary "
<< dict.name() << nl; << dict.relativeName() << nl;
if (noExit) if (noExit)
{ {
@ -3847,7 +3847,7 @@ Foam::ITstream& Foam::meshRefinement::lookup
} }
} }
return finder.ref().stream(); return eptr->stream();
} }

View File

@ -335,9 +335,16 @@ Type Foam::meshRefinement::get
const Type& deflt const Type& deflt
) )
{ {
// If noExit, emit FatalIOError message without exiting
IOobjectOption::readOption readOpt
(
noExit
? IOobjectOption::READ_IF_PRESENT : IOobjectOption::MUST_READ
);
Type val(deflt); Type val(deflt);
if (!dict.readEntry(keyword, val, matchOpt, !noExit)) if (!dict.readEntry(keyword, val, matchOpt, readOpt))
{ {
FatalIOErrorInFunction(dict) FatalIOErrorInFunction(dict)
<< "Entry '" << keyword << "' not found in dictionary " << "Entry '" << keyword << "' not found in dictionary "

View File

@ -53,12 +53,13 @@ Foam::PatchFunction1<Type>::New
<< "For " << entryName << " with dictionary entries: " << "For " << entryName << " with dictionary entries: "
<< flatOutput(coeffs->toc()) << nl; << flatOutput(coeffs->toc()) << nl;
// The "type" entry - mandatory, no fallback type provided
coeffs->readEntry coeffs->readEntry
( (
"type", "type",
modelType, modelType,
keyType::LITERAL keyType::LITERAL,
// "type" entry is mandatory, there is no 'redirect' IOobjectOption::MUST_READ
); );
} }
else if (eptr) else if (eptr)

View File

@ -59,13 +59,17 @@ namespace Foam
// Read min/max or min/span // Read min/max or min/span
static void readBoxDim(const dictionary& dict, treeBoundBox& bb) static void readBoxDim(const dictionary& dict, treeBoundBox& bb)
{ {
dict.readEntry<point>("min", bb.min()); IOobjectOption::readOption readOpt = IOobjectOption::MUST_READ;
const bool hasSpan = dict.found("span"); dict.readEntry("min", bb.min(), keyType::LITERAL, readOpt);
if (!dict.readEntry<point>("max", bb.max(), keyType::REGEX, !hasSpan))
if (dict.readIfPresent("span", bb.max(), keyType::LITERAL))
{ {
bb.max() = bb.min() + dict.get<vector>("span"); bb.max() += bb.min();
readOpt = IOobjectOption::READ_IF_PRESENT;
} }
dict.readEntry("max", bb.max(), keyType::LITERAL, readOpt);
} }
} // End namespace Foam } // End namespace Foam

View File

@ -72,13 +72,17 @@ namespace Foam
// Read min/max or min/span // Read min/max or min/span
static void readBoxDim(const dictionary& dict, treeBoundBox& bb) static void readBoxDim(const dictionary& dict, treeBoundBox& bb)
{ {
dict.readEntry<point>("min", bb.min()); IOobjectOption::readOption readOpt = IOobjectOption::MUST_READ;
const bool hasSpan = dict.found("span"); dict.readEntry("min", bb.min(), keyType::LITERAL, readOpt);
if (!dict.readEntry<point>("max", bb.max(), keyType::REGEX, !hasSpan))
if (dict.readIfPresent("span", bb.max(), keyType::LITERAL))
{ {
bb.max() = bb.min() + dict.get<vector>("span"); bb.max() += bb.min();
readOpt = IOobjectOption::READ_IF_PRESENT;
} }
dict.readEntry("max", bb.max(), keyType::LITERAL, readOpt);
} }
} // End namespace Foam } // End namespace Foam

View File

@ -72,13 +72,17 @@ namespace Foam
// Read min/max or min/span // Read min/max or min/span
static void readBoxDim(const dictionary& dict, treeBoundBox& bb) static void readBoxDim(const dictionary& dict, treeBoundBox& bb)
{ {
dict.readEntry<point>("min", bb.min()); IOobjectOption::readOption readOpt = IOobjectOption::MUST_READ;
const bool hasSpan = dict.found("span"); dict.readEntry("min", bb.min(), keyType::LITERAL, readOpt);
if (!dict.readEntry<point>("max", bb.max(), keyType::REGEX, !hasSpan))
if (dict.readIfPresent("span", bb.max(), keyType::LITERAL))
{ {
bb.max() = bb.min() + dict.get<vector>("span"); bb.max() += bb.min();
readOpt = IOobjectOption::READ_IF_PRESENT;
} }
dict.readEntry("max", bb.max(), keyType::LITERAL, readOpt);
} }
} // End namespace Foam } // End namespace Foam

View File

@ -72,13 +72,17 @@ namespace Foam
// Read min/max or min/span // Read min/max or min/span
static void readBoxDim(const dictionary& dict, treeBoundBox& bb) static void readBoxDim(const dictionary& dict, treeBoundBox& bb)
{ {
dict.readEntry<point>("min", bb.min()); IOobjectOption::readOption readOpt = IOobjectOption::MUST_READ;
const bool hasSpan = dict.found("span"); dict.readEntry("min", bb.min(), keyType::LITERAL, readOpt);
if (!dict.readEntry<point>("max", bb.max(), keyType::REGEX, !hasSpan))
if (dict.readIfPresent("span", bb.max(), keyType::LITERAL))
{ {
bb.max() = bb.min() + dict.get<vector>("span"); bb.max() += bb.min();
readOpt = IOobjectOption::READ_IF_PRESENT;
} }
dict.readEntry("max", bb.max(), keyType::LITERAL, readOpt);
} }
} // End namespace Foam } // End namespace Foam