diff --git a/applications/test/HashPtrTable/Test-HashPtrTable.C b/applications/test/HashPtrTable/Test-HashPtrTable.C index 831ab9557ef9d5942ff16bba6d52332538be1514..43675e68d36ceb24e8aeff84c226a8ac567373ba 100644 --- a/applications/test/HashPtrTable/Test-HashPtrTable.C +++ b/applications/test/HashPtrTable/Test-HashPtrTable.C @@ -42,7 +42,7 @@ void printTable(const HashPtrTable<T>& table) forAllConstIters(table, iter) { - const T* ptr = iter.val(); + const auto& ptr = iter.val(); Info<< iter.key() << " = "; if (ptr) { diff --git a/src/OpenFOAM/containers/HashTables/HashPtrTable/HashPtrTable.C b/src/OpenFOAM/containers/HashTables/HashPtrTable/HashPtrTable.C index f318b9dd22b594dd717b537bf363f898708b3d2a..162fa67e5c33166545007841b39993f76e1cac07 100644 --- a/src/OpenFOAM/containers/HashTables/HashPtrTable/HashPtrTable.C +++ b/src/OpenFOAM/containers/HashTables/HashPtrTable/HashPtrTable.C @@ -42,14 +42,14 @@ Foam::HashPtrTable<T, Key, Hash>::HashPtrTable { for (const_iterator iter = ht.begin(); iter != ht.end(); ++iter) { - const T* ptr = iter.val(); + const auto& ptr = iter.val(); if (ptr) { - this->set(iter.key(), new T(*ptr)); + parent_type::set(iter.key(), autoPtr<T>(new T(*ptr))); } else { - this->set(iter.key(), nullptr); + parent_type::set(iter.key(), autoPtr<T>()); } } } @@ -65,15 +65,6 @@ Foam::HashPtrTable<T, Key, Hash>::HashPtrTable {} -// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * // - -template<class T, class Key, class Hash> -Foam::HashPtrTable<T, Key, Hash>::~HashPtrTable() -{ - clear(); -} - - // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // template<class T, class Key, class Hash> @@ -81,8 +72,9 @@ Foam::autoPtr<T> Foam::HashPtrTable<T, Key, Hash>::remove(iterator& iter) { if (iter.good()) { - autoPtr<T> aptr(iter.val()); - this->parent_type::erase(iter); + autoPtr<T> aptr(iter.val().release()); + parent_type::erase(iter); + return aptr; } @@ -93,53 +85,11 @@ Foam::autoPtr<T> Foam::HashPtrTable<T, Key, Hash>::remove(iterator& iter) template<class T, class Key, class Hash> Foam::autoPtr<T> Foam::HashPtrTable<T, Key, Hash>::remove(const Key& key) { - auto iter = this->find(key); + iterator iter(this->find(key)); return this->remove(iter); } -template<class T, class Key, class Hash> -bool Foam::HashPtrTable<T, Key, Hash>::erase(iterator& iter) -{ - if (iter.good()) - { - T* ptr = iter.val(); - - if (this->parent_type::erase(iter)) - { - if (ptr) - { - delete ptr; - } - - return true; - } - } - - return false; -} - - -template<class T, class Key, class Hash> -bool Foam::HashPtrTable<T, Key, Hash>::erase(const Key& key) -{ - auto iter = this->find(key); - return this->erase(iter); -} - - -template<class T, class Key, class Hash> -void Foam::HashPtrTable<T, Key, Hash>::clear() -{ - for (iterator iter = this->begin(); iter != this->end(); ++iter) - { - delete iter.val(); - } - - this->parent_type::clear(); -} - - // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * // template<class T, class Key, class Hash> @@ -153,18 +103,18 @@ void Foam::HashPtrTable<T, Key, Hash>::operator= return; // Self-assignment is a no-op } - this->clear(); + parent_type::clear(); for (const_iterator iter = rhs.begin(); iter != rhs.end(); ++iter) { - const T* ptr = iter.val(); + const auto& ptr = iter.val(); if (ptr) { - this->set(iter.key(), new T(*ptr)); + parent_type::set(iter.key(), autoPtr<T>(new T(*ptr))); } else { - this->set(iter.key(), nullptr); + parent_type::set(iter.key(), autoPtr<T>()); } } } @@ -181,8 +131,8 @@ void Foam::HashPtrTable<T, Key, Hash>::operator= return; // Self-assignment is a no-op } - this->clear(); - this->transfer(rhs); + parent_type::clear(); + parent_type::transfer(rhs); } diff --git a/src/OpenFOAM/containers/HashTables/HashPtrTable/HashPtrTable.H b/src/OpenFOAM/containers/HashTables/HashPtrTable/HashPtrTable.H index e666c6a8b5be5b1b9a9fab723d95a427c6593fc4..2a4a7c140572cbe491b5bcf18d78689148ab4870 100644 --- a/src/OpenFOAM/containers/HashTables/HashPtrTable/HashPtrTable.H +++ b/src/OpenFOAM/containers/HashTables/HashPtrTable/HashPtrTable.H @@ -7,7 +7,7 @@ ------------------------------------------------------------------------------- Released 2004-2011 OpenCFD Ltd. Copyright (C) 2011-2016 OpenFOAM Foundation - Modified code Copyright (C) 2017-2018 OpenCFD Ltd. + Modified code Copyright (C) 2017-2019 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -29,9 +29,16 @@ Class Foam::HashPtrTable Description - A HashTable of pointers to objects of type \<T\>. + A HashTable of pointers to objects of type \<T\>, + using an autoPtr for memory management. + +Note + Uses autoPtr instead of std::unique_ptr for the memory management, + primarily for its cast to pointer methods that allow for backwards + compatible callers. SourceFiles + HashPtrTableI.H HashPtrTable.C HashPtrTableIO.C @@ -40,6 +47,7 @@ SourceFiles #ifndef HashPtrTable_H #define HashPtrTable_H +#include "autoPtr.H" #include "HashTable.H" // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // @@ -47,12 +55,10 @@ SourceFiles namespace Foam { -// Forward declarations - +// Forward Declarations class Istream; class Ostream; -template<class T> class autoPtr; template<class T, class Key, class Hash> class HashPtrTable; template<class T, class Key, class Hash> @@ -66,7 +72,7 @@ Istream& operator>>(Istream& is, HashPtrTable<T, Key, Hash>& tbl); template<class T, class Key=word, class Hash=string::hash> class HashPtrTable : - public HashTable<T*, Key, Hash> + public HashTable<autoPtr<T>, Key, Hash> { // Private Member Functions @@ -85,7 +91,7 @@ public: typedef HashPtrTable<T, Key, Hash> this_type; //- The template instance used for the parent HashTable - typedef HashTable<T*, Key, Hash> parent_type; + typedef HashTable<autoPtr<T>, Key, Hash> parent_type; using iterator = typename parent_type::iterator; using const_iterator = typename parent_type::const_iterator; @@ -116,41 +122,17 @@ public: HashPtrTable(this_type&& ht); - //- Destructor - ~HashPtrTable(); - - // Member Functions // Edit - //- Remove entry specified by given iterator. - // Includes a safeguard against the end-iterator. - // - // \return entry as an encapsulated pointer. + //- Remove entry specified by given iterator and return as autoPtr + // Includes an end-iterator safeguard. autoPtr<T> remove(iterator& iter); - //- Remove entry specified by given key. - // - // \return entry as an encapsulated pointer. + //- Remove entry specified by given key and return as autoPtr autoPtr<T> remove(const Key& key); - //- Erase entry specified by given iterator and delete the - //- allocated pointer. - // Includes a safeguard against the end-iterator. - // - // \return True if item was removed - bool erase(iterator& iter); - - //- Erase entry specified by given key and delete the - //- allocated pointer. - // - // \return True if item was removed - bool erase(const Key& key); - - //- Clear all entries from table and delete any allocated pointers - void clear(); - //- Write void write(Ostream& os) const; @@ -175,28 +157,27 @@ public: // Housekeeping - //- No insert() with raw pointers (potential memory leaks). - //- Use insert() with autoPtr or set() - inline bool insert(const Key&, T*) = delete; + //- Insert a new entry, not overwriting existing entries. + // Memory is freed if the insert fails. + // \return True if the entry inserted (not previously in table) + inline bool insert(const Key& key, T* ptr); //- Insert a new entry, not overwriting existing entries. - // // \return True if the entry inserted (not previously in table) - inline bool insert(const Key& key, autoPtr<T>& aptr); + inline bool insert(const Key& key, autoPtr<T>& ptr); //- Insert a new entry, not overwriting existing entries. - // // \return True if the entry inserted (not previously in table) - inline bool insert(const Key& key, autoPtr<T>&& aptr); + inline bool insert(const Key& key, autoPtr<T>&& ptr); //- Assign a new entry, overwriting existing entries. inline bool set(const Key& key, T* ptr); //- Assign a new entry, overwriting existing entries. - inline bool set(const Key& key, autoPtr<T>& aptr); + inline bool set(const Key& key, autoPtr<T>& ptr); //- Assign a new entry, overwriting existing entries. - inline bool set(const Key& key, autoPtr<T>&& aptr); + inline bool set(const Key& key, autoPtr<T>&& ptr); }; diff --git a/src/OpenFOAM/containers/HashTables/HashPtrTable/HashPtrTableI.H b/src/OpenFOAM/containers/HashTables/HashPtrTable/HashPtrTableI.H index 5c76321ab6354e74bb2eea845176cb4dbd60162b..308f20f7cd33d41d23e7476202a4a5ad9fcb5117 100644 --- a/src/OpenFOAM/containers/HashTables/HashPtrTable/HashPtrTableI.H +++ b/src/OpenFOAM/containers/HashTables/HashPtrTable/HashPtrTableI.H @@ -5,7 +5,7 @@ \\ / A nd | www.openfoam.com \\/ M anipulation | ------------------------------------------------------------------------------- - Copyright (C) 2018 OpenCFD Ltd. + Copyright (C) 2018-2019 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -49,16 +49,10 @@ template<class T, class Key, class Hash> inline bool Foam::HashPtrTable<T, Key, Hash>::insert ( const Key& key, - autoPtr<T>& aptr + T* ptr ) { - if (parent_type::insert(key, aptr.get())) - { - aptr.release(); // Now owned by HashPtrTable - return true; - } - - return false; + return parent_type::insert(key, autoPtr<T>(ptr)); } @@ -66,16 +60,21 @@ template<class T, class Key, class Hash> inline bool Foam::HashPtrTable<T, Key, Hash>::insert ( const Key& key, - autoPtr<T>&& aptr + autoPtr<T>& ptr ) { - if (parent_type::insert(key, aptr.get())) - { - aptr.release(); // Now owned by HashPtrTable - return true; - } + return parent_type::insert(key, std::move(ptr)); +} - return false; + +template<class T, class Key, class Hash> +inline bool Foam::HashPtrTable<T, Key, Hash>::insert +( + const Key& key, + autoPtr<T>&& ptr +) +{ + return parent_type::insert(key, std::move(ptr)); } @@ -86,7 +85,7 @@ inline bool Foam::HashPtrTable<T, Key, Hash>::set T* ptr ) { - return this->parent_type::set(key, ptr); + return parent_type::set(key, autoPtr<T>(ptr)); } @@ -94,10 +93,10 @@ template<class T, class Key, class Hash> inline bool Foam::HashPtrTable<T, Key, Hash>::set ( const Key& key, - autoPtr<T>& aptr + autoPtr<T>& ptr ) { - return this->set(key, aptr.release()); + return parent_type::set(key, std::move(ptr)); } @@ -105,10 +104,10 @@ template<class T, class Key, class Hash> inline bool Foam::HashPtrTable<T, Key, Hash>::set ( const Key& key, - autoPtr<T>&& aptr + autoPtr<T>&& ptr ) { - return this->set(key, aptr.release()); + return parent_type::set(key, std::move(ptr)); } diff --git a/src/OpenFOAM/containers/HashTables/HashPtrTable/HashPtrTableIO.C b/src/OpenFOAM/containers/HashTables/HashPtrTable/HashPtrTableIO.C index 4fe3a79cdaec11740dfe5b8049b9bac6c67de71c..03ae32bfbad5ba1673f6c04b2d1046062a89c1de 100644 --- a/src/OpenFOAM/containers/HashTables/HashPtrTable/HashPtrTableIO.C +++ b/src/OpenFOAM/containers/HashTables/HashPtrTable/HashPtrTableIO.C @@ -157,10 +157,11 @@ void Foam::HashPtrTable<T, Key, Hash>::write(Ostream& os) const { for (const_iterator iter = this->cbegin(); iter != this->cend(); ++iter) { - const T* ptr = iter.val(); - if (ptr) + const auto& uptr = iter.val(); + + if (uptr) { - ptr->write(os); + uptr->write(os); } } } @@ -193,7 +194,11 @@ Foam::HashPtrTable<T, Key, Hash>::HashPtrTable(const dictionary& dict) // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * // template<class T, class Key, class Hash> -Foam::Istream& Foam::operator>>(Istream& is, HashPtrTable<T, Key, Hash>& tbl) +Foam::Istream& Foam::operator>> +( + Istream& is, + HashPtrTable<T, Key, Hash>& tbl +) { tbl.clear(); tbl.readIstream(is, INew<T>());