From 3b6268a4fc6bbdff69fc1d2b58f53a0f28e38d01 Mon Sep 17 00:00:00 2001 From: Michael Davidsaver Date: Thu, 30 May 2013 19:16:22 -0400 Subject: [PATCH] add const_iterator to shared_vector --- pvDataApp/misc/sharedVector.h | 15 ++++++++++++--- testApp/misc/testSharedVector.cpp | 21 ++++++++++++++++++++- 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/pvDataApp/misc/sharedVector.h b/pvDataApp/misc/sharedVector.h index e5098a8..9d9cc5e 100644 --- a/pvDataApp/misc/sharedVector.h +++ b/pvDataApp/misc/sharedVector.h @@ -32,6 +32,10 @@ namespace detail { } }; + // avoid adding 'const' twice + template struct decorate_const { typedef const T type; }; + template struct decorate_const { typedef const T type; }; + /* All the parts of shared_vector which * don't need special handling for E=void */ @@ -193,9 +197,12 @@ class shared_vector : public detail::shared_vector_base public: typedef E value_type; typedef E& reference; + typedef typename detail::decorate_const::type& const_reference; typedef E* pointer; typedef E* iterator; typedef std::reverse_iterator reverse_iterator; + typedef typename detail::decorate_const::type* const_iterator; + typedef std::reverse_iterator const_reverse_iterator; typedef ptrdiff_t difference_type; typedef size_t size_type; @@ -296,7 +303,7 @@ public: if(this->m_data && this->m_data.unique()) { // we have data and exclusive ownership of it if(i<=this->m_total) { - // We have room to grow! + // We have room to grow (or shrink)! this->m_count = i; return; } @@ -364,12 +371,16 @@ public: // STL iterators iterator begin() const{return this->m_data.get()+this->m_offset;} + const_iterator cbegin() const{return begin();} iterator end() const{return this->m_data.get()+this->m_offset+this->m_count;} + const_iterator cend() const{return end();} reverse_iterator rbegin() const{return reverse_iterator(end());} + const_reverse_iterator crbegin() const{return rbegin();} reverse_iterator rend() const{return reverse_iterator(begin());} + const_reverse_iterator crend() const{return rend();} reference front() const{return (*this)[0];} reference back() const{return (*this)[this->m_count-1];} @@ -636,8 +647,6 @@ std::ostream& operator<<(std::ostream& strm, const epics::pvData::shared_vector< * Therefore the get_allocator() method and the allocator_type typedef are * not provided. * - * shared_vector does not provide the const_* typedefs. - * * The assign() method and the related constructor are not implemented * at this time. * diff --git a/testApp/misc/testSharedVector.cpp b/testApp/misc/testSharedVector.cpp index c76e350..71fd943 100644 --- a/testApp/misc/testSharedVector.cpp +++ b/testApp/misc/testSharedVector.cpp @@ -206,9 +206,28 @@ static void testConst() testOk1(rodata[1]==100); + // intentionally modify shared data! + // safe since this thread owns all references. writable[1]=200; testOk1(rodata[1]==200); + + epics::pvData::shared_vector::const_iterator a; + epics::pvData::shared_vector::iterator b; + epics::pvData::shared_vector::const_iterator c; + + a = writable.begin(); + b = rodata.begin(); + c = rodata.end(); + + testOk1(std::equal(b, c, a)); + + a = writable.cbegin(); + c = rodata.cend(); + + epics::pvData::shared_vector::const_reference x = rodata[1]; + + testOk1(x==200); } static void testSlice() @@ -318,7 +337,7 @@ static void testVoid() MAIN(testSharedVector) { - testPlan(99); + testPlan(101); testDiag("Tests for shared_vector"); testDiag("sizeof(shared_vector)=%lu",