diff --git a/applications/test/HashPtrTable/Test-HashPtrTable.C b/applications/test/HashPtrTable/Test-HashPtrTable.C index 2c5b9d0912d71a1a63e6fc7ef05e822c232150ec..567d1b0efcb0ef294aa56661cce377e8b07d4d50 100644 --- a/applications/test/HashPtrTable/Test-HashPtrTable.C +++ b/applications/test/HashPtrTable/Test-HashPtrTable.C @@ -41,11 +41,11 @@ void printTable(const HashPtrTable<T>& table) forAllConstIters(table, iter) { - const T* ptr = iter.val(); + const auto& ptr = iter.val(); Info<< iter.key() << " = "; if (ptr) { - Info<< *ptr << " (" << uintptr_t(ptr) << ")"; + Info<< *ptr << " (" << uintptr_t(ptr.get()) << ")"; } else { @@ -56,14 +56,14 @@ void printTable(const HashPtrTable<T>& table) Info<< ")" << endl; - // Values only, with for-range + // Iterate across values, with for-range Info<< "values ("; - for (const auto& val : table) + for (const auto& ptr : table) { Info<< ' '; - if (val) + if (ptr) { - Info<< *val; + Info<< *ptr; } else { diff --git a/src/OpenFOAM/containers/HashTables/HashPtrTable/HashPtrTable.C b/src/OpenFOAM/containers/HashTables/HashPtrTable/HashPtrTable.C index dac7a7af50b14389872c595ff5c083adeccc3d54..3672148403c9e264477931500890ab0140c1ac03 100644 --- a/src/OpenFOAM/containers/HashTables/HashPtrTable/HashPtrTable.C +++ b/src/OpenFOAM/containers/HashTables/HashPtrTable/HashPtrTable.C @@ -40,14 +40,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>()); } } } @@ -63,15 +63,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> @@ -79,8 +70,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; } @@ -91,53 +83,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= << abort(FatalError); } - 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>()); } } } @@ -183,8 +133,8 @@ void Foam::HashPtrTable<T, Key, Hash>::operator= << abort(FatalError); } - 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 030557c9ecb500a647bca21775d729c08587da16..01341cb96c9af2fe310f6dbfe9a031666ac9741a 100644 --- a/src/OpenFOAM/containers/HashTables/HashPtrTable/HashPtrTable.H +++ b/src/OpenFOAM/containers/HashTables/HashPtrTable/HashPtrTable.H @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | Copyright (C) 2004-2010, 2017-2018 OpenCFD Ltd. + \\ / A nd | Copyright (C) 2004-2010, 2017-2019 OpenCFD Ltd. \\/ M anipulation | ------------------------------------------------------------------------------- | Copyright (C) 2011-2016 OpenFOAM Foundation @@ -27,9 +27,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 @@ -38,6 +45,7 @@ SourceFiles #ifndef HashPtrTable_H #define HashPtrTable_H +#include "autoPtr.H" #include "HashTable.H" // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // @@ -45,12 +53,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> @@ -64,7 +70,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 @@ -83,7 +89,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; @@ -114,41 +120,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; @@ -173,28 +155,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 9d5a4ad0b2e64d212ddc76a9d37b7469eda90a57..468d15ec075d18237eba2bfc7baea8d55bf403f7 100644 --- a/src/OpenFOAM/containers/HashTables/HashPtrTable/HashPtrTableI.H +++ b/src/OpenFOAM/containers/HashTables/HashPtrTable/HashPtrTableI.H @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | Copyright (C) 2018 OpenCFD Ltd. + \\ / A nd | Copyright (C) 2018-2019 OpenCFD Ltd. \\/ M anipulation | ------------------------------------------------------------------------------- License @@ -47,16 +47,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)); } @@ -64,16 +58,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)); } @@ -84,7 +83,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)); } @@ -92,10 +91,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)); } @@ -103,10 +102,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 10c72d3102f36c5f8c854e5bed2281836820e9a1..fe4d2560008a8164c59d3499f6c52346ce0d7e5e 100644 --- a/src/OpenFOAM/containers/HashTables/HashPtrTable/HashPtrTableIO.C +++ b/src/OpenFOAM/containers/HashTables/HashPtrTable/HashPtrTableIO.C @@ -155,10 +155,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); } } } @@ -191,7 +192,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>());