From 828693bc90a6633157eb6fb4e8206dcdb8f2301c Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Mon, 18 Aug 2025 14:27:58 +0200 Subject: [PATCH] ENH: explicitly handle empty name lookup (objectRegistry, IOobjectList) - provides some additional safety and precludes any recursive searching ENH: support local directory handling in regionProperties - allows alternative locations. Eg, for finite-area (#3419) --- src/OpenFOAM/db/IOobjectList/IOobjectList.C | 15 ++-- src/OpenFOAM/db/IOobjectList/IOobjectList.H | 6 +- src/OpenFOAM/db/IOobjectList/IOobjectListI.H | 8 +-- .../db/IOobjectList/IOobjectListTemplates.C | 12 ++-- .../db/objectRegistry/objectRegistry.C | 10 +-- .../db/objectRegistry/objectRegistry.H | 72 ++++++++----------- .../objectRegistry/objectRegistryTemplates.C | 7 +- .../regionProperties/regionProperties.C | 32 +++++---- .../regionProperties/regionProperties.H | 32 +++++++-- 9 files changed, 102 insertions(+), 92 deletions(-) diff --git a/src/OpenFOAM/db/IOobjectList/IOobjectList.C b/src/OpenFOAM/db/IOobjectList/IOobjectList.C index c06d0f7503..0763495f42 100644 --- a/src/OpenFOAM/db/IOobjectList/IOobjectList.C +++ b/src/OpenFOAM/db/IOobjectList/IOobjectList.C @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2017 OpenFOAM Foundation - Copyright (C) 2016-2023 OpenCFD Ltd. + Copyright (C) 2016-2025 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -101,8 +101,6 @@ Foam::IOobjectList::IOobjectList const fileName& local, IOobjectOption ioOpt ) -: - HashPtrTable() { word newInstance; fileNameList objNames = fileHandler().readObjects @@ -113,6 +111,8 @@ Foam::IOobjectList::IOobjectList newInstance ); + HashPtrTable::reserve(objNames.size()); + for (const auto& objName : objNames) { auto objectPtr = autoPtr::New @@ -141,7 +141,7 @@ Foam::IOobjectList::IOobjectList if (ok) { - insert(objectPtr->name(), objectPtr); + insert(objectPtr->name(), std::move(objectPtr)); } } } @@ -157,8 +157,11 @@ const Foam::IOobject* Foam::IOobjectList::cfindObject // Like HashPtrTable::get(), or lookup() with a nullptr const IOobject* io = nullptr; - const const_iterator iter(cfind(objName)); - if (iter.good()) + if (objName.empty()) + { + return nullptr; + } + else if (auto iter = cfind(objName); iter.good()) { io = iter.val(); } diff --git a/src/OpenFOAM/db/IOobjectList/IOobjectList.H b/src/OpenFOAM/db/IOobjectList/IOobjectList.H index d5e14a637d..6c140dae16 100644 --- a/src/OpenFOAM/db/IOobjectList/IOobjectList.H +++ b/src/OpenFOAM/db/IOobjectList/IOobjectList.H @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2016 OpenFOAM Foundation - Copyright (C) 2016-2024 OpenCFD Ltd. + Copyright (C) 2016-2025 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -172,7 +172,7 @@ public: IOobjectList() noexcept = default; //- Construct empty without allocation (capacity=0) - inline explicit IOobjectList(const Foam::zero) noexcept; + explicit IOobjectList(Foam::zero) noexcept {} //- Construct empty with initial table capacity inline explicit IOobjectList(const label initialCapacity); @@ -181,7 +181,7 @@ public: inline IOobjectList(const IOobjectList& list); //- Move construct - inline IOobjectList(IOobjectList&& list); + inline IOobjectList(IOobjectList&& list) noexcept; //- Construct from registry, instance, io options inline IOobjectList diff --git a/src/OpenFOAM/db/IOobjectList/IOobjectListI.H b/src/OpenFOAM/db/IOobjectList/IOobjectListI.H index 24044638ec..22221b19ff 100644 --- a/src/OpenFOAM/db/IOobjectList/IOobjectListI.H +++ b/src/OpenFOAM/db/IOobjectList/IOobjectListI.H @@ -27,12 +27,6 @@ License // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // -inline Foam::IOobjectList::IOobjectList(const Foam::zero) noexcept -: - HashPtrTable(Foam::zero{}) -{} - - inline Foam::IOobjectList::IOobjectList(const label initialCapacity) : HashPtrTable(initialCapacity) @@ -45,7 +39,7 @@ inline Foam::IOobjectList::IOobjectList(const IOobjectList& list) {} -inline Foam::IOobjectList::IOobjectList(IOobjectList&& list) +inline Foam::IOobjectList::IOobjectList(IOobjectList&& list) noexcept : HashPtrTable(std::move(list)) {} diff --git a/src/OpenFOAM/db/IOobjectList/IOobjectListTemplates.C b/src/OpenFOAM/db/IOobjectList/IOobjectListTemplates.C index 475c3586b6..d41f6716dc 100644 --- a/src/OpenFOAM/db/IOobjectList/IOobjectListTemplates.C +++ b/src/OpenFOAM/db/IOobjectList/IOobjectListTemplates.C @@ -5,7 +5,7 @@ \\ / A nd | www.openfoam.com \\/ M anipulation | ------------------------------------------------------------------------------- - Copyright (C) 2018-2023 OpenCFD Ltd. + Copyright (C) 2018-2025 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -38,7 +38,8 @@ Foam::HashTable Foam::IOobjectList::classesImpl const MatchPredicate& matchName ) { - HashTable summary(2*list.size()); + HashTable summary; + summary.reserve(16); // Relatively few types // Summary (key,val) = (class-name, object-names) forAllConstIters(list, iter) @@ -315,8 +316,11 @@ const Foam::IOobject* Foam::IOobjectList::cfindObject // Like HashPtrTable::get(), or lookup() with a nullptr const IOobject* io = nullptr; - const const_iterator iter(cfind(objName)); - if (iter.good()) + if (objName.empty()) + { + return nullptr; + } + else if (auto iter = cfind(objName); iter.good()) { io = iter.val(); } diff --git a/src/OpenFOAM/db/objectRegistry/objectRegistry.C b/src/OpenFOAM/db/objectRegistry/objectRegistry.C index 2978baad85..349ba07140 100644 --- a/src/OpenFOAM/db/objectRegistry/objectRegistry.C +++ b/src/OpenFOAM/db/objectRegistry/objectRegistry.C @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2019 OpenFOAM Foundation - Copyright (C) 2015-2024 OpenCFD Ltd. + Copyright (C) 2015-2025 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -513,9 +513,11 @@ const Foam::regIOobject* Foam::objectRegistry::cfindIOobject const bool recursive ) const { - const_iterator iter = cfind(name); - - if (iter.good()) + if (name.empty()) + { + return nullptr; + } + else if (auto iter = cfind(name); iter.good()) { return iter.val(); } diff --git a/src/OpenFOAM/db/objectRegistry/objectRegistry.H b/src/OpenFOAM/db/objectRegistry/objectRegistry.H index 9fed2b2c2c..3953941267 100644 --- a/src/OpenFOAM/db/objectRegistry/objectRegistry.H +++ b/src/OpenFOAM/db/objectRegistry/objectRegistry.H @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2019 OpenFOAM Foundation - Copyright (C) 2016-2024 OpenCFD Ltd. + Copyright (C) 2016-2025 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -457,14 +457,11 @@ public: // Lookup //- Lookup and return a const sub-objectRegistry. - // - // \param forceCreate create it if it does not exist. - // \param recursive search parent registries. const objectRegistry& subRegistry ( const word& name, - const bool forceCreate = false, - const bool recursive = false + const bool forceCreate = false, //!< Create if it does not exist + const bool recursive = false //!< Search parent registries ) const; @@ -492,101 +489,90 @@ public: //- Return const pointer to the regIOobject. // - // \param recursive search parent registries - // - // \return nullptr if the object was not found. + // \return nullptr if the object was not found (or name == "") const regIOobject* cfindIOobject ( - const word& name, - const bool recursive = false + const word& name, //!< The object name + const bool recursive = false //!< Search parent registries ) const; //- Does the registry contain the regIOobject object (by name). - // - // \param name the object name - // \param recursive search parent registries - bool contains(const word& name, const bool recursive = false) const; + // \return false if the object was not found (or name == "") + bool contains + ( + const word& name, //!< The object name + const bool recursive = false //!< Search parent registries + ) const; - //- Is the named Type found? + //- Contains the named Type? // - // \param recursive search parent registries + // \return false if the object was not found (or name == "") + // or had incorrect type. template bool foundObject ( - const word& name, - const bool recursive = false + const word& name, //!< The object name + const bool recursive = false //!< Search parent registries ) const; //- Return const pointer to the object of the given Type. // - // \param recursive search parent registries - // // \return nullptr if the object was not found or had incorrect type. template const Type* cfindObject ( - const word& name, - const bool recursive = false + const word& name, //!< The object name + const bool recursive = false //!< Search parent registries ) const; //- Return const pointer to the object of the given Type. // - // \param recursive search parent registries - // // \return nullptr if the object was not found or had incorrect type. template const Type* findObject ( - const word& name, - const bool recursive = false + const word& name, //!< The object name + const bool recursive = false //!< Search parent registries ) const; //- Return non-const pointer to the object of the given Type. // - // \param recursive search parent registries - // // \return nullptr if the object was not found or had incorrect type. template Type* findObject ( - const word& name, - const bool recursive = false + const word& name, //!< The object name + const bool recursive = false //!< Search parent registries ); //- Return non-const pointer to the object of the given Type, //- using a const-cast to have it behave like a mutable. // Exercise caution when using. // - // \param recursive search parent registries. - // // \return nullptr if the object was not found or had incorrect type. template Type* getObjectPtr ( - const word& name, - const bool recursive = false + const word& name, //!< The object name + const bool recursive = false //!< Search parent registries ) const; //- Lookup and return const reference to the object //- of the given Type. Fatal if not found or the wrong type. - // - // \param recursive search parent registries. template const Type& lookupObject ( - const word& name, - const bool recursive = false + const word& name, //!< The object name + const bool recursive = false //!< Search parent registries ) const; //- Lookup and return non-const reference to the object //- of the given Type. Fatal if not found or the wrong type. - // - // \param recursive search parent registries. template Type& lookupObjectRef ( - const word& name, - const bool recursive = false + const word& name, //!< The object name + const bool recursive = false //!< Search parent registries ) const; diff --git a/src/OpenFOAM/db/objectRegistry/objectRegistryTemplates.C b/src/OpenFOAM/db/objectRegistry/objectRegistryTemplates.C index 1b13cf4078..d3f1766a25 100644 --- a/src/OpenFOAM/db/objectRegistry/objectRegistryTemplates.C +++ b/src/OpenFOAM/db/objectRegistry/objectRegistryTemplates.C @@ -40,7 +40,8 @@ Foam::HashTable Foam::objectRegistry::classesImpl const MatchPredicate& matchName ) { - HashTable summary(2*list.size()); + HashTable summary; + summary.reserve(16); // Relatively few types // Summary (key,val) = (class-name, object-names) forAllConstIters(list, iter) @@ -601,9 +602,7 @@ const Type& Foam::objectRegistry::lookupObject const bool recursive ) const { - const_iterator iter = cfind(name); - - if (iter.good()) + if (auto iter = cfind(name); iter.good()) { const Type* ptr = dynamic_cast(iter.val()); diff --git a/src/meshTools/regionModel/regionProperties/regionProperties.C b/src/meshTools/regionModel/regionProperties/regionProperties.C index 0e82006feb..defd4b48f9 100644 --- a/src/meshTools/regionModel/regionProperties/regionProperties.C +++ b/src/meshTools/regionModel/regionProperties/regionProperties.C @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2012 OpenFOAM Foundation - Copyright (C) 2018-2022 OpenCFD Ltd. + Copyright (C) 2018-2025 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -32,15 +32,10 @@ License // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // -Foam::regionProperties::regionProperties(const Time& runTime) -: - regionProperties(runTime, IOobject::MUST_READ_IF_MODIFIED) -{} - - Foam::regionProperties::regionProperties ( const Time& runTime, + const fileName& local, IOobjectOption::readOption rOpt ) { @@ -52,19 +47,28 @@ Foam::regionProperties::regionProperties ( "regionProperties", runTime.time().constant(), + local, runTime.db(), rOpt, - IOobjectOption::NO_WRITE + IOobjectOption::NO_WRITE, + IOobjectOption::NO_REGISTER ) ); - if (IOobjectOption::isReadRequired(rOpt) || iodict.size()) - { - iodict.readEntry("regions", props); - } + iodict.readEntry("regions", props, keyType::LITERAL, rOpt); } +Foam::regionProperties::regionProperties +( + const Time& runTime, + IOobjectOption::readOption rOpt +) +: + regionProperties(runTime, fileName::null, rOpt) +{} + + // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // Foam::label Foam::regionProperties::count() const @@ -90,9 +94,9 @@ Foam::wordList Foam::regionProperties::names() const const HashTable& props = *this; - for (const word& grp : props.sortedToc()) + for (const auto& iter : props.csorted()) { - for (const word& name : props[grp]) + for (const word& name : iter.val()) { list[n] = name; ++n; diff --git a/src/meshTools/regionModel/regionProperties/regionProperties.H b/src/meshTools/regionModel/regionProperties/regionProperties.H index 2f5eb67403..4bcde1dc9e 100644 --- a/src/meshTools/regionModel/regionProperties/regionProperties.H +++ b/src/meshTools/regionModel/regionProperties/regionProperties.H @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2015 OpenFOAM Foundation - Copyright (C) 2018-2022 OpenCFD Ltd. + Copyright (C) 2018-2025 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -34,6 +34,17 @@ Description is no assumption on this level that a region should only have a single set of physics. + Uses the "regions" table from the constant/regionProperties file. + For example, + + \verbatim + regions + ( + fluid (air water) + solid (walls) + ); + \endverbatim + SourceFiles regionProperties.C @@ -43,8 +54,9 @@ SourceFiles #define Foam_regionProperties_H #include "HashTable.H" +#include "fileName.H" #include "wordList.H" -#include "IOobject.H" +#include "IOobjectOption.H" // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // @@ -66,14 +78,21 @@ public: // Constructors - //- Construct from Time - explicit regionProperties(const Time& runTime); - //- Construct from Time with specified read option + //- (default: MUST_READ) + explicit regionProperties + ( + const Time& runTime, + IOobjectOption::readOption rOpt = IOobjectOption::MUST_READ + ); + + //- Construct from Time and local (prefix) with specified read option + //- (default: MUST_READ) regionProperties ( const Time& runTime, - IOobjectOption::readOption rOpt + const fileName& local, + IOobjectOption::readOption rOpt = IOobjectOption::MUST_READ ); @@ -91,7 +110,6 @@ public: //- The region names in sorted order. wordList sortedNames() const; - };