From 165cfc7fcdcf8a818160dc47e5317368b04a8daa Mon Sep 17 00:00:00 2001 From: Ichthyostega Date: Fri, 17 Jul 2009 04:13:14 +0200 Subject: [PATCH] WIP allow some conversions, attempt to define the const_iterator properly --- src/lib/iter-adapter.hpp | 65 ++++++++++++++++++++++++++----- src/lib/scoped-ptrvect.hpp | 10 +++-- tests/lib/scoped-ptrvect-test.cpp | 20 ++++++++++ 3 files changed, 82 insertions(+), 13 deletions(-) diff --git a/src/lib/iter-adapter.hpp b/src/lib/iter-adapter.hpp index 6744ed23c..4de2c994a 100644 --- a/src/lib/iter-adapter.hpp +++ b/src/lib/iter-adapter.hpp @@ -258,6 +258,16 @@ namespace lib { { } + /** allow copy, + * when the underlying iterators + * are compatible or convertible */ + template + RangeIter (I2 const& oIter) + : p_(oIter.getPos()) + , e_(oIter.getEnd()) + { } + + /* === lumiera forward iterator concept === */ reference @@ -299,6 +309,12 @@ namespace lib { return !isValid(); } + + /** access wrapped STL iterator */ + const IT& getPos() const { return p_; } + const IT& getEnd() const { return e_; } + + private: void @@ -308,18 +324,13 @@ namespace lib { throw lumiera::error::Invalid ("Can't iterate further", lumiera::error::LUMIERA_ERROR_ITER_EXHAUST); } - - - /// comparison operator is allowed to access the underlying impl iterator - template - friend bool operator== (RangeIter const&, RangeIter const&); }; /// Supporting equality comparisons... template - bool operator== (RangeIter const& il, RangeIter const& ir) { return (!il && !ir) || (il.p_ == ir.p_); } + bool operator== (RangeIter const& il, RangeIter const& ir) { return (!il && !ir) || (il.getPos() == ir.getPos()); } template bool operator!= (RangeIter const& il, RangeIter const& ir) { return !(il == ir); } @@ -374,6 +385,14 @@ namespace lib { { } + /** allow copy initialisation also + * when the base iter types are convertible */ + template + PtrDerefIter (I2 const& oIter) + : i_(reinterpret_cast (oIter.getBase())) /////////////////////////////TODO: properly guard this dangerous conversion by a traits template; the idea is to allow this conversion only for the initialisation of a "const iterator" from its sister type + { } + + /* === lumiera forward iterator concept === */ reference @@ -414,19 +433,45 @@ namespace lib { } - /// comparison operator is allowed to access the underlying impl iterator - template - friend bool operator== (PtrDerefIter const&, PtrDerefIter const&); + /** access the wrapped implementation iterator */ + IT const& + getBase() const + { + return i_; + } }; /// Supporting equality comparisons... template - bool operator== (PtrDerefIter const& il, PtrDerefIter const& ir) { return il.i_ == ir.i_; } + bool operator== (PtrDerefIter const& il, PtrDerefIter const& ir) { return il.getBase() == ir.getBase(); } template bool operator!= (PtrDerefIter const& il, PtrDerefIter const& ir) { return !(il == ir); } + + /** + * Helper for type rewritings: + * get the element type for an iterator like entity + */ + template + struct IterType; + + template class Iter, class TY, class CON> + struct IterType > + { + typedef CON Container; + typedef TY ElemType; + + typedef typename RemovePtr::Type Type; + + template + struct SimilarIter + { + typedef Iter Type; + }; + }; + } // namespace lib #endif diff --git a/src/lib/scoped-ptrvect.hpp b/src/lib/scoped-ptrvect.hpp index d01992edc..e10704679 100644 --- a/src/lib/scoped-ptrvect.hpp +++ b/src/lib/scoped-ptrvect.hpp @@ -72,7 +72,8 @@ namespace lib { { typedef std::vector _Vec; typedef typename _Vec::iterator VIter; - typedef typename _Vec::const_iterator VcIter; +// typedef typename _Vec::const_iterator VcIter; + typedef typename IterType::template SimilarIter::Type VcIter; typedef RangeIter RIter; typedef RangeIter RcIter; @@ -83,6 +84,9 @@ namespace lib { typedef T & reference; typedef T const& const_reference; + typedef typename IterType::Type Tupe; + + ScopedPtrVect () : _Vec() @@ -155,8 +159,8 @@ namespace lib { iterator begin() { return iterator (allPtrs()); } const_iterator begin() const { return const_iterator (allPtrs()); } - iterator end() { return iterator (); } - const_iterator end() const { return const_iterator (); } + iterator end() { return iterator ( RIter() ); } + const_iterator end() const { return const_iterator (RcIter() ); } diff --git a/tests/lib/scoped-ptrvect-test.cpp b/tests/lib/scoped-ptrvect-test.cpp index d718ddc31..d8d051727 100644 --- a/tests/lib/scoped-ptrvect-test.cpp +++ b/tests/lib/scoped-ptrvect-test.cpp @@ -28,6 +28,13 @@ #include "lib/scoped-ptrvect.hpp" #include "testdummy.hpp" +//////////////////////////////////////////////TODO test code +#include "lib/test/test-helper.hpp" +using lib::test::showSizeof; +#include +using std::cout; +using std::endl; +//////////////////////////////////////////////TODO test code namespace lib { namespace test{ @@ -111,6 +118,19 @@ namespace test{ ++check; ++ii; } + check = 0; + +///////////////////////////////////////////////////////////////////TODO test code + cout << showSizeof () << endl; +///////////////////////////////////////////////////////////////////TODO test code + + VectD::const_iterator cii = holder.begin(); + while (cii) + { + ASSERT (check == cii->getVal()); + ++check; + ++cii; + } } ASSERT (0==checksum); }