mirror of
https://develop.openfoam.com/Development/openfoam.git
synced 2025-11-28 03:28:01 +00:00
ENH: add HashPtrTable release method
- effectively 'steals' the pointer from the table but leaves its name as a placeholder
This commit is contained in:
@ -6,7 +6,7 @@
|
|||||||
\\/ M anipulation |
|
\\/ M anipulation |
|
||||||
-------------------------------------------------------------------------------
|
-------------------------------------------------------------------------------
|
||||||
Copyright (C) 2011 OpenFOAM Foundation
|
Copyright (C) 2011 OpenFOAM Foundation
|
||||||
Copyright (C) 2017-2020 OpenCFD Ltd.
|
Copyright (C) 2017-2021 OpenCFD Ltd.
|
||||||
-------------------------------------------------------------------------------
|
-------------------------------------------------------------------------------
|
||||||
License
|
License
|
||||||
This file is part of OpenFOAM.
|
This file is part of OpenFOAM.
|
||||||
@ -125,6 +125,7 @@ int main()
|
|||||||
myTable.set("pi", new double(3.14159));
|
myTable.set("pi", new double(3.14159));
|
||||||
myTable.set("natlog", new double(2.718282));
|
myTable.set("natlog", new double(2.718282));
|
||||||
myTable.insert("sqrt2", autoPtr<double>::New(1.414214));
|
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;
|
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.set("natlog", autoPtr<double>(new double(2.718282)));
|
||||||
myTable2.insert("sqrt2", autoPtr<double>::New(1.414214));
|
myTable2.insert("sqrt2", autoPtr<double>::New(1.414214));
|
||||||
|
|
||||||
// Info<< myTable << endl;
|
Info<< "Initial table" << nl;
|
||||||
printTable(myTable);
|
printTable(myTable);
|
||||||
|
|
||||||
|
Info<< "print" << nl;
|
||||||
Info<< myTable2 << nl;
|
Info<< myTable2 << nl;
|
||||||
|
|
||||||
|
{
|
||||||
auto iter2 = myTable2.find("pi");
|
auto iter2 = myTable2.find("pi");
|
||||||
|
Info<< nl "Got pi=";
|
||||||
Info<<"PI: " << **iter2 << nl;
|
if (iter2.good())
|
||||||
|
{
|
||||||
|
Info<< **iter2 << nl;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Info<< "not-found" << nl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
HashPtrTable<double> copy(myTable);
|
HashPtrTable<double> copy(myTable);
|
||||||
|
|
||||||
@ -165,6 +176,9 @@ int main()
|
|||||||
myTable.erase("abc");
|
myTable.erase("abc");
|
||||||
myTable.erase("unknownKey");
|
myTable.erase("unknownKey");
|
||||||
|
|
||||||
|
myTable.release("euler");
|
||||||
|
|
||||||
|
Info<< "After erasure" << nl;
|
||||||
printTable(myTable);
|
printTable(myTable);
|
||||||
|
|
||||||
HashPtrTable<double> moved(std::move(copy));
|
HashPtrTable<double> moved(std::move(copy));
|
||||||
|
|||||||
@ -6,7 +6,7 @@
|
|||||||
\\/ M anipulation |
|
\\/ M anipulation |
|
||||||
-------------------------------------------------------------------------------
|
-------------------------------------------------------------------------------
|
||||||
Copyright (C) 2011-2016 OpenFOAM Foundation
|
Copyright (C) 2011-2016 OpenFOAM Foundation
|
||||||
Copyright (C) 2017-2020 OpenCFD Ltd.
|
Copyright (C) 2017-2021 OpenCFD Ltd.
|
||||||
-------------------------------------------------------------------------------
|
-------------------------------------------------------------------------------
|
||||||
License
|
License
|
||||||
This file is part of OpenFOAM.
|
This file is part of OpenFOAM.
|
||||||
@ -26,8 +26,9 @@ License
|
|||||||
|
|
||||||
\*---------------------------------------------------------------------------*/
|
\*---------------------------------------------------------------------------*/
|
||||||
|
|
||||||
#include "error.H"
|
|
||||||
#include "HashPtrTable.H"
|
#include "HashPtrTable.H"
|
||||||
|
#include "autoPtr.H"
|
||||||
|
#include "error.H"
|
||||||
|
|
||||||
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
|
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
|
||||||
|
|
||||||
@ -77,6 +78,28 @@ Foam::HashPtrTable<T, Key, Hash>::~HashPtrTable()
|
|||||||
|
|
||||||
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
|
// * * * * * * * * * * * * * * * 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>
|
template<class T, class Key, class Hash>
|
||||||
Foam::autoPtr<T> Foam::HashPtrTable<T, Key, Hash>::remove(iterator& iter)
|
Foam::autoPtr<T> Foam::HashPtrTable<T, Key, Hash>::remove(iterator& iter)
|
||||||
{
|
{
|
||||||
|
|||||||
@ -6,7 +6,7 @@
|
|||||||
\\/ M anipulation |
|
\\/ M anipulation |
|
||||||
-------------------------------------------------------------------------------
|
-------------------------------------------------------------------------------
|
||||||
Copyright (C) 2011-2016 OpenFOAM Foundation
|
Copyright (C) 2011-2016 OpenFOAM Foundation
|
||||||
Copyright (C) 2017-2020 OpenCFD Ltd.
|
Copyright (C) 2017-2021 OpenCFD Ltd.
|
||||||
-------------------------------------------------------------------------------
|
-------------------------------------------------------------------------------
|
||||||
License
|
License
|
||||||
This file is part of OpenFOAM.
|
This file is part of OpenFOAM.
|
||||||
@ -93,7 +93,7 @@ public:
|
|||||||
// Constructors
|
// Constructors
|
||||||
|
|
||||||
//- Default construct with default table capacity
|
//- Default construct with default table capacity
|
||||||
inline HashPtrTable();
|
HashPtrTable() = default;
|
||||||
|
|
||||||
//- Construct given initial table capacity
|
//- Construct given initial table capacity
|
||||||
inline explicit HashPtrTable(const label size);
|
inline explicit HashPtrTable(const label size);
|
||||||
@ -130,15 +130,26 @@ public:
|
|||||||
|
|
||||||
// Edit
|
// 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.
|
//- Remove entry specified by given iterator.
|
||||||
// Includes a safeguard against the end-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);
|
autoPtr<T> remove(iterator& iter);
|
||||||
|
|
||||||
//- Remove entry specified by given key.
|
//- 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);
|
autoPtr<T> remove(const Key& key);
|
||||||
|
|
||||||
//- Erase entry specified by given iterator and delete the
|
//- Erase entry specified by given iterator and delete the
|
||||||
|
|||||||
@ -29,13 +29,6 @@ License
|
|||||||
|
|
||||||
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
|
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
|
||||||
|
|
||||||
template<class T, class Key, class Hash>
|
|
||||||
inline Foam::HashPtrTable<T, Key, Hash>::HashPtrTable()
|
|
||||||
:
|
|
||||||
parent_type()
|
|
||||||
{}
|
|
||||||
|
|
||||||
|
|
||||||
template<class T, class Key, class Hash>
|
template<class T, class Key, class Hash>
|
||||||
inline Foam::HashPtrTable<T, Key, Hash>::HashPtrTable(const label size)
|
inline Foam::HashPtrTable<T, Key, Hash>::HashPtrTable(const label size)
|
||||||
:
|
:
|
||||||
|
|||||||
Reference in New Issue
Block a user