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 OpenFOAM Foundation
Copyright (C) 2017-2020 OpenCFD Ltd.
Copyright (C) 2017-2021 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -125,6 +125,7 @@ int main()
myTable.set("pi", new double(3.14159));
myTable.set("natlog", new double(2.718282));
myTable.insert("sqrt2", autoPtr<double>::New(1.414214));
myTable.insert("euler", autoPtr<double>::New(0.577216));
HashTable<std::unique_ptr<double>, word, string::hash> myTable1;
@ -139,14 +140,24 @@ int main()
myTable2.set("natlog", autoPtr<double>(new double(2.718282)));
myTable2.insert("sqrt2", autoPtr<double>::New(1.414214));
// Info<< myTable << endl;
Info<< "Initial table" << nl;
printTable(myTable);
Info<< "print" << nl;
Info<< myTable2 << nl;
auto iter2 = myTable2.find("pi");
Info<<"PI: " << **iter2 << nl;
{
auto iter2 = myTable2.find("pi");
Info<< nl "Got pi=";
if (iter2.good())
{
Info<< **iter2 << nl;
}
else
{
Info<< "not-found" << nl;
}
}
HashPtrTable<double> copy(myTable);
@ -165,6 +176,9 @@ int main()
myTable.erase("abc");
myTable.erase("unknownKey");
myTable.release("euler");
Info<< "After erasure" << nl;
printTable(myTable);
HashPtrTable<double> moved(std::move(copy));

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)
: