ENH: refine definition of PtrList emplace() and HashPtrTable emplace_set()

- like emplace_back(), return a reference to the new element
This commit is contained in:
Mark Olesen
2023-02-06 16:21:08 +01:00
parent d597b3f959
commit 1dbb54c391
7 changed files with 31 additions and 17 deletions

View File

@ -599,7 +599,9 @@ int main(int argc, char *argv[])
dynPlanes.set(6, new plane(vector(2,2,1), vector::one));
dynPlanes.set(10, new plane(vector(4,5,6), vector::one));
dynPlanes.emplace(12, vector(3,2,1), vector::one);
Info<< "emplaced :"
<< dynPlanes.emplace(12, vector(3,2,1), vector::one) << endl;
dynPlanes.emplace_back(Zero, vector::one);
}

View File

@ -207,9 +207,9 @@ public:
inline bool emplace(const Key& key, Args&&... args);
//- Emplace set an entry, overwriting any existing entries.
// \return True, since it always overwrites any entries.
// \return Reference to the new element.
template<class... Args>
inline bool emplace_set(const Key& key, Args&&... args);
inline T& emplace_set(const Key& key, Args&&... args);
//- No insert() with raw pointers (potential memory leaks).
//- Use insert() with autoPtr or set()

View File

@ -95,13 +95,15 @@ inline bool Foam::HashPtrTable<T, Key, Hash>::emplace
template<class T, class Key, class Hash>
template<class... Args>
inline bool Foam::HashPtrTable<T, Key, Hash>::emplace_set
inline T& Foam::HashPtrTable<T, Key, Hash>::emplace_set
(
const Key& key,
Args&&... args
)
{
return this->set(key, new T(std::forward<Args>(args)...));
T* ptr = new T(std::forward<Args>(args)...);
(void)this->set(key, ptr);
return *ptr;
}

View File

@ -174,9 +174,11 @@ public:
//- Remove and return the top element. Can be called on an empty list.
inline autoPtr<T> remove();
//- Construct and set an element
//- Construct and set a new element at given position,
//- (discard old element at that location).
//- Return reference to the new list element.
template<class... Args>
inline autoPtr<T> emplace(const label i, Args&&... args);
inline T& emplace(const label i, Args&&... args);
//- Set element to given pointer and return old element (can be null).
//- Auto-sizes list as required.

View File

@ -220,8 +220,9 @@ template<class T, int SizeMin>
template<class... Args>
inline T& Foam::PtrDynList<T, SizeMin>::emplace_back(Args&&... args)
{
this->push_back(new T(std::forward<Args>(args)...));
return this->back();
T* ptr = new T(std::forward<Args>(args)...);
this->push_back(ptr);
return *ptr;
}
@ -346,13 +347,15 @@ inline Foam::autoPtr<T> Foam::PtrDynList<T, SizeMin>::remove()
template<class T, int SizeMin>
template<class... Args>
inline Foam::autoPtr<T> Foam::PtrDynList<T, SizeMin>::emplace
inline T& Foam::PtrDynList<T, SizeMin>::emplace
(
const label i,
Args&&... args
)
{
return this->set(i, new T(std::forward<Args>(args)...));
T* ptr = new T(std::forward<Args>(args)...);
(void)this->set(i, ptr);
return *ptr;
}

View File

@ -172,9 +172,11 @@ public:
//- Move append another list to the end of this list.
inline void push_back(PtrList<T>&& other);
//- Construct and set an element
//- Construct and set a new element at given position,
//- (discard old element at that location).
//- Return reference to the new list element.
template<class... Args>
inline autoPtr<T> emplace(const label i, Args&&... args);
inline T& emplace(const label i, Args&&... args);
//- Set element to given pointer and return old element (can be null)
// No-op if the new pointer value is identical to the current content.

View File

@ -105,8 +105,9 @@ template<class T>
template<class... Args>
inline T& Foam::PtrList<T>::emplace_back(Args&&... args)
{
UPtrList<T>::push_back(new T(std::forward<Args>(args)...));
return this->back();
T* ptr = new T(std::forward<Args>(args)...);
UPtrList<T>::push_back(ptr);
return *ptr;
}
@ -171,13 +172,15 @@ inline void Foam::PtrList<T>::push_back(PtrList<T>&& other)
template<class T>
template<class... Args>
inline Foam::autoPtr<T> Foam::PtrList<T>::emplace
inline T& Foam::PtrList<T>::emplace
(
const label i,
Args&&... args
)
{
return set(i, new T(std::forward<Args>(args)...));
T* ptr = new T(std::forward<Args>(args)...);
(void)this->set(i, ptr);
return *ptr;
}