mirror of
https://develop.openfoam.com/Development/openfoam.git
synced 2025-12-28 03:37:59 +00:00
ENH: HashTable::emplace_set() method, HashPtrTable support for unique_ptr
- forwarding like the emplace() method, but overwriting existing
entries as required
- propagate similar changes to HashPtrTable
For example, with HashPtrTable<labelList> table(...) :
With 'insert' semantics
table.emplace("list1", 1000);
vs
if (!table.found("list1"))
{
table.set("list1", new labelList(1000));
}
or
table.insert("list1", autoPtr<labelList>::New(1000));
Note that the last example invokes an unnecessary allocation/deletion
if the insertion is unsuccessful.
With 'set' semantics:
table.emplace_set("list1", 15);
vs
table.set("list1", new labelList(15));
This commit is contained in:
@ -6,7 +6,7 @@
|
||||
\\/ M anipulation |
|
||||
-------------------------------------------------------------------------------
|
||||
Copyright (C) 2011-2016 OpenFOAM Foundation
|
||||
Copyright (C) 2017-2018 OpenCFD Ltd.
|
||||
Copyright (C) 2017-2020 OpenCFD Ltd.
|
||||
-------------------------------------------------------------------------------
|
||||
License
|
||||
This file is part of OpenFOAM.
|
||||
@ -41,6 +41,7 @@ SourceFiles
|
||||
#define HashPtrTable_H
|
||||
|
||||
#include "HashTable.H"
|
||||
#include <memory>
|
||||
|
||||
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
|
||||
|
||||
@ -172,6 +173,16 @@ public:
|
||||
|
||||
// Override HashTable methods
|
||||
|
||||
//- Emplace insert a new entry, not overwriting existing entries.
|
||||
// \return True if the entry did not previously exist in the table.
|
||||
template<class... Args>
|
||||
inline bool emplace(const Key& key, Args&&... args);
|
||||
|
||||
//- Emplace set an entry, overwriting any existing entries.
|
||||
// \return True, since it always overwrites any entries.
|
||||
template<class... Args>
|
||||
inline bool emplace_set(const Key& key, Args&&... args);
|
||||
|
||||
//- No insert() with raw pointers (potential memory leaks).
|
||||
//- Use insert() with autoPtr or set()
|
||||
inline bool insert(const Key&, T*) = delete;
|
||||
@ -186,6 +197,11 @@ public:
|
||||
// \return True if the entry inserted (not previously in table)
|
||||
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, std::unique_ptr<T>&& ptr);
|
||||
|
||||
//- Assign a new entry, overwriting existing entries.
|
||||
inline bool set(const Key& key, T* ptr);
|
||||
|
||||
@ -194,6 +210,9 @@ public:
|
||||
|
||||
//- Assign a new entry, overwriting existing entries.
|
||||
inline bool set(const Key& key, autoPtr<T>&& ptr);
|
||||
|
||||
//- Assign a new entry, overwriting existing entries.
|
||||
inline bool set(const Key& key, std::unique_ptr<T>&& ptr);
|
||||
};
|
||||
|
||||
|
||||
|
||||
@ -45,6 +45,52 @@ inline Foam::HashPtrTable<T, Key, Hash>::HashPtrTable(const label size)
|
||||
|
||||
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
|
||||
|
||||
template<class T, class Key, class Hash>
|
||||
template<class... Args>
|
||||
inline bool Foam::HashPtrTable<T, Key, Hash>::emplace
|
||||
(
|
||||
const Key& key,
|
||||
Args&&... args
|
||||
)
|
||||
{
|
||||
// Use insertion semantics
|
||||
return
|
||||
(
|
||||
!parent_type::found(key)
|
||||
&& this->parent_type::set(key, new T(std::forward<Args>(args)...))
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
template<class T, class Key, class Hash>
|
||||
template<class... Args>
|
||||
inline bool Foam::HashPtrTable<T, Key, Hash>::emplace_set
|
||||
(
|
||||
const Key& key,
|
||||
Args&&... args
|
||||
)
|
||||
{
|
||||
return this->set(key, new T(std::forward<Args>(args)...));
|
||||
}
|
||||
|
||||
|
||||
template<class T, class Key, class Hash>
|
||||
inline bool Foam::HashPtrTable<T, Key, Hash>::insert
|
||||
(
|
||||
const Key& key,
|
||||
autoPtr<T>& ptr
|
||||
)
|
||||
{
|
||||
if (parent_type::insert(key, ptr.get()))
|
||||
{
|
||||
ptr.release(); // Now owned by HashPtrTable
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
template<class T, class Key, class Hash>
|
||||
inline bool Foam::HashPtrTable<T, Key, Hash>::insert
|
||||
(
|
||||
@ -66,7 +112,7 @@ template<class T, class Key, class Hash>
|
||||
inline bool Foam::HashPtrTable<T, Key, Hash>::insert
|
||||
(
|
||||
const Key& key,
|
||||
autoPtr<T>&& ptr
|
||||
std::unique_ptr<T>&& ptr
|
||||
)
|
||||
{
|
||||
if (parent_type::insert(key, ptr.get()))
|
||||
@ -112,4 +158,15 @@ inline bool Foam::HashPtrTable<T, Key, Hash>::set
|
||||
}
|
||||
|
||||
|
||||
template<class T, class Key, class Hash>
|
||||
inline bool Foam::HashPtrTable<T, Key, Hash>::set
|
||||
(
|
||||
const Key& key,
|
||||
std::unique_ptr<T>&& ptr
|
||||
)
|
||||
{
|
||||
return this->set(key, ptr.release());
|
||||
}
|
||||
|
||||
|
||||
// ************************************************************************* //
|
||||
|
||||
@ -364,6 +364,11 @@ public:
|
||||
template<class... Args>
|
||||
inline bool emplace(const Key& key, Args&&... args);
|
||||
|
||||
//- Emplace set an entry, overwriting any existing entries.
|
||||
// \return True, since it always overwrites any entries.
|
||||
template<class... Args>
|
||||
inline bool emplace_set(const Key& key, Args&&... args);
|
||||
|
||||
//- Copy insert a new entry, not overwriting existing entries.
|
||||
// \return True if the entry did not previously exist in the table.
|
||||
inline bool insert(const Key& key, const T& obj);
|
||||
|
||||
@ -6,7 +6,7 @@
|
||||
\\/ M anipulation |
|
||||
-------------------------------------------------------------------------------
|
||||
Copyright (C) 2011-2016 OpenFOAM Foundation
|
||||
Copyright (C) 2017-2019 OpenCFD Ltd.
|
||||
Copyright (C) 2017-2020 OpenCFD Ltd.
|
||||
-------------------------------------------------------------------------------
|
||||
License
|
||||
This file is part of OpenFOAM.
|
||||
@ -163,6 +163,18 @@ inline bool Foam::HashTable<T, Key, Hash>::emplace
|
||||
}
|
||||
|
||||
|
||||
template<class T, class Key, class Hash>
|
||||
template<class... Args>
|
||||
inline bool Foam::HashTable<T, Key, Hash>::emplace_set
|
||||
(
|
||||
const Key& key,
|
||||
Args&&... args
|
||||
)
|
||||
{
|
||||
return this->setEntry(true, key, std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
|
||||
template<class T, class Key, class Hash>
|
||||
inline bool Foam::HashTable<T, Key, Hash>::insert
|
||||
(
|
||||
|
||||
Reference in New Issue
Block a user