diff --git a/applications/utilities/preProcessing/setExprFields/setExprFields.C b/applications/utilities/preProcessing/setExprFields/setExprFields.C index bc367fa2bf2b76e6ee80e91ae2550daee3051fc1..2d493cd02219b4062ce2358ae2e408c2f7e5fd06 100644 --- a/applications/utilities/preProcessing/setExprFields/setExprFields.C +++ b/applications/utilities/preProcessing/setExprFields/setExprFields.C @@ -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) { diff --git a/src/OpenFOAM/dimensionSet/dimensionSet.H b/src/OpenFOAM/dimensionSet/dimensionSet.H index c1ada0612c0f0fe78049866a6385f48ece0f6d20..a579c7b5bc3cc88742de9abcb9c712c18c3100b5 100644 --- a/src/OpenFOAM/dimensionSet/dimensionSet.H +++ b/src/OpenFOAM/dimensionSet/dimensionSet.H @@ -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 diff --git a/src/OpenFOAM/dimensionSet/dimensionSetIO.C b/src/OpenFOAM/dimensionSet/dimensionSetIO.C index cfcb215b7a27f255a2ee15cc1cdefcbc56f2c350..4e3301a21a0548293e296b352ce035115615363a 100644 --- a/src/OpenFOAM/dimensionSet/dimensionSetIO.C +++ b/src/OpenFOAM/dimensionSet/dimensionSetIO.C @@ -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 " diff --git a/src/OpenFOAM/dimensionedTypes/dimensionedType/dimensionedType.C b/src/OpenFOAM/dimensionedTypes/dimensionedType/dimensionedType.C index 8afb532ad1cd4425091bc71596d8e099ae4704a6..c8f2c06e1d09b4702a9bceb0f5e6b789ef4e36df 100644 --- a/src/OpenFOAM/dimensionedTypes/dimensionedType/dimensionedType.C +++ b/src/OpenFOAM/dimensionedTypes/dimensionedType/dimensionedType.C @@ -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); } diff --git a/src/OpenFOAM/dimensionedTypes/dimensionedType/dimensionedType.H b/src/OpenFOAM/dimensionedTypes/dimensionedType/dimensionedType.H index 875c1115f5a2842a33952a3e59985fefcfbc9504..0b6f7975c1369c04c25eac9f05b0fb32eeea46a4 100644 --- a/src/OpenFOAM/dimensionedTypes/dimensionedType/dimensionedType.H +++ b/src/OpenFOAM/dimensionedTypes/dimensionedType/dimensionedType.H @@ -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 ); diff --git a/src/finiteVolume/expressions/volume/volumeExprDriver.C b/src/finiteVolume/expressions/volume/volumeExprDriver.C index ff9e160fbc148cfd46f2bef4f7fcce56d16cce0b..f1f1b57ac7dd277c3266a5e8da5ad11441c2a8d9 100644 --- a/src/finiteVolume/expressions/volume/volumeExprDriver.C +++ b/src/finiteVolume/expressions/volume/volumeExprDriver.C @@ -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; } diff --git a/src/functionObjects/field/expressions/fvExpressionField.C b/src/functionObjects/field/expressions/fvExpressionField.C index 139bfaf6c5e92e00b60002ecd7a48c94ead93b24..3479c14ef089a5f7d1f6b48c61b80561133fe67e 100644 --- a/src/functionObjects/field/expressions/fvExpressionField.C +++ b/src/functionObjects/field/expressions/fvExpressionField.C @@ -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) {