ENH: add dictionary::findStream() - symmetric with findDict()

- can be used with this type of code:

  ITstream* streamPtr = dict.findStream(name);
  if (streamPtr)
  {
      auto& is = *streamPtr;
      ...
  }

  versus:

  const entry* eptr = dict.findEntry(name);
  if (eptr && eptr->isStream())
  {
      auto& is = eptr->stream();
      ...
  }

ENH: add findStream(), streamPtr(), isStream() to dictionary search

- symmetric with findDict(), dictPtr(), isDict() methods

STYLE: use findDict() instead of found() + subDict() pairing

COMP: define is_globalIOobject trait at top of IOobject header

- more visibility, permits reuse for specializations etc.
This commit is contained in:
Mark Olesen 2024-05-13 14:31:32 +02:00
parent 6a80d4de40
commit 3b9176665f
15 changed files with 116 additions and 100 deletions

View File

@ -66,13 +66,13 @@ int main(int argc, char *argv[])
for (const entry& dEntry : dict)
{
if (!dEntry.isStream())
if (dEntry.isStream())
{
continue;
List<namedDictionary> list(dEntry.stream());
Info<< "input: " << dEntry << nl
<< "list: " << list << nl;
}
Info<< "input: " << dEntry << nl;
List<namedDictionary> list(dEntry.stream());
Info<< "list: " << list << nl;
}
}
}

View File

@ -549,16 +549,17 @@ int main(int argc, char *argv[])
{
Info<< finder.dict();
}
else if (finder.ref().isStream())
else if (finder.isStream())
{
bool addSep = false;
bool separator = false;
const tokenList& tokens = finder.ref().stream();
for (const token& tok : tokens)
for (const token& tok : finder.stream())
{
if (addSep) Info<< token::SPACE;
addSep = true;
if (separator)
{
Info<< token::SPACE;
}
separator = true;
Info<< tok;
}
Info<< endl;

View File

@ -121,6 +121,12 @@ class IOobject;
template<>
Ostream& operator<<(Ostream&, const InfoProxy<IOobject>&);
// Traits
//- Trait for specifying global vs. local IOobject file types
template<class T>
struct is_globalIOobject : std::false_type {};
/*---------------------------------------------------------------------------*\
Class IOobject Declaration
@ -712,6 +718,8 @@ public:
};
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
//- Specialization for \c void always returns true (no headerClassName check).
template<>
inline bool IOobject::isHeaderClass<void>() const
@ -720,11 +728,6 @@ inline bool IOobject::isHeaderClass<void>() const
}
//- Trait for specifying global vs. local file types
template<class T>
struct is_globalIOobject : std::false_type {};
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
} // End namespace Foam

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2017 OpenFOAM Foundation
Copyright (C) 2016-2022 OpenCFD Ltd.
Copyright (C) 2016-2024 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -183,8 +183,7 @@ public:
pointer eptr_;
//- Construct for the given dictionary context.
// Allow implicit conversion
//- Implicit construct for the given dictionary context
Searcher(dict_pointer dict) noexcept
:
dict_(dict),
@ -212,6 +211,7 @@ public:
bool good() const noexcept { return eptr_; }
//- True if entry was found
// \deprecated(2019-01) - prefer good() method
bool found() const noexcept { return eptr_; }
//- The containing dictionary context
@ -229,19 +229,32 @@ public:
return (eptr_ && eptr_->dictPtr());
}
//- True if found entry is a stream.
bool isStream() const noexcept
{
return (eptr_ && eptr_->streamPtr());
}
//- Pointer to the found entry as a dictionary, nullptr otherwise
dict_pointer dictPtr() const noexcept
{
return eptr_ ? eptr_->dictPtr() : nullptr;
return (eptr_ ? eptr_->dictPtr() : nullptr);
}
//- Reference the found entry as a dictionary.
// (Error if not found, or not a dictionary).
dict_reference dict() const
//- Pointer to the found entry as a stream, nullptr otherwise
ITstream* streamPtr() const noexcept
{
return eptr_->dict();
return (eptr_ ? eptr_->streamPtr() : nullptr);
}
//- Return the found entry as a dictionary.
//- Error if not found, or not a dictionary.
dict_reference dict() const { return eptr_->dict(); }
//- Return the found entry as a ITstream.
//- Error if not found, or not a stream.
ITstream& stream() const { return eptr_->stream(); }
//- Permit an explicit cast to the other (const/non-const) searcher
explicit operator const Searcher<!Const>&() const
{
@ -249,16 +262,10 @@ public:
}
//- A pointer to the entry (nullptr if not found)
pointer operator->() const noexcept
{
return eptr_;
}
pointer operator->() const noexcept { return eptr_; }
//- A reference to the entry (Error if not found)
reference operator*() const
{
return *eptr_;
}
reference operator*() const { return *eptr_; }
};
@ -557,7 +564,7 @@ public:
) const;
//- Find and return a sub-dictionary pointer if present
//- (and a sub-dictionary) otherwise return nullptr.
//- (and it is a dictionary) otherwise return nullptr.
//
// \param keyword the keyword to search for
// \param matchOpt search mode (default: non-recursive with patterns)
@ -570,7 +577,7 @@ public:
) const;
//- Find and return a sub-dictionary pointer if present
//- (and a sub-dictionary) otherwise return nullptr.
//- (and it is a dictionary) otherwise return nullptr.
//
// \param keyword the keyword to search for
// \param matchOpt search mode (default: non-recursive with patterns)
@ -582,13 +589,14 @@ public:
enum keyType::option matchOpt = keyType::REGEX
);
//- Find a sub-dictionary.
//- Find and return an entry stream if present
//- (and it is a stream) otherwise return nullptr.
//
// \param keyword the keyword to search for
// \param matchOpt search mode (default: non-recursive with patterns)
//
// \return true if the sub-dictionary was found
inline bool isDict
// \return pointer to ITstream or a nullptr
inline ITstream* findStream
(
const word& keyword,
enum keyType::option matchOpt = keyType::REGEX
@ -1283,7 +1291,7 @@ public:
// Shortcuts - when a templated classes also inherits from a dictionary
#undef defineDictionaryGetter
#undef defineDictionaryGetter
#define defineDictionaryGetter(Func, Type) \
/*! \brief Same as get\<Type\>(const word&, keyType::option) */ \
Type Func \
@ -1307,6 +1315,18 @@ public:
// Housekeeping
//- Check for existence of a sub-dictionary.
//- Generally prefer findDict() for more flexibility.
FOAM_DEPRECATED_STRICT(2024-05, "findDict()")
bool isDict
(
const word& keyword,
enum keyType::option matchOpt = keyType::REGEX
) const
{
return static_cast<bool>(findDict(keyword, matchOpt));
}
//- Same as getOrDefault()
template<class T>
FOAM_DEPRECATED_STRICT(2019-06, "getOrDefault()")
@ -1320,7 +1340,6 @@ public:
return getOrDefault<T>(keyword, deflt, matchOpt);
}
//- Same as getOrAdd()
template<class T>
FOAM_DEPRECATED_STRICT(2019-06, "getOrAdd()")

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2021-2023 OpenCFD Ltd.
Copyright (C) 2021-2024 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -147,13 +147,13 @@ inline Foam::dictionary* Foam::dictionary::findDict
}
inline bool Foam::dictionary::isDict
inline Foam::ITstream* Foam::dictionary::findStream
(
const word& keyword,
enum keyType::option matchOpt
) const
{
return static_cast<bool>(findDict(keyword, matchOpt));
return csearch(keyword, matchOpt).streamPtr();
}

View File

@ -214,32 +214,34 @@ public:
virtual label endLineNumber() const = 0;
//- Return true if this entry is a stream
virtual bool isStream() const noexcept { return false; }
//- True if this entry is a stream
virtual bool isStream() const noexcept { return this->streamPtr(); }
//- Return pointer to token stream, if entry is a primitive entry
// Return nullptr if the entry is not a primitive entry
virtual ITstream* streamPtr() const { return nullptr; }
//- Return pointer to token stream, if it is a primitive entry,
//- otherwise return nullptr
virtual ITstream* streamPtr() const noexcept { return nullptr; }
//- Return token stream, if entry is a primitive entry
virtual ITstream& stream() const = 0;
//- Return true if this entry is a dictionary
//- True if this entry is a dictionary
virtual bool isDict() const noexcept { return this->dictPtr(); }
//- Return pointer to dictionary, if entry is a dictionary.
// Return nullptr if the entry is not a dictionary.
//- Return pointer to dictionary, if entry is a dictionary,
//- otherwise return nullptr.
virtual const dictionary* dictPtr() const noexcept { return nullptr; }
//- Return non-const pointer to dictionary, if entry is a dictionary
// Return nullptr if the entry is not a dictionary.
//- Return non-const pointer to dictionary, if entry is a dictionary,
//- otherwise return nullptr.
virtual dictionary* dictPtr() noexcept { return nullptr; }
//- Return dictionary, if entry is a dictionary
//- Return dictionary, if entry is a dictionary,
//- otherwise Fatal.
virtual const dictionary& dict() const = 0;
//- Return non-const access to dictionary, if entry is a dictionary
//- Return non-const access to dictionary, if entry is a dictionary,
//- otherwise Fatal.
virtual dictionary& dict() = 0;

View File

@ -247,7 +247,7 @@ bool Foam::entry::New
if (finder.good())
{
// Read as primitiveEntry
const keyType newKeyword(finder.ptr()->stream());
const keyType newKeyword(finder.stream());
return parentDict.add
(

View File

@ -273,7 +273,7 @@ Foam::primitiveEntry::primitiveEntry
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
Foam::ITstream* Foam::primitiveEntry::streamPtr() const
Foam::ITstream* Foam::primitiveEntry::streamPtr() const noexcept
{
ITstream* ptr = const_cast<primitiveEntry*>(this);
ptr->seek(0);

View File

@ -181,14 +181,8 @@ public:
return ITstream::endLineNumber();
}
//- Return true - this entry is a stream
virtual bool isStream() const noexcept
{
return true;
}
//- Return pointer to token stream for this primitive entry
virtual ITstream* streamPtr() const;
virtual ITstream* streamPtr() const noexcept;
//- Return token stream for this primitive entry
virtual ITstream& stream() const;

View File

@ -241,7 +241,7 @@ Foam::label Foam::solution::upgradeSolverDict
// recast primitive entries into dictionary entries
for (const entry& dEntry : dict)
{
if (!dEntry.isDict())
if (dEntry.isStream())
{
ITstream& is = dEntry.stream();
word name(is);
@ -256,11 +256,12 @@ Foam::label Foam::solution::upgradeSolverDict
// transform primitiveEntry with settings -> dictionaryEntry
for (const word& dictName : subDictNames)
{
entry* eptr = subdict.findEntry(dictName, keyType::LITERAL);
ITstream* streamPtr =
subdict.findStream(dictName, keyType::LITERAL);
if (eptr && !eptr->isDict())
if (streamPtr)
{
ITstream& is = eptr->stream();
auto& is = *streamPtr;
is >> name;
if (!is.eof())

View File

@ -129,9 +129,9 @@ Foam::coordinateSystem::New
{
dictPtr = finder.dictPtr();
}
else if (finder.good())
else if (finder.isStream())
{
const word csName(finder.ref().stream());
const word csName(finder.stream());
// Deprecated, unsupported syntax
if (error::master())

View File

@ -522,9 +522,9 @@ void Foam::cellTable::combine(const dictionary& mapDict, labelList& tableIds)
labelList mapping(identity(this->maxIndex() + 1));
bool remap = false;
forAllConstIters(mapDict, iter)
for (const entry& dEntry : mapDict)
{
wordRes patterns(iter().stream());
wordRes patterns(dEntry.stream());
// find all matches
Map<word> matches;
@ -538,14 +538,14 @@ void Foam::cellTable::combine(const dictionary& mapDict, labelList& tableIds)
if (matches.size())
{
label targetId = this->findIndex(iter().keyword());
label targetId = this->findIndex(dEntry.keyword());
Info<< "combine cellTable: " << iter().keyword();
Info<< "combine cellTable: " << dEntry.keyword();
if (targetId < 0)
{
// not found - reuse 1st element but with different name
targetId = min(matches.toc());
operator[](targetId).set("Label", iter().keyword());
operator[](targetId).set("Label", dEntry.keyword());
Info<< " = (";
}

View File

@ -35,17 +35,11 @@ namespace Foam
static inline const Foam::entry* resolveLabel(const entry& e, const label val)
{
if (e.isStream())
{
const tokenList& toks = e.stream();
if (!toks.empty() && toks[0].isLabel(val))
{
return &e;
}
}
return nullptr;
return
(
(e.isStream() && e.stream().front().isLabel(val))
? &e : nullptr
);
}
} // End namespace Foam

View File

@ -323,29 +323,29 @@ Foam::optimisationManager::optimisationManager(fvMesh& mesh)
"optimisationDict",
mesh.time().system(),
mesh,
IOobject::MUST_READ_IF_MODIFIED,
IOobject::READ_MODIFIED,
IOobject::NO_WRITE,
IOobject::REGISTER
)
),
mesh_(mesh),
time_(const_cast<Time&>(mesh.time())),
designVars_
(
this->subOrEmptyDict("optimisation").isDict("designVariables") ?
designVariables::New
(
mesh_,
subDict("optimisation").subDict("designVariables")
) :
nullptr
),
designVars_(nullptr),
primalSolvers_(),
adjointSolverManagers_(),
managerType_(get<word>("optimisationManager")),
dvUpdate_(nullptr),
shouldUpdateDesignVariables_(true)
{}
{
// The "designVariables" sub-dictionary is optional
const dictionary* designVarsDictPtr =
this->subOrEmptyDict("optimisation").findDict("designVariables");
if (designVarsDictPtr)
{
designVars_ = designVariables::New(mesh_, *designVarsDictPtr);
}
}
// * * * * * * * * * * * * * * * * Selectors * * * * * * * * * * * * * * //

View File

@ -37,9 +37,11 @@ Foam::autoPtr<Foam::surfaceTensionModel> Foam::surfaceTensionModel::New
const fvMesh& mesh
)
{
if (dict.isDict("sigma"))
const dictionary* sigmaDictPtr = dict.findDict("sigma");
if (sigmaDictPtr)
{
const dictionary& sigmaDict = surfaceTensionModel::sigmaDict(dict);
const dictionary& sigmaDict = *sigmaDictPtr;
const word modelType(sigmaDict.get<word>("type"));