ENH: dimensioned/dimensionedSet handle optional read as per dictionary

This commit is contained in:
Mark Olesen
2022-12-09 17:17:26 +01:00
parent 1888bc62ab
commit 4b94ac97c2
7 changed files with 59 additions and 43 deletions

View File

@ -874,10 +874,7 @@ int main(int argc, char *argv[])
// Optional: "dimensions"
dimensionSet dims;
if (dims.readEntry("dimensions", dict, false))
{
ctrl.hasDimensions = true;
}
ctrl.hasDimensions = dims.readIfPresent("dimensions", dict);
if (args.verbose() && !timei)
{

View File

@ -59,6 +59,7 @@ SourceFiles
#include "scalarField.H"
#include "PtrList.H"
#include "HashTable.H"
#include "IOobjectOption.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
@ -146,10 +147,7 @@ private:
// Member Functions
Istream& stream()
{
return is_;
}
Istream& stream() noexcept { return is_; }
bool hasToken() const;
@ -230,7 +228,7 @@ public:
(
const word& entryName, //!< Lookup key. LITERAL (not REGEX)
const dictionary& dict,
const bool mandatory = true
IOobjectOption::readOption readOpt = IOobjectOption::MUST_READ
);
//- Construct and return a clone
@ -272,9 +270,24 @@ public:
(
const word& entryName, //!< Lookup key. LITERAL (not REGEX)
const dictionary& dict, //!< The dictionary
const bool mandatory = true //!< The entry is mandatory
//! The read option
IOobjectOption::readOption readOpt = IOobjectOption::MUST_READ
);
//- Update the dimensions from dictionary entry.
//- FatalIOError if it is found and the number of tokens is incorrect,
//- or it is mandatory and not found.
//
// \return true if the entry was found.
bool readIfPresent
(
const word& entryName, //!< Lookup key. LITERAL (not REGEX)
const dictionary& dict //!< The dictionary
)
{
return readEntry(entryName, dict, IOobjectOption::READ_IF_PRESENT);
}
//- Read using provided units, return scaling in multiplier.
//- Used only in initial parsing
Istream& read

View File

@ -38,12 +38,12 @@ Foam::dimensionSet::dimensionSet
(
const word& entryName,
const dictionary& dict,
const bool mandatory
IOobjectOption::readOption readOpt
)
:
exponents_(Zero)
{
readEntry(entryName, dict, mandatory);
readEntry(entryName, dict, readOpt);
}
@ -416,9 +416,14 @@ bool Foam::dimensionSet::readEntry
(
const word& entryName,
const dictionary& dict,
const bool mandatory
IOobjectOption::readOption readOpt
)
{
if (readOpt == IOobjectOption::NO_READ)
{
return false;
}
const entry* eptr = dict.findEntry(entryName, keyType::LITERAL);
if (eptr)
@ -432,7 +437,7 @@ bool Foam::dimensionSet::readEntry
return true;
}
else if (mandatory)
else if (IOobjectOption::isReadRequired(readOpt))
{
FatalIOErrorInFunction(dict)
<< "Entry '" << entryName << "' not found in dictionary "

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2016-2020 OpenCFD Ltd.
Copyright (C) 2016-2022 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -75,11 +75,16 @@ bool Foam::dimensioned<Type>::readEntry
(
const word& key,
const dictionary& dict,
const bool mandatory,
IOobjectOption::readOption readOpt,
const bool checkDims,
enum keyType::option matchOpt
)
{
if (readOpt == IOobjectOption::NO_READ)
{
return false;
}
// Largely identical to dictionary::readEntry(),
// but with optional handling of checkDims
@ -95,7 +100,7 @@ bool Foam::dimensioned<Type>::readEntry
return true;
}
else if (mandatory)
else if (IOobjectOption::isReadRequired(readOpt))
{
FatalIOErrorInFunction(dict)
<< "Entry '" << key << "' not found in dictionary "
@ -205,7 +210,7 @@ Foam::dimensioned<Type>::dimensioned
{
ITstream& is = e.stream();
// no checkDims
// checkDims = false
initialize(is, false);
e.checkITstream(is);
@ -225,7 +230,7 @@ Foam::dimensioned<Type>::dimensioned
{
ITstream& is = e.stream();
// checkDims
// checkDims = true
initialize(is, true);
e.checkITstream(is);
@ -243,8 +248,8 @@ Foam::dimensioned<Type>::dimensioned
dimensions_(),
value_(Zero)
{
// mandatory, no checkDims
readEntry(name, dict, true, false);
// checkDims = false
readEntry(name, dict, IOobjectOption::MUST_READ, false);
}
@ -260,8 +265,8 @@ Foam::dimensioned<Type>::dimensioned
dimensions_(dims),
value_(Zero)
{
// mandatory, checkDims
readEntry(name, dict);
// checkDims = true
readEntry(name, dict, IOobjectOption::MUST_READ);
}
@ -278,8 +283,8 @@ Foam::dimensioned<Type>::dimensioned
dimensions_(dims),
value_(Zero)
{
// mandatory, checkDims
readEntry(entryName, dict);
// checkDims = true
readEntry(entryName, dict, IOobjectOption::MUST_READ);
}
@ -296,8 +301,8 @@ Foam::dimensioned<Type>::dimensioned
dimensions_(dims),
value_(val)
{
// non-mandatory, checkDims
readEntry(name, dict, false);
// checkDims = true
readEntry(name, dict, IOobjectOption::READ_IF_PRESENT);
}
@ -336,7 +341,7 @@ Foam::dimensioned<Type>::dimensioned
dimensions_(dims),
value_(Zero)
{
// checkDims
// checkDims = true
initialize(is, true);
}
@ -451,8 +456,8 @@ bool Foam::dimensioned<Type>::read
const dictionary& dict
)
{
// mandatory, checkDims
return readEntry(entryName, dict);
// checkDims = true
return readEntry(entryName, dict, IOobjectOption::MUST_READ);
}
@ -463,8 +468,8 @@ bool Foam::dimensioned<Type>::readIfPresent
const dictionary& dict
)
{
// non-mandatory, checkDims
return readEntry(entryName, dict, false);
// checkDims = true
return readEntry(entryName, dict, IOobjectOption::READ_IF_PRESENT);
}

View File

@ -35,13 +35,14 @@ SourceFiles
\*---------------------------------------------------------------------------*/
#ifndef dimensionedType_H
#define dimensionedType_H
#ifndef Foam_dimensionedType_H
#define Foam_dimensionedType_H
#include "word.H"
#include "direction.H"
#include "dimensionSet.H"
#include "VectorSpace.H"
#include "IOobjectOption.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
@ -107,7 +108,7 @@ class dimensioned
(
const word& key,
const dictionary& dict,
const bool mandatory = true, //!< entry is mandatory
IOobjectOption::readOption readOpt = IOobjectOption::MUST_READ,
const bool checkDims = true, //!< verify dimensions read
enum keyType::option matchOpt = keyType::REGEX
);

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2019-2021 OpenCFD Ltd.
Copyright (C) 2019-2022 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -166,12 +166,7 @@ bool Foam::expressions::volumeExpr::parseDriver::readDict
resultDimensions_.clear(); // Avoid stickiness
hasDimensions_ = resultDimensions_.readEntry
(
"dimensions",
dict,
false // mandatory=false
);
hasDimensions_ = resultDimensions_.readIfPresent("dimensions", dict);
return true;
}

View File

@ -393,7 +393,7 @@ bool Foam::functionObjects::fvExpressionField::read(const dictionary& dict)
// "dimensions" is optional
dimensions_.clear();
hasDimensions_ = dimensions_.readEntry("dimensions", dict, false);
hasDimensions_ = dimensions_.readIfPresent("dimensions", dict);
if (action_ == actionType::opNew)
{