From fa71840d8ba460ba5fc4d43a90bb8f745c2169c9 Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Mon, 27 Jul 2020 07:51:10 +0200 Subject: [PATCH] ENH: add get() retrieval of a pointer from PtrLists, HashPtrTable - naming similarity with autoPtr, unique_ptr and other containers. For UPtrList derivatives, this is equivalent to the existing operator(). The read-only variant is also equivalent to the single-parameter 'set(label)' method. With PtrList list(...) : const T* ptr = list.get(10); if (ptr) { ptr->method(); } vs. if (list.set(10)) { list[10].method(); } For HashPtrTable there is only a read-only variant which is equivalent to testing for existence and for value. With HashPtrTable hash(...) : const T* ptr = list.get("key"); if (ptr) { ptr->method(); } vs. if (list.found("key")) { // Fails on null pointer!! list["key"].method(); } Use of get() is largely a matter of taste or local coding requirements --- .../test/HashPtrTable/Test-HashPtrTable.C | 18 ++++++++++++++++++ .../HashTables/HashPtrTable/HashPtrTable.H | 7 +++++++ .../HashTables/HashPtrTable/HashPtrTableI.H | 12 ++++++++++++ .../PtrLists/PtrDynList/PtrDynList.H | 6 +++++- .../PtrLists/PtrDynList/PtrDynListI.H | 2 +- .../containers/PtrLists/PtrList/PtrList.H | 4 ++-- .../containers/PtrLists/UPtrList/UPtrList.H | 12 ++++++++++-- .../containers/PtrLists/UPtrList/UPtrListI.H | 14 ++++++++++++++ 8 files changed, 69 insertions(+), 6 deletions(-) diff --git a/applications/test/HashPtrTable/Test-HashPtrTable.C b/applications/test/HashPtrTable/Test-HashPtrTable.C index 1003b0b992..a7ad4bc2f1 100644 --- a/applications/test/HashPtrTable/Test-HashPtrTable.C +++ b/applications/test/HashPtrTable/Test-HashPtrTable.C @@ -198,6 +198,24 @@ int main() Info<< "Table: " << tbl << nl; + Info<< nl << "Check exists, non-null" << nl; + + for (const word& k : { "abc", "foo", "pi" }) + { + Info<< " " << k << ' '; + + const auto* inspect = tbl.get(k); + + if (inspect) + { + Info<< *inspect << nl; + } + else + { + Info<< "(null)" << nl; + } + } + Info<< nl << "... overwrite again" << nl; tbl.set("abc", new Scalar(42.1)); diff --git a/src/OpenFOAM/containers/HashTables/HashPtrTable/HashPtrTable.H b/src/OpenFOAM/containers/HashTables/HashPtrTable/HashPtrTable.H index bd43a25530..d2f17d0951 100644 --- a/src/OpenFOAM/containers/HashTables/HashPtrTable/HashPtrTable.H +++ b/src/OpenFOAM/containers/HashTables/HashPtrTable/HashPtrTable.H @@ -120,6 +120,13 @@ public: // Member Functions + // Access + + //- Return const pointer associated with given entry, + //- returning a nullptr if the key does not exist in the table. + inline const T* get(const Key& key) const; + + // Edit //- Remove entry specified by given iterator. diff --git a/src/OpenFOAM/containers/HashTables/HashPtrTable/HashPtrTableI.H b/src/OpenFOAM/containers/HashTables/HashPtrTable/HashPtrTableI.H index d15e8bf50f..b0d04f0197 100644 --- a/src/OpenFOAM/containers/HashTables/HashPtrTable/HashPtrTableI.H +++ b/src/OpenFOAM/containers/HashTables/HashPtrTable/HashPtrTableI.H @@ -45,6 +45,18 @@ inline Foam::HashPtrTable::HashPtrTable(const label size) // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // +template +inline const T* Foam::HashPtrTable::get(const Key& key) const +{ + const const_iterator iter(this->cfind(key)); + if (iter.good()) + { + return iter.val(); + } + return nullptr; +} + + template template inline bool Foam::HashPtrTable::emplace diff --git a/src/OpenFOAM/containers/PtrLists/PtrDynList/PtrDynList.H b/src/OpenFOAM/containers/PtrLists/PtrDynList/PtrDynList.H index d30b06301a..9739690e62 100644 --- a/src/OpenFOAM/containers/PtrLists/PtrDynList/PtrDynList.H +++ b/src/OpenFOAM/containers/PtrLists/PtrDynList/PtrDynList.H @@ -108,10 +108,14 @@ public: //- Size of the underlying storage. inline label capacity() const noexcept; + //- Return const pointer to element (can be nullptr), + // with bounds checking. + inline const T* get(const label i) const; + //- Return const pointer to element (if set) or nullptr, // with bounds checking. // The return value can be tested as a bool. - inline const T* set(const label i) const; + const T* set(const label i) const { return this->get(i); } // Edit diff --git a/src/OpenFOAM/containers/PtrLists/PtrDynList/PtrDynListI.H b/src/OpenFOAM/containers/PtrLists/PtrDynList/PtrDynListI.H index a75bbff861..ab8dc7a2f8 100644 --- a/src/OpenFOAM/containers/PtrLists/PtrDynList/PtrDynListI.H +++ b/src/OpenFOAM/containers/PtrLists/PtrDynList/PtrDynListI.H @@ -100,7 +100,7 @@ inline Foam::label Foam::PtrDynList::capacity() const noexcept template -inline const T* Foam::PtrDynList::set(const label i) const +inline const T* Foam::PtrDynList::get(const label i) const { return (i >= 0 && i < PtrList::size()) ? PtrList::get(i) : nullptr; } diff --git a/src/OpenFOAM/containers/PtrLists/PtrList/PtrList.H b/src/OpenFOAM/containers/PtrLists/PtrList/PtrList.H index 494eccd484..cc5cb25af0 100644 --- a/src/OpenFOAM/containers/PtrLists/PtrList/PtrList.H +++ b/src/OpenFOAM/containers/PtrLists/PtrList/PtrList.H @@ -134,9 +134,9 @@ public: // Access //- Return const pointer to element (can be nullptr), - // without bounds checking. + // without bounds checking - same as get(). // The return value can also be tested as a bool. - const T* set(const label i) const { return UPtrList::set(i); } + const T* set(const label i) const { return this->get(i); } // Edit diff --git a/src/OpenFOAM/containers/PtrLists/UPtrList/UPtrList.H b/src/OpenFOAM/containers/PtrLists/UPtrList/UPtrList.H index 4dd0662854..b338bf16ff 100644 --- a/src/OpenFOAM/containers/PtrLists/UPtrList/UPtrList.H +++ b/src/OpenFOAM/containers/PtrLists/UPtrList/UPtrList.H @@ -157,10 +157,18 @@ public: //- Return reference to the last element of the list inline const T& last() const; + //- Return pointer to element (can be nullptr), + // without bounds checking. + inline T* get(const label i); + //- Return const pointer to element (can be nullptr), // without bounds checking. + inline const T* get(const label i) const; + + //- Return const pointer to element (can be nullptr), + // without bounds checking - same as get(). // The return value can also be tested as a bool. - const T* set(const label i) const { return ptrs_[i]; } + const T* set(const label i) const { return this->get(i); } // Edit @@ -212,7 +220,7 @@ public: //- Return reference to the element inline T& operator[](const label i); - //- Return const pointer to the element. + //- Return const pointer to the element - same as get(). inline const T* operator()(const label i) const; //- Copy assignment (shallow copies addresses) diff --git a/src/OpenFOAM/containers/PtrLists/UPtrList/UPtrListI.H b/src/OpenFOAM/containers/PtrLists/UPtrList/UPtrListI.H index f3923aab74..816b435d62 100644 --- a/src/OpenFOAM/containers/PtrLists/UPtrList/UPtrListI.H +++ b/src/OpenFOAM/containers/PtrLists/UPtrList/UPtrListI.H @@ -107,6 +107,20 @@ inline bool Foam::UPtrList::empty() const noexcept } +template +inline T* Foam::UPtrList::get(const label i) +{ + return ptrs_[i]; +} + + +template +inline const T* Foam::UPtrList::get(const label i) const +{ + return ptrs_[i]; +} + + template inline void Foam::UPtrList::clear() {