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
This commit is contained in:
Mark Olesen
2020-07-27 07:51:05 +02:00
parent 4110699d90
commit 872c9d370b
10 changed files with 312 additions and 107 deletions

View File

@ -6,7 +6,7 @@
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2011 OpenFOAM Foundation Copyright (C) 2011 OpenFOAM Foundation
Copyright (C) 2018-2019 OpenCFD Ltd. Copyright (C) 2018-2020 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. 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(200));
llist1.insert(new Scalar(300)); llist1.insert(new Scalar(300));
DLPtrList<Scalar>::const_iterator citer = llist1.begin(); auto citer = llist1.begin();
Info<< *citer << endl; Info<< *citer << endl;
Info<< typeid(*citer).name() << endl; Info<< typeid(*citer).name() << endl;
@ -324,15 +324,18 @@ int main(int argc, char *argv[])
list1.set(i, new Scalar(1.3*i)); list1.set(i, new Scalar(1.3*i));
} }
Info<<"Emplace set " << list2.size() << " values" << nl;
forAll(list2, i) 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) for (label i = 0; i < 5; ++i)
{ {
listApp.append(new Scalar(1.3*i)); listApp.append(new Scalar(1.3*i));
} }
listApp.emplace_append(100);
Info<< nl Info<< nl
<<"list1: " << list1 << nl <<"list1: " << list1 << nl
@ -529,8 +532,8 @@ int main(int argc, char *argv[])
} }
PtrList<plane> planes; PtrList<plane> planes;
planes.append(new plane(vector::one, vector::one)); planes.emplace_append(vector::one, vector::one);
planes.append(new plane(vector(1,2,3), vector::one)); planes.emplace_append(vector(1,2,3), vector::one);
Info<< nl << "appended values" << nl; Info<< nl << "appended values" << nl;
for (const plane& p : planes) for (const plane& p : planes)
@ -543,15 +546,18 @@ int main(int argc, char *argv[])
PtrDynList<plane> dynPlanes; PtrDynList<plane> dynPlanes;
{ {
dynPlanes.append(new plane(vector::one, vector::one)); dynPlanes.emplace_append(vector::one, vector::one);
dynPlanes.append(new plane(vector(1,2,3), vector::one)); dynPlanes.emplace_append(vector(1,2,3), vector::one);
dynPlanes.append(nullptr); dynPlanes.append(nullptr);
dynPlanes.set(6, new plane(vector(2,2,1), vector::one)); dynPlanes.set(6, new plane(vector(2,2,1), vector::one));
dynPlanes.set(10, new plane(vector(4,5,6), 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); report(Info, dynPlanes, true);
dynPlanes.resize(9); dynPlanes.resize(9);

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com \\ / A nd | www.openfoam.com
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2018-2019 OpenCFD Ltd. Copyright (C) 2018-2020 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -43,6 +43,8 @@ SourceFiles
#include "PtrList.H" #include "PtrList.H"
#include <type_traits> #include <type_traits>
#include <memory>
#include <utility>
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
@ -50,7 +52,6 @@ namespace Foam
{ {
// Forward Declarations // Forward Declarations
template<class T, int SizeMin> class PtrDynList; template<class T, int SizeMin> class PtrDynList;
/*---------------------------------------------------------------------------*\ /*---------------------------------------------------------------------------*\
@ -107,6 +108,11 @@ public:
//- Size of the underlying storage. //- Size of the underlying storage.
inline label capacity() const noexcept; 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 // Edit
@ -145,14 +151,27 @@ public:
// \return the number of non-null entries // \return the number of non-null entries
inline label squeezeNull(); 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 //- Append an element to the end of the list
inline void append(T* ptr); inline void append(T* ptr);
//- Move append an element to the end of the list //- 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 //- 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. //- Move append another list to the end of this list.
inline void append(PtrList<T>&& other); inline void append(PtrList<T>&& other);
@ -164,18 +183,27 @@ public:
//- Remove and return the top element //- Remove and return the top element
inline autoPtr<T> remove(); inline autoPtr<T> remove();
//- Return const pointer to element (if set) or nullptr. //- Construct and set an element
// The return value can be tested as a bool. template<class... Args>
inline const T* set(const label i) const; inline autoPtr<T> emplace(const label i, Args&&... args);
//- Set element to given pointer and return old element (can be null) //- Set element to given pointer and return old element (can be null)
inline autoPtr<T> set(const label i, T* ptr); inline autoPtr<T> set(const label i, T* ptr);
//- Set element to given autoPtr and return old element //- 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 //- 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). //- Reorder elements. Reordering must be unique (ie, shuffle).
inline void reorder(const labelUList& oldToNew); inline void reorder(const labelUList& oldToNew);

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com \\ / A nd | www.openfoam.com
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2018-2019 OpenCFD Ltd. Copyright (C) 2018-2020 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -26,6 +26,7 @@ License
\*---------------------------------------------------------------------------*/ \*---------------------------------------------------------------------------*/
#include "autoPtr.H" #include "autoPtr.H"
#include "refPtr.H"
#include "tmp.H" #include "tmp.H"
// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * // // * * * * * * * * * * * * * 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> template<class T, int SizeMin>
inline void Foam::PtrDynList<T, SizeMin>::setCapacity(const label nElem) 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 // Truncation frees old pointers
for (label i=newLen; i<oldLen; ++i) for (label i=newLen; i<oldLen; ++i)
{ {
T* ptr = ptrs[i]; delete ptrs[i];
if (ptr)
{
delete ptr;
}
ptrs[i] = nullptr; 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> template<class T, int SizeMin>
inline void Foam::PtrDynList<T, SizeMin>::append(T* ptr) 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> 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(const_cast<autoPtr<T>&>(aptr).release()); this->append(ptr.release());
} }
template<class T, int SizeMin> template<class T, int SizeMin>
inline void Foam::PtrDynList<T, SizeMin>::append(const tmp<T>& tptr) inline void Foam::PtrDynList<T, SizeMin>::append(autoPtr<T>&& ptr)
{ {
this->append(tptr.ptr()); this->append(ptr.release());
}
template<class T, int SizeMin>
inline void Foam::PtrDynList<T, SizeMin>::append(std::unique_ptr<T>&& ptr)
{
this->append(ptr.release());
}
template<class T, int SizeMin>
inline void Foam::PtrDynList<T, SizeMin>::append(const refPtr<T>& 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> 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,10 @@ template<class T, int SizeMin>
inline Foam::autoPtr<T> Foam::PtrDynList<T, SizeMin>::set inline Foam::autoPtr<T> Foam::PtrDynList<T, SizeMin>::set
( (
const label i, const label i,
const autoPtr<T>& aptr autoPtr<T>& ptr
) )
{ {
return this->set(i, const_cast<autoPtr<T>&>(aptr).release()); return this->set(i, ptr.release());
} }
@ -345,10 +381,43 @@ template<class T, int SizeMin>
inline Foam::autoPtr<T> Foam::PtrDynList<T, SizeMin>::set inline Foam::autoPtr<T> Foam::PtrDynList<T, SizeMin>::set
( (
const label i, const label i,
const tmp<T>& tptr autoPtr<T>&& ptr
) )
{ {
return this->set(i, tptr.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, ptr.ptr());
}
template<class T, int SizeMin>
inline Foam::autoPtr<T> Foam::PtrDynList<T, SizeMin>::set
(
const label i,
const tmp<T>& ptr
)
{
return this->set(i, ptr.ptr());
} }

View File

@ -6,7 +6,7 @@
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2018-2019 OpenCFD Ltd. Copyright (C) 2018-2020 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -48,6 +48,8 @@ SourceFiles
#include "UPtrList.H" #include "UPtrList.H"
#include "SLPtrListFwd.H" #include "SLPtrListFwd.H"
#include <memory>
#include <utility>
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
@ -55,14 +57,12 @@ namespace Foam
{ {
// Forward Declarations // Forward Declarations
template<class T> class autoPtr; template<class T> class autoPtr;
template<class T> class refPtr;
template<class T> class tmp; template<class T> class tmp;
template<class T> class PtrList; template<class T> class PtrList;
template<class T> Istream& operator>>(Istream& is, PtrList<T>& list); template<class T> Istream& operator>>(Istream& is, PtrList<T>& list);
/*---------------------------------------------------------------------------*\ /*---------------------------------------------------------------------------*\
Class PtrList Declaration Class PtrList Declaration
\*---------------------------------------------------------------------------*/ \*---------------------------------------------------------------------------*/
@ -87,7 +87,7 @@ public:
// Constructors // Constructors
//- Construct null //- Default construct
inline constexpr PtrList() noexcept; inline constexpr PtrList() noexcept;
//- Construct with specified size, each element initialized to nullptr //- Construct with specified size, each element initialized to nullptr
@ -130,6 +130,17 @@ public:
template<class... Args> template<class... Args>
PtrList<T> clone(Args&&... args) const; 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. //- Clear the PtrList. Delete allocated entries and set size to zero.
inline void clear(); inline void clear();
@ -140,42 +151,58 @@ public:
//- Same as resize() //- Same as resize()
inline void setSize(const label newLen); 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 //- Append an element to the end of the list
inline void append(T* ptr); inline void append(T* ptr);
//- Move append an element to the end of the list //- 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 //- 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(std::unique_ptr<T>&& ptr);
//- 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 //- Move or clone append a tmp to the end of the list
inline void append(const tmp<T>& tptr); inline void append(const tmp<T>& ptr);
//- Transfer into this list and annul the argument list //- Construct and set an element
inline void transfer(PtrList<T>& list); template<class... Args>
inline autoPtr<T> emplace(const label i, Args&&... args);
//- 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;
//- Set element to given pointer and return old element (can be null) //- 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. // No-op if the new pointer value is identical to the current content.
inline autoPtr<T> set(const label i, T* ptr); inline autoPtr<T> set(const label i, T* ptr);
//- Set element to given autoPtr and return old element //- 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 //- 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 //- 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. //- Release ownership of the pointer at the given position.
// Out of bounds addressing is a no-op and returns nullptr. // Out of bounds addressing is a no-op and returns nullptr.
inline autoPtr<T> release(const label i); inline autoPtr<T> release(const label i);
//- Transfer into this list and annul the argument list
inline void transfer(PtrList<T>& list);
// Member Operators // Member Operators
@ -192,7 +219,6 @@ public:
//- Read from Istream, discarding contents of existing list //- Read from Istream, discarding contents of existing list
friend Istream& operator>> <T>(Istream& is, PtrList<T>& list); friend Istream& operator>> <T>(Istream& is, PtrList<T>& list);
}; };

View File

@ -6,7 +6,7 @@
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2018-2019 OpenCFD Ltd. Copyright (C) 2018-2020 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -27,6 +27,7 @@ License
\*---------------------------------------------------------------------------*/ \*---------------------------------------------------------------------------*/
#include "autoPtr.H" #include "autoPtr.H"
#include "refPtr.H"
#include "tmp.H" #include "tmp.H"
// * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * // // * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
@ -71,14 +72,10 @@ inline Foam::PtrList<T>::PtrList(PtrList<T>&& list)
template<class T> template<class T>
inline Foam::PtrList<T>::PtrList(UList<T*>& list) inline Foam::PtrList<T>::PtrList(UList<T*>& list)
: :
UPtrList<T>(list.size()) UPtrList<T>(list)
{ {
// Take ownership of the pointer // Took ownership of the pointers
forAll(list, i) list = reinterpret_cast<T*>(0);
{
set(i, list[i]);
list[i] = nullptr;
}
} }
@ -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> template<class T>
inline void Foam::PtrList<T>::append(T* ptr) inline void Foam::PtrList<T>::append(T* ptr)
{ {
@ -119,58 +124,115 @@ inline void Foam::PtrList<T>::append(T* ptr)
template<class T> template<class T>
inline void Foam::PtrList<T>::append(autoPtr<T>& aptr) inline void Foam::PtrList<T>::append(autoPtr<T>& ptr)
{ {
UPtrList<T>::append(aptr.release()); UPtrList<T>::append(ptr.release());
} }
template<class T> template<class T>
inline void Foam::PtrList<T>::append(autoPtr<T>&& aptr) inline void Foam::PtrList<T>::append(autoPtr<T>&& ptr)
{ {
UPtrList<T>::append(aptr.release()); UPtrList<T>::append(ptr.release());
} }
template<class T> template<class T>
inline void Foam::PtrList<T>::append(const tmp<T>& tptr) inline void Foam::PtrList<T>::append(std::unique_ptr<T>&& ptr)
{ {
UPtrList<T>::append(tptr.ptr()); UPtrList<T>::append(ptr.release());
} }
template<class T> template<class T>
inline const T* Foam::PtrList<T>::set(const label i) const inline void Foam::PtrList<T>::append(const refPtr<T>& ptr)
{ {
return UPtrList<T>::set(i); UPtrList<T>::append(ptr.ptr());
}
template<class T>
inline void Foam::PtrList<T>::append(const tmp<T>& ptr)
{
UPtrList<T>::append(ptr.ptr());
}
template<class T>
template<class... Args>
inline Foam::autoPtr<T> Foam::PtrList<T>::emplace
(
const label i,
Args&&... args
)
{
return set(i, new T(std::forward<Args>(args)...));
} }
template<class T> template<class T>
inline Foam::autoPtr<T> Foam::PtrList<T>::set(const label i, T* ptr) 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)); return autoPtr<T>(UPtrList<T>::set(i, ptr));
} }
template<class T> 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, aptr.release()); return set(i, ptr.release());
} }
template<class T> 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, aptr.release()); return set(i, ptr.release());
} }
template<class T> template<class T>
inline Foam::autoPtr<T> Foam::PtrList<T>::set(const label i, const tmp<T>& tptr) inline Foam::autoPtr<T> Foam::PtrList<T>::set
(
const label i,
std::unique_ptr<T>&& ptr
)
{ {
return set(i, tptr.ptr()); return set(i, ptr.release());
}
template<class T>
inline Foam::autoPtr<T> Foam::PtrList<T>::set
(
const label i,
const refPtr<T>& ptr
)
{
return set(i, ptr.ptr());
}
template<class T>
inline Foam::autoPtr<T> Foam::PtrList<T>::set
(
const label i,
const tmp<T>& ptr
)
{
return set(i, ptr.ptr());
} }

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com \\ / A nd | www.openfoam.com
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2018-2019 OpenCFD Ltd. Copyright (C) 2018-2020 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -31,9 +31,13 @@ Description
This class is considered implementation detail and should not normally This class is considered implementation detail and should not normally
be used other than by OpenFOAM container classes. be used other than by OpenFOAM container classes.
It stores a list of pointers, but makes leaves memory management
to the caller or sub-class.
The free() method can be used explicitly as required.
SourceFiles SourceFiles
PtrListDetailI.H
PtrListDetail.C PtrListDetail.C
PtrListDetailI.H
PtrListDetailIO.C PtrListDetailIO.C
\*---------------------------------------------------------------------------*/ \*---------------------------------------------------------------------------*/
@ -47,10 +51,6 @@ SourceFiles
namespace Foam namespace Foam
{ {
// Forward declarations
class Ostream;
namespace Detail namespace Detail
{ {
@ -67,11 +67,15 @@ public:
// Constructors // Constructors
//- Construct null //- Default construct
inline constexpr PtrListDetail() noexcept; inline constexpr PtrListDetail() noexcept;
//- Construct with specified size, each element initialized to nullptr //- Construct with specified size, each element initialized to nullptr
inline PtrListDetail(const label len); inline explicit PtrListDetail(const label len);
//- Copy a list of pointers.
// The caller is responsible for memory management.
inline explicit PtrListDetail(const UList<T*>& list);
//- Copy construct (shallow copies addresses) //- Copy construct (shallow copies addresses)
inline PtrListDetail(const PtrListDetail<T>& list); inline PtrListDetail(const PtrListDetail<T>& list);
@ -125,7 +129,6 @@ public:
void setSize(const label) = delete; void setSize(const label) = delete;
void setSize(const label, const T&) = delete; void setSize(const label, const T&) = delete;
void setSize(const label, const T*) = delete; void setSize(const label, const T*) = delete;
}; };
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com \\ / A nd | www.openfoam.com
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2018 OpenCFD Ltd. Copyright (C) 2018-2020 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -41,6 +41,16 @@ inline Foam::Detail::PtrListDetail<T>::PtrListDetail(const label len)
{} {}
template<class T>
inline Foam::Detail::PtrListDetail<T>::PtrListDetail
(
const UList<T*>& list
)
:
List<T*>(list)
{}
template<class T> template<class T>
inline Foam::Detail::PtrListDetail<T>::PtrListDetail inline Foam::Detail::PtrListDetail<T>::PtrListDetail
( (

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com \\ / A nd | www.openfoam.com
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2019 OpenCFD Ltd. Copyright (C) 2019-2020 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -79,7 +79,7 @@ void Foam::sortedOrder
template<class T> template<class T>
void Foam::sort(UPtrList<T>& list) void Foam::sort(UPtrList<T>& list)
{ {
labelList order(input.size()); labelList order(list.size());
sortedOrder(list, order); sortedOrder(list, order);
list.sortOrder(order, false); // false = allow nullptr list.sortOrder(order, false); // false = allow nullptr
} }
@ -88,7 +88,7 @@ void Foam::sort(UPtrList<T>& list)
template<class T, class Compare> template<class T, class Compare>
void Foam::sort(UPtrList<T>& list, const Compare& comp) void Foam::sort(UPtrList<T>& list, const Compare& comp)
{ {
labelList order(input.size()); labelList order(list.size());
sortedOrder(list, order, comp); sortedOrder(list, order, comp);
list.sortOrder(order, false); // false = allow nullptr list.sortOrder(order, false); // false = allow nullptr
} }

View File

@ -6,7 +6,7 @@
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2018-2019 OpenCFD Ltd. Copyright (C) 2018-2020 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -59,13 +59,10 @@ namespace Foam
{ {
// Forward Declarations // Forward Declarations
template<class T> class PtrList; template<class T> class PtrList;
template<class T> class UPtrList; template<class T> class UPtrList;
template<class T> Ostream& operator<<(Ostream& os, const UPtrList<T>& list); template<class T> Ostream& operator<<(Ostream& os, const UPtrList<T>& list);
/*---------------------------------------------------------------------------*\ /*---------------------------------------------------------------------------*\
Class UPtrList Declaration Class UPtrList Declaration
\*---------------------------------------------------------------------------*/ \*---------------------------------------------------------------------------*/
@ -109,7 +106,7 @@ public:
// Constructors // Constructors
//- Construct null //- Default construct
inline constexpr UPtrList() noexcept; inline constexpr UPtrList() noexcept;
//- Construct with specified size, each element initialized to nullptr //- Construct with specified size, each element initialized to nullptr
@ -126,9 +123,12 @@ public:
//- Shallow copy from PtrList. //- Shallow copy from PtrList.
// The argument is non-const to reflect that the UPtrList can change // The argument is non-const to reflect that the UPtrList can change
// the values (but not the addresses) of the original list. // the values (not the addresses) within the original list.
explicit UPtrList(PtrList<T>& list); explicit UPtrList(PtrList<T>& list);
//- Construct from UList of pointers
inline explicit UPtrList(const UList<T*>& list);
//- Construct from UList, taking the address of each list element //- Construct from UList, taking the address of each list element
// The argument is non-const to reflect that the UPtrList can change // The argument is non-const to reflect that the UPtrList can change
// the values of the original list. // the values of the original list.
@ -157,6 +157,11 @@ public:
//- Return reference to the last element of the list //- Return reference to the last element of the list
inline const T& last() const; inline const T& last() const;
//- 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 ptrs_[i]; }
// Edit // Edit
@ -183,10 +188,6 @@ public:
//- Transfer contents into this list and annul the argument //- Transfer contents into this list and annul the argument
inline void transfer(UPtrList<T>& list); inline void transfer(UPtrList<T>& list);
//- 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;
//- Set element to specified pointer and return the old list element, //- Set element to specified pointer and return the old list element,
//- which can be a nullptr. //- which can be a nullptr.
// No-op if the new pointer value is identical to the current content. // No-op if the new pointer value is identical to the current content.
@ -211,7 +212,7 @@ public:
//- Return reference to the element //- Return reference to the element
inline T& operator[](const label i); inline T& operator[](const label i);
//- Return const pointer to the element //- Return const pointer to the element.
inline const T* operator()(const label i) const; inline const T* operator()(const label i) const;
//- Copy assignment (shallow copies addresses) //- Copy assignment (shallow copies addresses)

View File

@ -6,7 +6,7 @@
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2018-2019 OpenCFD Ltd. Copyright (C) 2018-2020 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -70,6 +70,13 @@ inline Foam::UPtrList<T>::UPtrList(UPtrList<T>& list, bool reuse)
{} {}
template<class T>
inline Foam::UPtrList<T>::UPtrList(const UList<T*>& list)
:
ptrs_(list)
{}
template<class T> template<class T>
inline Foam::UPtrList<T>::UPtrList(UList<T>& list) inline Foam::UPtrList<T>::UPtrList(UList<T>& list)
: :
@ -172,13 +179,6 @@ inline void Foam::UPtrList<T>::append(T* ptr)
} }
template<class T>
inline const T* Foam::UPtrList<T>::set(const label i) const
{
return ptrs_[i];
}
template<class T> template<class T>
inline T* Foam::UPtrList<T>::set(const label i, T* ptr) inline T* Foam::UPtrList<T>::set(const label i, T* ptr)
{ {