diff --git a/src/OpenFOAM/db/error/error.C b/src/OpenFOAM/db/error/error.C index 5dce9fd6cdf5978e9caeb30a76fdfd1b70e34780..120ac2427a2eee52158f14bdcd40928c6367343e 100644 --- a/src/OpenFOAM/db/error/error.C +++ b/src/OpenFOAM/db/error/error.C @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2014 OpenFOAM Foundation - Copyright (C) 2015-2021 OpenCFD Ltd. + Copyright (C) 2015-2023 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -37,8 +37,24 @@ Note #include "StringStream.H" #include "foamVersion.H" #include "OSspecific.H" +#include "Enum.H" #include "Switch.H" +// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // + +const Foam::Enum +< + Foam::error::handlerTypes +> +Foam::error::handlerNames +({ + { handlerTypes::DEFAULT, "default" }, + { handlerTypes::IGNORE, "ignore" }, + { handlerTypes::WARN, "warn" }, + { handlerTypes::STRICT, "strict" }, +}); + + // * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * // bool Foam::error::master(const label communicator) diff --git a/src/OpenFOAM/db/error/error.H b/src/OpenFOAM/db/error/error.H index 36b5a200bc952203ae7049fbbd63cb2487c2be62..21dfea45f1f6b5a882e28013b97d8bf75ebdde1d 100644 --- a/src/OpenFOAM/db/error/error.H +++ b/src/OpenFOAM/db/error/error.H @@ -53,8 +53,8 @@ SeeAlso \*---------------------------------------------------------------------------*/ -#ifndef error_H -#define error_H +#ifndef Foam_error_H +#define Foam_error_H #include "messageStream.H" #include <memory> @@ -66,6 +66,7 @@ namespace Foam // Forward Declarations class OStringStream; +template<class EnumType> class Enum; /*---------------------------------------------------------------------------*\ Class error Declaration @@ -101,6 +102,21 @@ protected: public: + // Data Types + + //- Handling of errors. The exact handling depends on the local context. + enum class handlerTypes : char + { + DEFAULT = 0, //!< Default behaviour (local meaning) + IGNORE, //!< Ignore on errors/problems + WARN, //!< Warn on errors/problems + STRICT //!< Fatal on errors/problems + }; + + //- Names of the error handler types + static const Enum<handlerTypes> handlerNames; + + // Constructors //- Construct from title string diff --git a/src/OpenFOAM/db/functionObjects/functionObjectList/functionObjectList.C b/src/OpenFOAM/db/functionObjects/functionObjectList/functionObjectList.C index d1e4105d0a9facbf0aa1df9b35474e3dc989aafb..3c0beb533a32a79994da1a5f60239838b56c9575 100644 --- a/src/OpenFOAM/db/functionObjects/functionObjectList/functionObjectList.C +++ b/src/OpenFOAM/db/functionObjects/functionObjectList/functionObjectList.C @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2017 OpenFOAM Foundation - Copyright (C) 2015-2022 OpenCFD Ltd. + Copyright (C) 2015-2023 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -44,7 +44,7 @@ License /* * * * * * * * * * * * * * * Static Member Data * * * * * * * * * * * * * */ //- Max number of warnings (per functionObject) -static constexpr const uint32_t maxWarnings = 10u; +static constexpr const unsigned maxWarnings = 10u; Foam::fileName Foam::functionObjectList::functionObjectDictPath ( @@ -52,19 +52,6 @@ Foam::fileName Foam::functionObjectList::functionObjectDictPath ); -const Foam::Enum -< - Foam::functionObjectList::errorHandlingType -> -Foam::functionObjectList::errorHandlingNames_ -({ - { errorHandlingType::DEFAULT, "default" }, - { errorHandlingType::WARN, "warn" }, - { errorHandlingType::IGNORE, "ignore" }, - { errorHandlingType::STRICT, "strict" }, -}); - - // * * * * * * * * * * * * * * * Local Functions * * * * * * * * * * * * * * // namespace Foam @@ -350,12 +337,12 @@ bool Foam::functionObjectList::readFunctionObject } -Foam::functionObjectList::errorHandlingType +Foam::error::handlerTypes Foam::functionObjectList::getOrDefaultErrorHandling ( const word& key, const dictionary& dict, - const errorHandlingType deflt + const error::handlerTypes deflt ) const { const entry* eptr = dict.findEntry(key, keyType::LITERAL); @@ -372,16 +359,16 @@ Foam::functionObjectList::getOrDefaultErrorHandling { const word enumName(eptr->get<word>()); - if (!errorHandlingNames_.found(enumName)) + if (!error::handlerNames.found(enumName)) { // Failed the name lookup FatalIOErrorInFunction(dict) << enumName << " is not in enumeration: " - << errorHandlingNames_ << nl + << error::handlerNames << nl << exit(FatalIOError); } - return errorHandlingNames_.get(enumName); + return error::handlerNames.get(enumName); } } @@ -640,15 +627,15 @@ bool Foam::functionObjectList::execute() for (functionObject& funcObj : functions()) { - const errorHandlingType errorHandling = *errIter; + const auto errorHandling = *errIter; ++errIter; const word& objName = funcObj.name(); if ( - errorHandling == errorHandlingType::WARN - || errorHandling == errorHandlingType::IGNORE + errorHandling == error::handlerTypes::WARN + || errorHandling == error::handlerTypes::IGNORE ) { // Throw FatalError, FatalIOError as exceptions @@ -672,12 +659,12 @@ bool Foam::functionObjectList::execute() catch (const Foam::error& err) { // Treat IOerror and error identically - uint32_t nWarnings; + unsigned nWarnings; hadError = true; if ( - errorHandling != errorHandlingType::IGNORE + (errorHandling != error::handlerTypes::IGNORE) && (nWarnings = ++warnings_(objName)) <= maxWarnings ) { @@ -718,11 +705,11 @@ bool Foam::functionObjectList::execute() catch (const Foam::error& err) { // Treat IOerror and error identically - uint32_t nWarnings; + unsigned nWarnings; if ( - errorHandling != errorHandlingType::IGNORE + (errorHandling != error::handlerTypes::IGNORE) && (nWarnings = ++warnings_(objName)) <= maxWarnings ) { @@ -851,7 +838,7 @@ bool Foam::functionObjectList::end() for (functionObject& funcObj : functions()) { - const errorHandlingType errorHandling = *errIter; + const auto errorHandling = *errIter; ++errIter; const word& objName = funcObj.name(); @@ -870,11 +857,11 @@ bool Foam::functionObjectList::end() catch (const Foam::error& err) { // Treat IOerror and error identically - uint32_t nWarnings; + unsigned nWarnings; if ( - errorHandling != errorHandlingType::IGNORE + (errorHandling != error::handlerTypes::IGNORE) && (nWarnings = ++warnings_(objName)) <= maxWarnings ) { @@ -982,7 +969,7 @@ bool Foam::functionObjectList::read() errorHandling_.resize ( functionsDict.size(), - errorHandlingType::DEFAULT + error::handlerTypes::DEFAULT ); HashTable<label> newIndices; @@ -998,12 +985,12 @@ bool Foam::functionObjectList::read() ); // Top-level "errors" specification (optional) - const errorHandlingType errorHandlingFallback = + const error::handlerTypes errorHandlingFallback = getOrDefaultErrorHandling ( "errors", functionsDict, - errorHandlingType::DEFAULT + error::handlerTypes::DEFAULT ); label nFunc = 0; @@ -1045,7 +1032,7 @@ bool Foam::functionObjectList::read() bool enabled = dict.getOrDefault("enabled", true); // Per-function "errors" specification - const errorHandlingType errorHandling = + const error::handlerTypes errorHandling = getOrDefaultErrorHandling ( "errors", @@ -1135,16 +1122,16 @@ bool Foam::functionObjectList::read() switch (errorHandling) { - case errorHandlingType::IGNORE: + case error::handlerTypes::IGNORE: break; - case errorHandlingType::STRICT: + case error::handlerTypes::STRICT: { exitNow(err); break; } - case errorHandlingType::DEFAULT: + case error::handlerTypes::DEFAULT: { if (isA<Foam::IOerror>(err)) { @@ -1157,7 +1144,7 @@ bool Foam::functionObjectList::read() [[fallthrough]]; } - case errorHandlingType::WARN: + case error::handlerTypes::WARN: { // Trickery to get original message err.write(Warning, false); diff --git a/src/OpenFOAM/db/functionObjects/functionObjectList/functionObjectList.H b/src/OpenFOAM/db/functionObjects/functionObjectList/functionObjectList.H index fa57a3c78f21272f8c3a74b7e0f637a0f0f9fc6c..da9df075273c3219f1edccccbcbc210378a473c6 100644 --- a/src/OpenFOAM/db/functionObjects/functionObjectList/functionObjectList.H +++ b/src/OpenFOAM/db/functionObjects/functionObjectList/functionObjectList.H @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2016 OpenFOAM Foundation - Copyright (C) 2015-2021 OpenCFD Ltd. + Copyright (C) 2015-2023 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -78,11 +78,10 @@ SourceFiles \*---------------------------------------------------------------------------*/ -#ifndef functionObjectList_H -#define functionObjectList_H +#ifndef Foam_functionObjectList_H +#define Foam_functionObjectList_H #include "PtrList.H" -#include "Enum.H" #include "functionObject.H" #include "SHA1Digest.H" #include "HashTable.H" @@ -108,25 +107,10 @@ class functionObjectList : private PtrList<functionObject> { - // Data Types - - //- Handling of construction or execution errors - enum class errorHandlingType : uint8_t - { - DEFAULT = 0, //!< Warn on construct, Fatal on runtime - WARN, //!< Warn on construct, Warn on runtime - IGNORE, //!< Ignore on construct, Ignore on runtime - STRICT, //!< Fatal on construct, Fatal on runtime - }; - - //- Names for error handling types - static const Enum<errorHandlingType> errorHandlingNames_; - - // Private Data //- A list of error/warning handling - List<errorHandlingType> errorHandling_; + List<error::handlerTypes> errorHandling_; //- A list of SHA1 digests for the function object dictionaries List<SHA1Digest> digests_; @@ -137,7 +121,7 @@ class functionObjectList //- Track the number of warnings per function object and limit // to a predefined number to avoid flooding the display. // Clear on re-read of functions. - HashTable<uint32_t> warnings_; + HashTable<unsigned> warnings_; //- Reference to Time const Time& time_; @@ -187,11 +171,11 @@ class functionObjectList // // This additional treatment is to ensure that potentially existing // code with an "errors" functionObject will continue to run. - errorHandlingType getOrDefaultErrorHandling + error::handlerTypes getOrDefaultErrorHandling ( const word& key, const dictionary& dict, - const errorHandlingType deflt + const error::handlerTypes deflt ) const;