diff --git a/applications/test/PtrList/Test-PtrList.C b/applications/test/PtrList/Test-PtrList.C
index 851d3990fc..043cfe5c37 100644
--- a/applications/test/PtrList/Test-PtrList.C
+++ b/applications/test/PtrList/Test-PtrList.C
@@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011 OpenFOAM Foundation
- \\/ M anipulation |
+ \\/ M anipulation | Copyright (C) 2018 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@@ -21,10 +21,8 @@ License
You should have received a copy of the GNU General Public License
along with OpenFOAM. If not, see .
-Application
-
Description
-
+ Test behaviour of UPtrList, PtrList
\*---------------------------------------------------------------------------*/
#include "OSspecific.H"
@@ -102,13 +100,13 @@ int main(int argc, char *argv[])
forAllIters(llist1, it)
{
- Info<< typeid(*it).name() << endl;
- Info<< "reversed: " << *it << endl;
+ Info<< typeid(*it).name() << nl
+ << "reversed: " << *it << endl;
}
for (const auto& it : llist1)
{
- Info<< typeid(it).name() << endl;
- Info<< "for-: " << it << endl;
+ Info<< typeid(it).name() << nl
+ << "for-: " << it << endl;
}
}
@@ -127,9 +125,10 @@ int main(int argc, char *argv[])
listApp.append(new Scalar(1.3*i));
}
- Info<<"list1: " << list1 << endl;
- Info<<"list2: " << list2 << endl;
- Info<<"listApp: " << listApp << endl;
+ Info<< nl
+ <<"list1: " << list1 << nl
+ <<"list2: " << list2 << nl
+ <<"list-appended: " << listApp << endl;
Info<<"indirectly delete some items via set(.., 0) :" << endl;
for (label i = 0; i < 3; i++)
@@ -140,8 +139,8 @@ int main(int argc, char *argv[])
Info<<"transfer list2 -> list1:" << endl;
list1.transfer(list2);
- Info<<"list1: " << list1 << endl;
- Info<<"list2: " << list2 << endl;
+ Info<<"list1: " << list1 << nl
+ <<"list2: " << list2 << endl;
Info<<"indirectly delete some items via setSize :" << endl;
list1.setSize(4);
@@ -151,16 +150,71 @@ int main(int argc, char *argv[])
PtrList list3(list1.xfer());
Info<< "Transferred via the xfer() method" << endl;
- Info<<"list1: " << list1 << endl;
- Info<<"list2: " << list2 << endl;
- Info<<"list3: " << list3 << endl;
+ Info<<"list1: " << list1 << nl
+ <<"list2: " << list2 << nl
+ <<"list3: " << list3 << endl;
+
+
+ Info<<"Move construct:" << endl;
+
+ PtrList list4(std::move(list3));
+
+ Info<<"list3: " << list3 << nl
+ <<"list4: " << list4 << endl;
+
+ Info<<"Move assign:" << endl;
+ list3 = std::move(list4);
+
+ Info<<"list3: " << list3 << nl
+ <<"list4: " << list4 << endl;
+
+
+ Info<<"UPtrList from PtrList" << nl;
+
+ UPtrList ulist1(list3);
+
+ Info<<"ulist1: " << ulist1 << nl;
+
+ Info<<"Move construct:" << endl;
+
+ UPtrList ulist2(std::move(ulist1));
+
+ Info<<"ulist1: " << ulist1 << nl
+ <<"ulist2: " << ulist2 << nl;
+
+ Info<<"Copy assign:" << endl;
+ ulist1 = ulist2;
+
+ Info<<"ulist1: " << ulist1 << nl
+ <<"ulist2: " << ulist2 << nl;
+
+ Info<<"Move assign:" << endl;
+ ulist1 = std::move(ulist2);
+
+ Info<<"ulist1: " << ulist1 << nl
+ <<"ulist2: " << ulist2 << nl;
+
+ // Test iterator random access
+ {
+ auto iter1 = ulist1.begin();
+ auto iter2 = iter1 + 3;
+
+ Info<<"begin:" << *iter1 << " (+3):" << *iter2 << nl;
+ Info<< "diff= " << (iter1 - iter2) << nl;
+ Info<< "iter[2]=" << iter1[2] << nl;
+ Info<< "iter1 < iter2 : " << (iter1 < iter2) << nl;
+ Info<< "iter1 >= iter2 : " << (iter1 >= iter2) << nl;
+ }
PtrList planes;
planes.append(new plane(vector::one, vector::one));
planes.append(new plane(vector(1,2,3), vector::one));
- forAll(planes, p)
- Info<< "plane " << planes[p] << endl;
+ Info<< nl << "appended values" << nl;
+ for (const plane& p : planes)
+ {
+ Info<< " plane " << p << endl;
+ }
Info<< nl << "Done." << endl;
return 0;
diff --git a/src/OpenFOAM/containers/Lists/PtrList/PtrList.C b/src/OpenFOAM/containers/Lists/PtrList/PtrList.C
index b8555da86b..fd0b7263ca 100644
--- a/src/OpenFOAM/containers/Lists/PtrList/PtrList.C
+++ b/src/OpenFOAM/containers/Lists/PtrList/PtrList.C
@@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation
- \\/ M anipulation |
+ \\/ M anipulation | Copyright (C) 2018 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@@ -29,40 +29,30 @@ License
// * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * * //
template
-Foam::PtrList::PtrList()
+Foam::PtrList::PtrList(const PtrList& lst)
:
- UPtrList()
-{}
-
-
-template
-Foam::PtrList::PtrList(const label len)
-:
- UPtrList(len)
-{}
-
-
-template
-Foam::PtrList::PtrList(const PtrList& a)
-:
- UPtrList(a.size())
+ UPtrList(lst.size())
{
- forAll(*this, i)
+ const label len = this->size();
+
+ for (label i=0; iptrs_[i] = (a[i]).clone().ptr();
+ this->ptrs_[i] = (lst[i]).clone().ptr();
}
}
template
template
-Foam::PtrList::PtrList(const PtrList& a, const CloneArg& cloneArg)
+Foam::PtrList::PtrList(const PtrList& lst, const CloneArg& cloneArg)
:
- UPtrList(a.size())
+ UPtrList(lst.size())
{
- forAll(*this, i)
+ const label len = this->size();
+
+ for (label i=0; iptrs_[i] = (a[i]).clone(cloneArg).ptr();
+ this->ptrs_[i] = (lst[i]).clone(cloneArg).ptr();
}
}
@@ -75,34 +65,31 @@ Foam::PtrList::PtrList(const Xfer>& lst)
template
-Foam::PtrList::PtrList(PtrList& a, bool reuse)
+Foam::PtrList::PtrList(PtrList& lst, bool reuse)
:
- UPtrList(a, reuse)
+ UPtrList(lst, reuse)
{
if (!reuse)
{
- forAll(*this, i)
+ const label len = this->size();
+
+ for (label i=0; iptrs_[i] = (a[i]).clone().ptr();
+ this->ptrs_[i] = (lst[i]).clone().ptr();
}
}
}
template
-Foam::PtrList::PtrList(const SLPtrList& sll)
+Foam::PtrList::PtrList(const SLPtrList& lst)
:
- UPtrList(sll.size())
+ UPtrList(lst.size())
{
- if (sll.size())
+ if (lst.size())
{
label i = 0;
- for
- (
- typename SLPtrList::const_iterator iter = sll.cbegin();
- iter != sll.cend();
- ++iter
- )
+ for (auto iter = lst.cbegin(); iter != lst.cend(); ++iter)
{
this->ptrs_[i++] = (*iter).clone().ptr();
}
@@ -115,7 +102,10 @@ Foam::PtrList::PtrList(const SLPtrList& sll)
template
Foam::PtrList::~PtrList()
{
- forAll(*this, i)
+ const label len = this->size();
+
+ // Free old pointers
+ for (label i=0; iptrs_[i])
{
@@ -127,53 +117,12 @@ Foam::PtrList::~PtrList()
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
-template
-void Foam::PtrList::setSize(const label newSize)
-{
- if (newSize < 0)
- {
- FatalErrorInFunction
- << "bad set size " << newSize
- << " for type " << typeid(T).name()
- << abort(FatalError);
- }
-
- label oldSize = this->size();
-
- if (newSize == 0)
- {
- clear();
- }
- else if (newSize < oldSize)
- {
- label i;
- for (i=newSize; iptrs_[i])
- {
- delete this->ptrs_[i];
- }
- }
-
- this->ptrs_.setSize(newSize);
- }
- else // newSize > oldSize
- {
- this->ptrs_.setSize(newSize);
-
- label i;
- for (i=oldSize; iptrs_[i] = nullptr;
- }
- }
-}
-
-
template
void Foam::PtrList::clear()
{
- forAll(*this, i)
+ const label len = this->size();
+
+ for (label i=0; iptrs_[i])
{
@@ -181,109 +130,82 @@ void Foam::PtrList::clear()
}
}
- this->ptrs_.clear();
+ UPtrList::clear();
}
template
-void Foam::PtrList::transfer(PtrList& a)
+void Foam::PtrList::setSize(const label newLen)
{
- clear();
- this->ptrs_.transfer(a.ptrs_);
-}
-
-
-template
-void Foam::PtrList::reorder(const labelUList& oldToNew)
-{
- if (oldToNew.size() != this->size())
+ if (newLen <= 0)
{
- FatalErrorInFunction
- << "Size of map (" << oldToNew.size()
- << ") not equal to list size (" << this->size()
- << ") for type " << typeid(T).name()
- << abort(FatalError);
+ clear();
+ return;
}
- List newPtrs_(this->ptrs_.size(), reinterpret_cast(0));
-
- forAll(*this, i)
+ const label oldLen = this->size();
+ if (newLen < oldLen)
{
- const label newI = oldToNew[i];
-
- if (newI < 0 || newI >= this->size())
+ // Truncate - free old pointers
+ for (label i=newLen; isize()-1
- << " for type " << typeid(T).name()
- << abort(FatalError);
+ if (this->ptrs_[i])
+ {
+ delete this->ptrs_[i];
+ }
}
- if (newPtrs_[newI])
- {
- FatalErrorInFunction
- << "reorder map is not unique; element " << newI
- << " already set for type " << typeid(T).name()
- << abort(FatalError);
- }
- newPtrs_[newI] = this->ptrs_[i];
+ this->ptrs_.setSize(newLen);
}
-
- forAll(newPtrs_, i)
+ else if (newLen > oldLen)
{
- if (!newPtrs_[i])
- {
- FatalErrorInFunction
- << "Element " << i << " not set after reordering with type "
- << typeid(T).name() << nl << abort(FatalError);
- }
+ // Extend - new elements initialized to nullptr
+ this->ptrs_.setSize(newLen, reinterpret_cast(0));
}
-
- this->ptrs_.transfer(newPtrs_);
}
// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
template
-void Foam::PtrList::operator=(const PtrList& a)
+void Foam::PtrList::operator=(const PtrList& lst)
{
- if (this == &a)
+ if (this == &lst)
{
FatalErrorInFunction
<< "attempted assignment to self for type " << typeid(T).name()
<< abort(FatalError);
}
- if (this->size() == 0)
- {
- setSize(a.size());
+ const label oldLen = this->size();
+ const label newLen = lst.size();
- forAll(*this, i)
- {
- this->ptrs_[i] = (a[i]).clone().ptr();
- }
- }
- else if (a.size() == this->size())
+ // Truncate (frees old pointers) or extend the length
+ setSize(newLen);
+
+ if (newLen < oldLen)
{
- forAll(*this, i)
+ // Copy values for existing entries
+ for (label i=0; iptrs_[i] = (lst[i]).clone().ptr();
+ }
}
}
-// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
-
-#include "PtrListIO.C"
-
// ************************************************************************* //
diff --git a/src/OpenFOAM/containers/Lists/PtrList/PtrList.H b/src/OpenFOAM/containers/Lists/PtrList/PtrList.H
index 1b81e189e3..c49f70846b 100644
--- a/src/OpenFOAM/containers/Lists/PtrList/PtrList.H
+++ b/src/OpenFOAM/containers/Lists/PtrList/PtrList.H
@@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation
- \\/ M anipulation |
+ \\/ M anipulation | Copyright (C) 2018 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@@ -25,11 +25,12 @@ Class
Foam::PtrList
Description
- A templated 1D list of pointers to objects of type \, where the
- size of the array is known and used for subscript bounds checking, etc.
+ A list of pointers to objects of type \, with allocation/deallocation
+ management of the pointers.
+ The operator[] returns a reference to the object, not the pointer.
- The element operator [] returns a reference to the object rather than a
- pointer.
+See Also
+ Foam::PtrList
SourceFiles
PtrListI.H
@@ -81,27 +82,30 @@ public:
// Constructors
- //- Null Constructor
- PtrList();
+ //- Construct null
+ PtrList() = default;
- //- Construct with size specified
- explicit PtrList(const label len);
+ //- Construct with specified size, each element initialized to nullptr
+ explicit inline PtrList(const label nElem);
- //- Copy constructor, using 'clone()' method on each element
- PtrList(const PtrList& a);
+ //- Copy construct using 'clone()' method on each element
+ PtrList(const PtrList& lst);
- //- Copy constructor with additional argument for clone
+ //- Move construct
+ inline PtrList(PtrList&& lst);
+
+ //- Copy construct with additional argument for 'clone()'
template
- PtrList(const PtrList& a, const CloneArg& cloneArg);
+ PtrList(const PtrList& lst, const CloneArg& cloneArg);
//- Construct by transferring the parameter contents
PtrList(const Xfer>& lst);
//- Construct as copy or re-use as specified
- PtrList(PtrList& a, bool reuse);
+ PtrList(PtrList& lst, bool reuse);
//- Construct as copy of SLPtrList
- explicit PtrList(const SLPtrList& sll);
+ explicit PtrList(const SLPtrList& lst);
//- Construct from Istream using given Istream constructor class
template
@@ -115,62 +119,62 @@ public:
~PtrList();
- // Member functions
+ // Member Functions
- // Edit
+ //- Clear the PtrList. Set size to zero and delete allocated entries
+ void clear();
- //- Reset size of PtrList. If extending the PtrList, new entries are
- // set to nullptr. If truncating the PtrList, removed entries are
- // deleted
- void setSize(const label);
+ //- Reset size of PtrList.
+ // New entries are initialized to nullptr, removed entries are deleted
+ void setSize(const label newLen);
- //- Alias for setSize(const label)
- inline void resize(const label);
+ //- Reset size of PtrList.
+ // New entries are initialized to nullptr, removed entries are deleted
+ inline void resize(const label newLen);
- //- Clear the PtrList, i.e. set size to zero deleting all the
- // allocated entries
- void clear();
+ //- Append an element at the end of the list
+ using UPtrList::append;
- //- Append an element at the end of the list
- inline void append(T*);
- inline void append(const autoPtr&);
- inline void append(const tmp&);
+ //- Append an element at the end of the list
+ inline void append(const autoPtr& aptr);
- //- Transfer the contents of the argument PtrList into this PtrList
- // and annul the argument list
- void transfer(PtrList&);
+ //- Append an element at the end of the list
+ inline void append(const tmp& tptr);
- //- Transfer contents to the Xfer container
- inline Xfer> xfer();
+ //- Transfer into this list and annul the argument list
+ inline void transfer(PtrList& lst);
- //- Is element set
- inline bool set(const label) const;
+ //- Transfer contents to the Xfer container
+ inline Xfer> xfer();
- //- Set element to given T* and return old element (can be nullptr)
- inline autoPtr set(const label, T*);
+ //- Return true if element is set (ie, not a nullptr)
+ inline bool set(const label i) const;
- //- Set element to given autoPtr and return old element
- inline autoPtr set(const label, const autoPtr&);
+ //- Set element to given pointer and return old element (can be null)
+ inline autoPtr set(const label i, T* ptr);
- //- Set element to given tmp and return old element
- inline autoPtr set(const label, const tmp&);
+ //- Set element to given autoPtr and return old element
+ inline autoPtr set(const label i, const autoPtr& aptr);
- //- Reorders elements. Ordering does not have to be done in
- // ascending or descending order. Reordering has to be unique.
- // (is shuffle)
- void reorder(const labelUList&);
+ //- Set element to given tmp and return old element
+ inline autoPtr set(const label i, const tmp& tptr);
// Member operators
- //- Assignment
- void operator=(const PtrList&);
+ //- Copy assignment.
+ // For existing list entries, values are copied from the list.
+ // For new list entries, pointers are cloned from the list.
+ void operator=(const PtrList& lst);
+
+ //- Move assignment
+ inline void operator=(PtrList&& lst);
// IOstream operator
//- Read PtrList from Istream, discarding contents of existing PtrList
- friend Istream& operator>> (Istream&, PtrList&);
+ friend Istream& operator>> (Istream& is, PtrList& lst);
};
@@ -186,6 +190,7 @@ public:
#ifdef NoRepository
#include "PtrList.C"
+ #include "PtrListIO.C"
#endif
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
diff --git a/src/OpenFOAM/containers/Lists/PtrList/PtrListI.H b/src/OpenFOAM/containers/Lists/PtrList/PtrListI.H
index ee1a0579fa..50885d572e 100644
--- a/src/OpenFOAM/containers/Lists/PtrList/PtrListI.H
+++ b/src/OpenFOAM/containers/Lists/PtrList/PtrListI.H
@@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation
- \\/ M anipulation |
+ \\/ M anipulation | Copyright (C) 2018 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@@ -26,38 +26,42 @@ License
#include "autoPtr.H"
#include "tmp.H"
+// * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * * //
+
+template
+inline Foam::PtrList::PtrList(const label nElem)
+:
+ UPtrList(nElem)
+{}
+
+
+template
+inline Foam::PtrList::PtrList(PtrList&& lst)
+:
+ UPtrList(std::move(lst))
+{}
+
+
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
template
-inline void Foam::PtrList::resize(const label newSize)
+inline void Foam::PtrList::resize(const label newLen)
{
- this->setSize(newSize);
-}
-
-
-template
-inline void Foam::PtrList::append(T* ptr)
-{
- const label idx = this->size();
- this->setSize(idx + 1);
- this->ptrs_[idx] = ptr;
+ this->setSize(newLen);
}
template
inline void Foam::PtrList::append(const autoPtr& aptr)
{
- return append(const_cast&>(aptr).ptr());
+ return UPtrList::append(const_cast&>(aptr).ptr());
}
template
-inline void Foam::PtrList::append
-(
- const tmp& t
-)
+inline void Foam::PtrList::append(const tmp& tptr)
{
- return append(const_cast&>(t).ptr());
+ return UPtrList::append(const_cast&>(tptr).ptr());
}
@@ -92,10 +96,18 @@ template
inline Foam::autoPtr Foam::PtrList::set
(
const label i,
- const tmp& t
+ const tmp& tptr
)
{
- return set(i, const_cast&>(t).ptr());
+ return set(i, const_cast&>(tptr).ptr());
+}
+
+
+template
+inline void Foam::PtrList::transfer(PtrList& lst)
+{
+ this->clear();
+ UPtrList::swap(lst);
}
@@ -106,4 +118,14 @@ inline Foam::Xfer> Foam::PtrList::xfer()
}
+// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
+
+template
+inline void Foam::PtrList::operator=(PtrList&& lst)
+{
+ this->clear();
+ this->swap(lst);
+}
+
+
// ************************************************************************* //
diff --git a/src/OpenFOAM/containers/Lists/PtrList/PtrListIO.C b/src/OpenFOAM/containers/Lists/PtrList/PtrListIO.C
index 388e7ca739..141225f776 100644
--- a/src/OpenFOAM/containers/Lists/PtrList/PtrListIO.C
+++ b/src/OpenFOAM/containers/Lists/PtrList/PtrListIO.C
@@ -139,16 +139,11 @@ void Foam::PtrList::read(Istream& is, const INew& inew)
setSize(sllPtrs.size());
- // Pointers - can simply copy
+ // A list of pointers - can simply copy
label i = 0;
- for
- (
- typename SLList::const_iterator iter = sllPtrs.cbegin();
- iter != sllPtrs.cend();
- ++iter
- )
+ for (T* ptr : sllPtrs)
{
- set(i++, *iter);
+ set(i++, ptr);
}
return;
diff --git a/src/OpenFOAM/containers/Lists/UPtrList/UPtrList.C b/src/OpenFOAM/containers/Lists/UPtrList/UPtrList.C
index 69737b210e..317cf817d7 100644
--- a/src/OpenFOAM/containers/Lists/UPtrList/UPtrList.C
+++ b/src/OpenFOAM/containers/Lists/UPtrList/UPtrList.C
@@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation
- \\/ M anipulation | Copyright (C) 2015 OpenCFD Ltd.
+ \\/ M anipulation | Copyright (C) 2015-2018 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@@ -27,137 +27,66 @@ License
// * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * * //
-template
-Foam::UPtrList::UPtrList()
-:
- ptrs_()
-{}
-
-
-template
-Foam::UPtrList::UPtrList(const label s)
-:
- ptrs_(s, reinterpret_cast(0))
-{}
-
-
template
Foam::UPtrList::UPtrList(UList& lst)
:
ptrs_(lst.size())
{
- forAll(lst, i)
+ const label len = lst.size();
+
+ for (label i=0; i
-Foam::UPtrList::UPtrList(PtrList& lst)
-:
- ptrs_(lst.size())
-{
- forAll(lst, i)
- {
- ptrs_[i] = &lst[i];
- }
-}
-
-
-template
-Foam::UPtrList::UPtrList(const Xfer>& lst)
-{
- transfer(lst());
-}
-
-
-template
-Foam::UPtrList::UPtrList(UPtrList& a, bool reuse)
-:
- ptrs_(a.ptrs_, reuse)
-{}
-
-
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
-template
-void Foam::UPtrList::setSize(const label newSize)
-{
- label oldSize = size();
-
- if (newSize <= 0)
- {
- clear();
- }
- else if (newSize < oldSize)
- {
- ptrs_.setSize(newSize);
- }
- else if (newSize > oldSize)
- {
- ptrs_.setSize(newSize);
-
- // set new elements to nullptr
- for (label i=oldSize; i
-void Foam::UPtrList::clear()
-{
- ptrs_.clear();
-}
-
-
-template
-void Foam::UPtrList::transfer(UPtrList& a)
-{
- ptrs_.transfer(a.ptrs_);
-}
-
-
template
void Foam::UPtrList::reorder(const labelUList& oldToNew)
{
- if (oldToNew.size() != size())
+ const label len = this->size();
+
+ if (oldToNew.size() != len)
{
FatalErrorInFunction
<< "Size of map (" << oldToNew.size()
- << ") not equal to list size (" << size()
- << ")." << abort(FatalError);
+ << ") not equal to list size (" << len
+ << ") for type " << typeid(T).name() << nl
+ << abort(FatalError);
}
- List newPtrs_(ptrs_.size(), reinterpret_cast(0));
+ // New list of pointers
+ List ptrLst(len, reinterpret_cast(0));
- forAll(*this, i)
+ for (label i=0; i= size())
+ if (idx < 0 || idx >= len)
{
FatalErrorInFunction
- << "Illegal index " << newI << nl
- << "Valid indices are 0.." << size()-1
+ << "Illegal index " << idx << nl
+ << "Valid indices are 0.." << len-1
+ << " for type " << typeid(T).name() << nl
<< abort(FatalError);
}
- if (newPtrs_[newI])
+ if (ptrLst[idx])
{
FatalErrorInFunction
- << "reorder map is not unique; element " << newI
- << " already set." << abort(FatalError);
+ << "reorder map is not unique; element " << idx
+ << " already set for type " << typeid(T).name()
+ << abort(FatalError);
}
- newPtrs_[newI] = ptrs_[i];
+ ptrLst[idx] = ptrs_[i];
}
- forAll(newPtrs_, i)
+ // Verify that all pointers were set
+ for (label i=0; i::reorder(const labelUList& oldToNew)
}
}
- ptrs_.transfer(newPtrs_);
+ ptrs_.swap(ptrLst);
}
-// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
-
-#include "UPtrListIO.C"
-
// ************************************************************************* //
diff --git a/src/OpenFOAM/containers/Lists/UPtrList/UPtrList.H b/src/OpenFOAM/containers/Lists/UPtrList/UPtrList.H
index 74e5d316ee..b23c6dbe1d 100644
--- a/src/OpenFOAM/containers/Lists/UPtrList/UPtrList.H
+++ b/src/OpenFOAM/containers/Lists/UPtrList/UPtrList.H
@@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation
- \\/ M anipulation |
+ \\/ M anipulation | Copyright (C) 2018 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@@ -25,14 +25,15 @@ Class
Foam::UPtrList
Description
- A templated 1D list of pointers to objects of type \, where the
- size of the array is known and used for subscript bounds checking, etc.
+ A list of pointers to objects of type \, without allocation/deallocation
+ management of the pointers - this is to be done elsewhere.
+ The operator[] returns a reference to the object, not the pointer.
- The element operator [] returns a reference to the object rather than a
- pointer. Storage is not allocated during construction or use but is
- supplied to the constructor as an argument.
+See Also
+ Foam::PtrList
SourceFiles
+ UPtrListI.H
UPtrList.C
UPtrListIO.C
@@ -43,337 +44,271 @@ SourceFiles
#include "List.H"
#include "PtrList.H"
+#include
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
namespace Foam
{
-// Forward declaration of friend classes
-template class PtrList;
+// Forward declarations
-// Forward declaration of friend functions and operators
+template class PtrList;
template class UPtrList;
-template
-inline typename UPtrList::iterator operator+
-(
- const typename UPtrList::iterator&,
- label
-);
-
-template
-inline typename UPtrList::iterator operator+
-(
- label,
- const typename UPtrList::iterator&
-);
-
-template
-inline typename UPtrList::iterator operator-
-(
- const typename UPtrList::iterator&,
- label
-);
-
-template
-inline label operator-
-(
- const typename UPtrList::iterator&,
- const typename UPtrList::iterator&
-);
-
-template
-inline typename UPtrList::const_iterator operator+
-(
- const typename UPtrList::const_iterator&,
- label
-);
-
-template
-inline typename UPtrList::const_iterator operator+
-(
- label,
- const typename UPtrList::const_iterator&
-);
-
-template
-inline typename UPtrList::const_iterator operator-
-(
- const typename UPtrList::const_iterator&,
- label
-);
-
-template
-inline label operator-
-(
- const typename UPtrList::const_iterator&,
- const typename UPtrList::const_iterator&
-);
-
-template
-Istream& operator>>(Istream&, UPtrList&);
-
-template
-Ostream& operator<<(Ostream&, const UPtrList&);
+template Ostream& operator<<(Ostream& os, const UPtrList& lst);
/*---------------------------------------------------------------------------*\
- Class UPtrList Declaration
+ Class UPtrList Declaration
\*---------------------------------------------------------------------------*/
template
class UPtrList
{
- // Private data
+protected:
+
+ // Protected data
List ptrs_;
public:
- // Related types
+ // STL type definitions
- //- Declare friendship with the UPtrList class
- friend class PtrList;
+ //- Type of values the list contains
+ typedef T value_type;
+
+ //- A non-const reference to the value_type
+ typedef T& reference;
+
+ //- A const reference to the value_type
+ typedef const T& const_reference;
+
+ //- Random-access iterator with non-const access
+ class iterator;
+
+ //- Random-access iterator with const access
+ class const_iterator;
// Constructors
- //- Null Constructor
- UPtrList();
+ //- Construct null
+ UPtrList() = default;
- //- Construct with size specified
- explicit UPtrList(const label);
+ //- Construct with specified size, each element initialized to nullptr
+ explicit inline UPtrList(const label nElem);
- //- Construct from UList
- explicit UPtrList(UList&);
+ //- Construct from PtrList, copying addresses of each list element.
+ // The argument is non-const access since this UPtrList can be used
+ // to change its values.
+ explicit inline UPtrList(PtrList& lst);
- //- Construct from PtrList
- explicit UPtrList(PtrList&);
+ //- Construct from UList, taking addresses of each list element
+ explicit UPtrList(UList& lst);
//- Construct by transferring the parameter contents
- UPtrList(const Xfer>&);
+ inline UPtrList(const Xfer>& lst);
//- Construct as copy or re-use as specified
- UPtrList(UPtrList&, bool reuse);
+ inline UPtrList(UPtrList& lst, bool reuse);
+
+ //- Copy construct
+ inline UPtrList(const UPtrList& lst) = default;
+
+ //- Move construct
+ inline UPtrList(UPtrList&& lst);
// Member functions
- // Access
+ // Access
- //- Return the number of elements in the UPtrList
- inline label size() const;
+ //- Return the number of elements in the UPtrList
+ inline label size() const;
- //- Return true if the UPtrList is empty (ie, size() is zero)
- inline bool empty() const;
+ //- Return true if the UPtrList is empty (ie, size() is zero)
+ inline bool empty() const;
- //- Swap content with another UPtrList
- inline void swap(UPtrList& lst);
+ //- Return reference to the first element of the list
+ inline T& first();
- //- Return reference to the first element of the list
- inline T& first();
+ //- Return reference to first element of the list
+ inline const T& first() const;
- //- Return reference to first element of the list
- inline const T& first() const;
+ //- Return reference to the last element of the list
+ inline T& last();
- //- Return reference to the last element of the list
- inline T& last();
-
- //- Return reference to the last element of the list
- inline const T& last() const;
+ //- Return reference to the last element of the list
+ inline const T& last() const;
- // Edit
+ // Edit
- //- Reset size of UPtrList. This can only be used to set the size
- // of an empty UPtrList, extend a UPtrList, remove entries from
- // the end of a UPtrList
- void setSize(const label);
+ //- Set list size to zero.
+ inline void clear();
- //- Reset size of UPtrList. This can only be used to set the size
- // of an empty UPtrList, extend a UPtrList, remove entries from
- // the end of a UPtrList
- inline void resize(const label);
+ //- Reset size of UPtrList.
+ // New entries are initialized to nullptr.
+ inline void setSize(const label newLen);
- //- Clear the UPtrList, i.e. set size to zero
- void clear();
+ //- Reset size of UPtrList.
+ // New entries are initialized to nullptr.
+ inline void resize(const label newLen);
- //- Transfer the contents of the argument UPtrList into this
- // UPtrList and annul the argument list
- void transfer(UPtrList&);
+ //- Append an element to the end of the list
+ inline void append(T* ptr);
- //- Transfer contents to the Xfer container
- inline Xfer> xfer();
+ //- Swap content
+ inline void swap(UPtrList& lst);
- //- Is element set
- inline bool set(const label) const;
+ //- Transfer contents into this list and annul the argument
+ inline void transfer(UPtrList& lst);
- //- Set element. Return old element (can be nullptr).
- // No checks on new element
- inline T* set(const label, T*);
+ //- Transfer contents to the Xfer container
+ inline Xfer> xfer();
- //- Reorders elements. Ordering does not have to be done in
- // ascending or descending order. Reordering has to be unique.
- // (is shuffle)
- void reorder(const labelUList&);
+ //- Return true if element is set (ie, not a nullptr)
+ inline bool set(const label i) const;
+
+ //- Set element to given pointer and return old element (can be null).
+ // No checks on new element
+ inline T* set(const label i, T* ptr);
+
+ //- Reorder elements. Reordering must be unique (ie, shuffle).
+ void reorder(const labelUList& oldToNew);
// Member operators
//- Return element const reference
- inline const T& operator[](const label) const;
+ inline const T& operator[](const label i) const;
//- Return element reference
- inline T& operator[](const label);
+ inline T& operator[](const label i);
//- Return element const pointer
- inline const T* operator()(const label) const;
+ inline const T* operator()(const label i) const;
+
+ //- Copy assignment
+ UPtrList& operator=(const UPtrList& lst) = default;
+
+ //- Move assignment
+ inline void operator=(UPtrList&& lst);
- // STL type definitions
+ // Iterators
- //- Type of values the UPtrList contains
- typedef T value_type;
-
- //- Type that can be used for storing into UPtrList::value_type objects
- typedef T& reference;
-
- //- Type that can be used for storing into constant UPtrList::value_type
- // objects
- typedef const T& const_reference;
-
-
- // STL iterator
- // Random access iterator for traversing UPtrList
-
- class iterator;
- friend class iterator;
-
- //- An STL iterator
+ //- Random-access iterator with non-const access
class iterator
{
T** ptr_;
public:
- //- Construct for a given UPtrList entry
- inline iterator(T**);
+ using iterator_category = std::random_access_iterator_tag;
+ using value_type = T;
+ using difference_type = label;
+ using pointer = T*;
+ using reference = T&;
+ friend class const_iterator;
+
+ //- Construct for a given entry
+ inline iterator(T** ptr);
// Member operators
- inline bool operator==(const iterator&) const;
- inline bool operator!=(const iterator&) const;
+ inline bool operator==(const iterator& iter) const;
+ inline bool operator!=(const iterator& iter) const;
- inline T& operator*();
- inline T& operator()();
+ inline reference operator*() const;
+ inline reference operator()() const;
- inline iterator operator++();
- inline iterator operator++(int);
+ // Forward iteration
+ inline iterator& operator++();
+ inline iterator operator++(int);
- inline iterator operator--();
- inline iterator operator--(int);
+ inline iterator& operator--();
+ inline iterator operator--(int);
- inline iterator operator+=(label);
+ // Random-access
+ inline iterator& operator+=(difference_type n);
+ inline iterator& operator-=(difference_type n);
+ inline iterator operator+(difference_type n) const;
+ inline iterator operator-(difference_type n) const;
- friend iterator operator+