Commit 872c9d37 authored by Mark Olesen's avatar Mark Olesen

ENH: support emplace methods and std::unique_ptr for PtrList-derivatives

- emplace methods
  Eg,
      m.internalCoeffs().emplace(patchi, fc.size(), Zero);
  vs.
      m.internalCoeffs().set(patchi, new Field<Type>(fc.size(), Zero));

- handle insert/append of refPtr wherever tmp was already supported

COMP: incorrect variable names in PtrListOpsTemplates.C
parent 4110699d
......@@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011 OpenFOAM Foundation
Copyright (C) 2018-2019 OpenCFD Ltd.
Copyright (C) 2018-2020 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -278,7 +278,7 @@ int main(int argc, char *argv[])
llist1.insert(new Scalar(200));
llist1.insert(new Scalar(300));
DLPtrList<Scalar>::const_iterator citer = llist1.begin();
auto citer = llist1.begin();
Info<< *citer << endl;
Info<< typeid(*citer).name() << endl;
......@@ -324,15 +324,18 @@ int main(int argc, char *argv[])
list1.set(i, new Scalar(1.3*i));
}
Info<<"Emplace set " << list2.size() << " values" << nl;
forAll(list2, i)
{
list2.set(i, new Scalar(10 + 1.3*i));
list2.emplace(i, (10 + 1.3*i));
}
for (label i = 0; i < 5; ++i)
{
listApp.append(new Scalar(1.3*i));
}
listApp.emplace_append(100);
Info<< nl
<<"list1: " << list1 << nl
......@@ -529,8 +532,8 @@ int main(int argc, char *argv[])
}
PtrList<plane> planes;
planes.append(new plane(vector::one, vector::one));
planes.append(new plane(vector(1,2,3), vector::one));
planes.emplace_append(vector::one, vector::one);
planes.emplace_append(vector(1,2,3), vector::one);
Info<< nl << "appended values" << nl;
for (const plane& p : planes)
......@@ -543,15 +546,18 @@ int main(int argc, char *argv[])
PtrDynList<plane> dynPlanes;
{
dynPlanes.append(new plane(vector::one, vector::one));
dynPlanes.append(new plane(vector(1,2,3), vector::one));
dynPlanes.emplace_append(vector::one, vector::one);
dynPlanes.emplace_append(vector(1,2,3), vector::one);
dynPlanes.append(nullptr);
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);
dynPlanes.emplace_append(Zero, vector::one);
}
Info<< nl << "PtrList: ";
Info<< nl << "PtrDynList: ";
report(Info, dynPlanes, true);
dynPlanes.resize(9);
......
......@@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2018-2019 OpenCFD Ltd.
Copyright (C) 2018-2020 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -43,6 +43,8 @@ SourceFiles
#include "PtrList.H"
#include <type_traits>
#include <memory>
#include <utility>
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
......@@ -50,7 +52,6 @@ namespace Foam
{
// Forward Declarations
template<class T, int SizeMin> class PtrDynList;
/*---------------------------------------------------------------------------*\
......@@ -107,6 +108,11 @@ public:
//- Size of the underlying storage.
inline label capacity() const noexcept;
//- Return const pointer to element (if set) or nullptr,
// with bounds checking.
// The return value can be tested as a bool.
inline const T* set(const label i) const;
// Edit
......@@ -145,14 +151,27 @@ public:
// \return the number of non-null entries
inline label squeezeNull();
//- Construct and append an element to the end of the list
template<class... Args>
inline void emplace_append(Args&&... args);
//- Append an element to the end of the list
inline void append(T* ptr);
//- Move append an element to the end of the list
inline void append(const autoPtr<T>& aptr);
inline void append(autoPtr<T>& ptr);
//- Move append an element to the end of the list
inline void append(autoPtr<T>&& ptr);
//- Move append an element to the end of the list
inline void append(std::unique_ptr<T>&& ptr);
//- Move or clone append a tmp to the end of the list
inline void append(const tmp<T>& tptr);
inline void append(const refPtr<T>& ptr);
//- Move or clone append a tmp to the end of the list
inline void append(const tmp<T>& ptr);
//- Move append another list to the end of this list.
inline void append(PtrList<T>&& other);
......@@ -164,18 +183,27 @@ public:
//- Remove and return the top element
inline autoPtr<T> remove();
//- Return const pointer to element (if set) or nullptr.
// The return value can be tested as a bool.
inline const T* set(const label i) const;
//- Construct and set an element
template<class... Args>
inline autoPtr<T> emplace(const label i, Args&&... args);
//- Set element to given pointer and return old element (can be null)
inline autoPtr<T> set(const label i, T* ptr);
//- Set element to given autoPtr and return old element
inline autoPtr<T> set(const label i, const autoPtr<T>& aptr);
inline autoPtr<T> set(const label i, autoPtr<T>& ptr);
//- Set element to given autoPtr and return old element
inline autoPtr<T> set(const label i, autoPtr<T>&& ptr);
//- Set element to given pointer and return old element
inline autoPtr<T> set(const label i, std::unique_ptr<T>&& ptr);
//- Set element to given refPtr and return old element
inline autoPtr<T> set(const label i, const refPtr<T>& ptr);
//- Set element to given tmp and return old element
inline autoPtr<T> set(const label i, const tmp<T>& tptr);
inline autoPtr<T> set(const label i, const tmp<T>& ptr);
//- Reorder elements. Reordering must be unique (ie, shuffle).
inline void reorder(const labelUList& oldToNew);
......
......@@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2018-2019 OpenCFD Ltd.
Copyright (C) 2018-2020 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -26,6 +26,7 @@ License
\*---------------------------------------------------------------------------*/
#include "autoPtr.H"
#include "refPtr.H"
#include "tmp.H"
// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
......@@ -98,6 +99,13 @@ inline Foam::label Foam::PtrDynList<T, SizeMin>::capacity() const noexcept
}
template<class T, int SizeMin>
inline const T* Foam::PtrDynList<T, SizeMin>::set(const label i) const
{
return (i >= 0 && i < PtrList<T>::size()) ? PtrList<T>::get(i) : nullptr;
}
template<class T, int SizeMin>
inline void Foam::PtrDynList<T, SizeMin>::setCapacity(const label nElem)
{
......@@ -151,13 +159,7 @@ inline void Foam::PtrDynList<T, SizeMin>::resize(const label newLen)
// Truncation frees old pointers
for (label i=newLen; i<oldLen; ++i)
{
T* ptr = ptrs[i];
if (ptr)
{
delete ptr;
}
delete ptrs[i];
ptrs[i] = nullptr;
}
}
......@@ -227,6 +229,14 @@ inline Foam::label Foam::PtrDynList<T, SizeMin>::squeezeNull()
}
template<class T, int SizeMin>
template<class... Args>
inline void Foam::PtrDynList<T, SizeMin>::emplace_append(Args&&... args)
{
this->append(new T(std::forward<Args>(args)...));
}
template<class T, int SizeMin>
inline void Foam::PtrDynList<T, SizeMin>::append(T* ptr)
{
......@@ -237,16 +247,37 @@ inline void Foam::PtrDynList<T, SizeMin>::append(T* ptr)
template<class T, int SizeMin>
inline void Foam::PtrDynList<T, SizeMin>::append(const autoPtr<T>& aptr)
inline void Foam::PtrDynList<T, SizeMin>::append(autoPtr<T>& ptr)
{
this->append(ptr.release());
}
template<class T, int SizeMin>
inline void Foam::PtrDynList<T, SizeMin>::append(autoPtr<T>&& ptr)
{
this->append(ptr.release());
}
template<class T, int SizeMin>
inline void Foam::PtrDynList<T, SizeMin>::append(std::unique_ptr<T>&& ptr)
{
this->append(const_cast<autoPtr<T>&>(aptr).release());
this->append(ptr.release());
}
template<class T, int SizeMin>
inline void Foam::PtrDynList<T, SizeMin>::append(const tmp<T>& tptr)
inline void Foam::PtrDynList<T, SizeMin>::append(const refPtr<T>& ptr)
{
this->append(tptr.ptr());
this->append(ptr.ptr());
}
template<class T, int SizeMin>
inline void Foam::PtrDynList<T, SizeMin>::append(const tmp<T>& ptr)
{
this->append(ptr.ptr());
}
......@@ -308,9 +339,14 @@ inline Foam::autoPtr<T> Foam::PtrDynList<T, SizeMin>::remove()
template<class T, int SizeMin>
inline const T* Foam::PtrDynList<T, SizeMin>::set(const label i) const
template<class... Args>
inline Foam::autoPtr<T> Foam::PtrDynList<T, SizeMin>::emplace
(
const label i,
Args&&... args
)
{
return (i >= 0 && i < PtrList<T>::size()) ? PtrList<T>::set(i) : nullptr;
return this->set(i, new T(std::forward<Args>(args)...));
}
......@@ -334,10 +370,43 @@ template<class T, int SizeMin>
inline Foam::autoPtr<T> Foam::PtrDynList<T, SizeMin>::set
(
const label i,
const autoPtr<T>& aptr
autoPtr<T>& ptr
)
{
return this->set(i, ptr.release());
}
template<class T, int SizeMin>
inline Foam::autoPtr<T> Foam::PtrDynList<T, SizeMin>::set
(
const label i,
autoPtr<T>&& ptr
)
{
return this->set(i, ptr.release());
}
template<class T, int SizeMin>
inline Foam::autoPtr<T> Foam::PtrDynList<T, SizeMin>::set
(
const label i,
std::unique_ptr<T>&& ptr
)
{
return this->set(i, ptr.release());
}
template<class T, int SizeMin>
inline Foam::autoPtr<T> Foam::PtrDynList<T, SizeMin>::set
(
const label i,
const refPtr<T>& ptr
)
{
return this->set(i, const_cast<autoPtr<T>&>(aptr).release());
return this->set(i, ptr.ptr());
}
......@@ -345,10 +414,10 @@ template<class T, int SizeMin>
inline Foam::autoPtr<T> Foam::PtrDynList<T, SizeMin>::set
(
const label i,
const tmp<T>& tptr
const tmp<T>& ptr
)
{
return this->set(i, tptr.ptr());
return this->set(i, ptr.ptr());
}
......
......@@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2018-2019 OpenCFD Ltd.
Copyright (C) 2018-2020 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -48,6 +48,8 @@ SourceFiles
#include "UPtrList.H"
#include "SLPtrListFwd.H"
#include <memory>
#include <utility>
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
......@@ -55,14 +57,12 @@ namespace Foam
{
// Forward Declarations
template<class T> class autoPtr;
template<class T> class refPtr;
template<class T> class tmp;
template<class T> class PtrList;
template<class T> Istream& operator>>(Istream& is, PtrList<T>& list);
/*---------------------------------------------------------------------------*\
Class PtrList Declaration
\*---------------------------------------------------------------------------*/
......@@ -87,7 +87,7 @@ public:
// Constructors
//- Construct null
//- Default construct
inline constexpr PtrList() noexcept;
//- Construct with specified size, each element initialized to nullptr
......@@ -130,6 +130,17 @@ public:
template<class... Args>
PtrList<T> clone(Args&&... args) const;
// Access
//- Return const pointer to element (can be nullptr),
// without bounds checking.
// The return value can also be tested as a bool.
const T* set(const label i) const { return UPtrList<T>::set(i); }
// Edit
//- Clear the PtrList. Delete allocated entries and set size to zero.
inline void clear();
......@@ -140,42 +151,58 @@ public:
//- Same as resize()
inline void setSize(const label newLen);
//- Construct and append an element to the end of the list
template<class... Args>
inline void emplace_append(Args&&... args);
//- Append an element to the end of the list
inline void append(T* ptr);
//- Move append an element to the end of the list
inline void append(autoPtr<T>& aptr);
inline void append(autoPtr<T>& ptr);
//- Move append an element to the end of the list
inline void append(autoPtr<T>&& aptr);
inline void append(autoPtr<T>&& ptr);
//- Move or clone append a tmp to the end of the list
inline void append(const tmp<T>& tptr);
//- Move append an element to the end of the list
inline void append(std::unique_ptr<T>&& ptr);
//- Transfer into this list and annul the argument list
inline void transfer(PtrList<T>& list);
//- Move or clone append a refPtr to the end of the list
inline void append(const refPtr<T>& ptr);
//- Move or clone append a tmp to the end of the list
inline void append(const tmp<T>& ptr);
//- Return const pointer to element (if set) or nullptr.
// The return value can be tested as a bool.
inline const T* set(const label i) const;
//- Construct and set an element
template<class... Args>
inline autoPtr<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.
inline autoPtr<T> set(const label i, T* ptr);
//- Set element to given autoPtr and return old element
inline autoPtr<T> set(const label i, autoPtr<T>& aptr);
inline autoPtr<T> set(const label i, autoPtr<T>& ptr);
//- Set element to given autoPtr and return old element
inline autoPtr<T> set(const label i, autoPtr<T>&& aptr);
inline autoPtr<T> set(const label i, autoPtr<T>&& ptr);
//- Set element to given unique_ptr and return old element
inline autoPtr<T> set(const label i, std::unique_ptr<T>&& ptr);
//- Set element to given refPtr and return old element
inline autoPtr<T> set(const label i, const refPtr<T>& ptr);
//- Set element to given tmp and return old element
inline autoPtr<T> set(const label i, const tmp<T>& tptr);
inline autoPtr<T> set(const label i, const tmp<T>& ptr);
//- Release ownership of the pointer at the given position.
// Out of bounds addressing is a no-op and returns nullptr.
inline autoPtr<T> release(const label i);
//- Transfer into this list and annul the argument list
inline void transfer(PtrList<T>& list);
// Member Operators
......@@ -192,7 +219,6 @@ public:
//- Read from Istream, discarding contents of existing list
friend Istream& operator>> <T>(Istream& is, PtrList<T>& list);
};
......
......@@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2018-2019 OpenCFD Ltd.
Copyright (C) 2018-2020 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -27,6 +27,7 @@ License
\*---------------------------------------------------------------------------*/
#include "autoPtr.H"
#include "refPtr.H"
#include "tmp.H"
// * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
......@@ -71,14 +72,10 @@ inline Foam::PtrList<T>::PtrList(PtrList<T>&& list)
template<class T>
inline Foam::PtrList<T>::PtrList(UList<T*>& list)
:
UPtrList<T>(list.size())
UPtrList<T>(list)
{
// Take ownership of the pointer
forAll(list, i)
{
set(i, list[i]);
list[i] = nullptr;
}
// Took ownership of the pointers
list = reinterpret_cast<T*>(0);
}
......@@ -111,6 +108,14 @@ inline void Foam::PtrList<T>::setSize(const label newLen)
}
template<class T>
template<class... Args>
inline void Foam::PtrList<T>::emplace_append(Args&&... args)
{
UPtrList<T>::append(new T(std::forward<Args>(args)...));
}
template<class T>
inline void Foam::PtrList<T>::append(T* ptr)
{
......@@ -119,58 +124,115 @@ inline void Foam::PtrList<T>::append(T* ptr)
template<class T>
inline void Foam::PtrList<T>::append(autoPtr<T>& aptr)
inline void Foam::PtrList<T>::append(autoPtr<T>& ptr)
{
UPtrList<T>::append(ptr.release());
}
template<class T>
inline void Foam::PtrList<T>::append(autoPtr<T>&& ptr)
{
UPtrList<T>::append(ptr.release());
}
template<class T>
inline void Foam::PtrList<T>::append(std::unique_ptr<T>&& ptr)
{
UPtrList<T>::append(aptr.release());
UPtrList<T>::append(ptr.release());
}
template<class T>
inline void Foam::PtrList<T>::append(autoPtr<T>&& aptr)
inline void Foam::PtrList<T>::append(const refPtr<T>& ptr)
{
UPtrList<T>::append(aptr.release());
UPtrList<T>::append(ptr.ptr());
}
template<class T>
inline void Foam::PtrList<T>::append(const tmp<T>& tptr)
inline void Foam::PtrList<T>::append(const tmp<T>& ptr)
{
UPtrList<T>::append(tptr.ptr());
UPtrList<T>::append(ptr.ptr());
}
template<class T>
inline const T* Foam::PtrList<T>::set(const label i) const
template<class... Args>
inline Foam::autoPtr<T> Foam::PtrList<T>::emplace
(
const label i,
Args&&... args
)
{
return UPtrList<T>::set(i);
return set(i, new T(std::forward<Args>(args)...));
}
template<class T>
inline Foam::autoPtr<T> Foam::PtrList<T>::set(const label i, T* ptr)
{
// UPtrList::set returns a nullptr if trying to set with the same
// pointer (again). This prevents the autoPtr from managing the
// memory (avoids possible double deletion).
return autoPtr<T>(UPtrList<T>::set(i, ptr));
}
template<class T>
inline Foam::autoPtr<T> Foam::PtrList<T>::set(const label i, autoPtr<T>& aptr)
inline Foam::autoPtr<T> Foam::PtrList<T>::set
(
const label i,
autoPtr<T>& ptr
)
{
return set(i, ptr.release());
}
template<class T>
inline Foam::autoPtr<T> Foam::PtrList<T>::set
(
const label i,
autoPtr<T>&& ptr
)
{
return set(i, ptr.release());
}