ENH: add HashPtrTable release method

- effectively 'steals' the pointer from the table but leaves its
  name as a placeholder
This commit is contained in:
Mark Olesen
2021-02-16 10:57:52 +01:00
parent f8a0677a66
commit b97cd5c380
4 changed files with 60 additions and 19 deletions

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2017-2020 OpenCFD Ltd.
Copyright (C) 2017-2021 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -26,8 +26,9 @@ License
\*---------------------------------------------------------------------------*/
#include "error.H"
#include "HashPtrTable.H"
#include "autoPtr.H"
#include "error.H"
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
@ -77,6 +78,28 @@ Foam::HashPtrTable<T, Key, Hash>::~HashPtrTable()
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
template<class T, class Key, class Hash>
Foam::autoPtr<T> Foam::HashPtrTable<T, Key, Hash>::release(iterator& iter)
{
if (iter.good())
{
autoPtr<T> old(iter.val());
iter.val() = nullptr;
return old;
}
return nullptr;
}
template<class T, class Key, class Hash>
Foam::autoPtr<T> Foam::HashPtrTable<T, Key, Hash>::release(const Key& key)
{
iterator iter(this->find(key));
return this->release(iter);
}
template<class T, class Key, class Hash>
Foam::autoPtr<T> Foam::HashPtrTable<T, Key, Hash>::remove(iterator& iter)
{

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2017-2020 OpenCFD Ltd.
Copyright (C) 2017-2021 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -93,7 +93,7 @@ public:
// Constructors
//- Default construct with default table capacity
inline HashPtrTable();
HashPtrTable() = default;
//- Construct given initial table capacity
inline explicit HashPtrTable(const label size);
@ -130,15 +130,26 @@ public:
// Edit
//- Release ownership of the pointer and replace with a nullptr
// Includes a safeguard against the end-iterator.
//
// \return the entry's old value as an encapsulated pointer.
autoPtr<T> release(iterator& iter);
//- Release ownership of the pointer and replace with a nullptr
//
// \return the entry's old value as an encapsulated pointer.
autoPtr<T> release(const Key& key);
//- Remove entry specified by given iterator.
// Includes a safeguard against the end-iterator.
//
// \return entry as an encapsulated pointer.
// \return the entry's old value as an encapsulated pointer.
autoPtr<T> remove(iterator& iter);
//- Remove entry specified by given key.
//
// \return entry as an encapsulated pointer.
// \return the entry's old value as an encapsulated pointer.
autoPtr<T> remove(const Key& key);
//- Erase entry specified by given iterator and delete the

View File

@ -29,13 +29,6 @@ License
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
template<class T, class Key, class Hash>
inline Foam::HashPtrTable<T, Key, Hash>::HashPtrTable()
:
parent_type()
{}
template<class T, class Key, class Hash>
inline Foam::HashPtrTable<T, Key, Hash>::HashPtrTable(const label size)
: