diff --git a/src/OpenFOAM/containers/Lists/PtrList/PtrList.H b/src/OpenFOAM/containers/Lists/PtrList/PtrList.H index 357b078dbf..b3b2924d9f 100644 --- a/src/OpenFOAM/containers/Lists/PtrList/PtrList.H +++ b/src/OpenFOAM/containers/Lists/PtrList/PtrList.H @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | Copyright (C) 2011-2013 OpenFOAM Foundation + \\ / A nd | Copyright (C) 2011-2015 OpenFOAM Foundation \\/ M anipulation | ------------------------------------------------------------------------------- License @@ -32,6 +32,7 @@ Description pointer. SourceFiles + PtrListI.H PtrList.C PtrListIO.C @@ -80,6 +81,34 @@ inline label operator- const typename PtrList::iterator& ); +template +inline typename PtrList::const_iterator operator+ +( + const typename PtrList::const_iterator&, + label +); + +template +inline typename PtrList::const_iterator operator+ +( + label, + const typename PtrList::const_iterator& +); + +template +inline typename PtrList::const_iterator operator- +( + const typename PtrList::const_iterator&, + label +); + +template +inline label operator- +( + const typename PtrList::const_iterator&, + const typename PtrList::const_iterator& +); + template Istream& operator>>(Istream&, PtrList&); @@ -245,7 +274,9 @@ public: // Random access iterator for traversing PtrList. class iterator; + class const_iterator; friend class iterator; + friend class const_iterator; //- An STL-conforming iterator class iterator @@ -254,6 +285,8 @@ public: public: + friend class const_iterator; + //- Construct for a given PtrList entry inline iterator(T**); @@ -303,6 +336,87 @@ public: inline iterator end(); + // STL const_iterator + // Random access iterator for traversing PtrList. + + //- An STL-conforming const_iterator + class const_iterator + { + const T* const* ptr_; + + public: + + //- Construct for a given PtrList entry + inline const_iterator(const T* const*); + + //- Construct from an iterator + inline const_iterator(const iterator&); + + + // Member operators + + inline bool operator==(const const_iterator&) const; + inline bool operator!=(const const_iterator&) const; + + typedef const T& Tref; + inline Tref operator*(); + inline Tref operator()(); + + inline const_iterator operator++(); + inline const_iterator operator++(int); + + inline const_iterator operator--(); + inline const_iterator operator--(int); + + inline const_iterator operator+=(label); + + friend const_iterator operator+ + ( + const const_iterator&, + label + ); + friend const_iterator operator+ + ( + label, + const const_iterator& + ); + + inline const_iterator operator-=(label); + + friend const_iterator operator- + ( + const const_iterator&, + label + ); + + friend label operator- + ( + const const_iterator&, + const const_iterator& + ); + + inline const T& operator[](label); + + inline bool operator<(const const_iterator&) const; + inline bool operator>(const const_iterator&) const; + + inline bool operator<=(const const_iterator&) const; + inline bool operator>=(const const_iterator&) const; + }; + + //- Return an const_iterator to begin traversing the PtrList. + inline const_iterator cbegin() const; + + //- Return an const_iterator to end traversing the PtrList. + inline const_iterator cend() const; + + //- Return an const_iterator to begin traversing the PtrList. + inline const_iterator begin() const; + + //- Return an const_iterator to end traversing the PtrList. + inline const_iterator end() const; + + // IOstream operator //- Read List from Istream, discarding contents of existing List. diff --git a/src/OpenFOAM/containers/Lists/PtrList/PtrListI.H b/src/OpenFOAM/containers/Lists/PtrList/PtrListI.H index e6acf7fab6..a2970eb361 100644 --- a/src/OpenFOAM/containers/Lists/PtrList/PtrListI.H +++ b/src/OpenFOAM/containers/Lists/PtrList/PtrListI.H @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | Copyright (C) 2011-2013 OpenFOAM Foundation + \\ / A nd | Copyright (C) 2011-2015 OpenFOAM Foundation \\/ M anipulation | ------------------------------------------------------------------------------- License @@ -23,8 +23,6 @@ License \*---------------------------------------------------------------------------*/ -#include "error.H" - #include "autoPtr.H" #include "tmp.H" @@ -373,4 +371,227 @@ inline typename Foam::PtrList::iterator Foam::PtrList::end() } +// * * * * * * * * * * * * * * * STL const_iterator * * * * * * * * * * * * // + +template +inline Foam::PtrList::const_iterator::const_iterator(const T* const* ptr) +: + ptr_(ptr) +{} + + +template +inline Foam::PtrList::const_iterator::const_iterator(const iterator& iter) +: + ptr_(iter.ptr_) +{} + + +template +inline bool Foam::PtrList::const_iterator::operator== +( + const const_iterator& iter +) const +{ + return ptr_ == iter.ptr_; +} + + +template +inline bool Foam::PtrList::const_iterator::operator!= +( + const const_iterator& iter +) const +{ + return ptr_ != iter.ptr_; +} + + +template +inline const T& Foam::PtrList::const_iterator::operator*() +{ + return **ptr_; +} + + +template +inline const T& Foam::PtrList::const_iterator::operator()() +{ + return operator*(); +} + + +template +inline typename Foam::PtrList::const_iterator +Foam::PtrList::const_iterator::operator++() +{ + ++ptr_; + return *this; +} + + +template +inline typename Foam::PtrList::const_iterator +Foam::PtrList::const_iterator::operator++(int) +{ + const_iterator tmp = *this; + ++ptr_; + return tmp; +} + + +template +inline typename Foam::PtrList::const_iterator +Foam::PtrList::const_iterator::operator--() +{ + --ptr_; + return *this; +} + + +template +inline typename Foam::PtrList::const_iterator +Foam::PtrList::const_iterator::operator--(int) +{ + const_iterator tmp = *this; + --ptr_; + return tmp; +} + + +template +inline typename Foam::PtrList::const_iterator +Foam::PtrList::const_iterator::operator+=(label n) +{ + ptr_ += n; + return *this; +} + + +template +inline typename Foam::PtrList::const_iterator +Foam::operator+(const typename PtrList::const_iterator& iter, label n) +{ + typename PtrList::const_iterator tmp = iter; + return tmp += n; +} + + +template +inline typename Foam::PtrList::const_iterator +Foam::operator+(label n, const typename PtrList::const_iterator& iter) +{ + typename PtrList::const_iterator tmp = iter; + return tmp += n; +} + + +template +inline typename Foam::PtrList::const_iterator +Foam::PtrList::const_iterator::operator-=(label n) +{ + ptr_ -= n; + return *this; +} + + +template +inline typename Foam::PtrList::const_iterator +Foam::operator-(const typename PtrList::const_iterator& iter, label n) +{ + typename PtrList::const_iterator tmp = iter; + return tmp -= n; +} + + +template +inline Foam::label Foam::operator- +( + const typename PtrList::const_iterator& iter1, + const typename PtrList::const_iterator& iter2 +) +{ + return (iter1.ptr_ - iter2.ptr_)/sizeof(T*); +} + + +template +inline const T& Foam::PtrList::const_iterator::operator[](label n) +{ + return *(*this + n); +} + + +template +inline bool Foam::PtrList::const_iterator::operator< +( + const const_iterator& iter +) const +{ + return ptr_ < iter.ptr_; +} + + +template +inline bool Foam::PtrList::const_iterator::operator> +( + const const_iterator& iter +) const +{ + return ptr_ > iter.ptr_; +} + + +template +inline bool Foam::PtrList::const_iterator::operator<= +( + const const_iterator& iter +) const +{ + return ptr_ <= iter.ptr_; +} + + +template +inline bool Foam::PtrList::const_iterator::operator>= +( + const const_iterator& iter +) const +{ + return ptr_ >= iter.ptr_; +} + + +template +inline typename Foam::PtrList::const_iterator +Foam::PtrList::begin() const +{ + return ptrs_.begin(); +} + + +template +inline typename Foam::PtrList::const_iterator +Foam::PtrList::end() const +{ + return ptrs_.end(); +} + + +template +inline typename Foam::PtrList::const_iterator +Foam::PtrList::cbegin() const +{ + return ptrs_.begin(); +} + + +template +inline typename Foam::PtrList::const_iterator +Foam::PtrList::cend() const +{ + return ptrs_.end(); +} + + // ************************************************************************* //