From: Kefu Chai Date: Tue, 8 Mar 2016 11:41:51 +0000 (+0800) Subject: buffer: add symmetry operator==() and operator!=() X-Git-Tag: v10.1.0~120^2 X-Git-Url: http://git.apps.os.sepia.ceph.com/?a=commitdiff_plain;h=18e33f243eb12608b85a06810fb1ac95eb49bae8;p=ceph.git buffer: add symmetry operator==() and operator!=() * add const_iterator(const iterator&) so an iterator can be converted to const_iterator * add tests for operator==() and operator!=() Signed-off-by: Kefu Chai --- diff --git a/src/common/buffer.cc b/src/common/buffer.cc index d656419ac8cc2..0368979998cf7 100644 --- a/src/common/buffer.cc +++ b/src/common/buffer.cc @@ -1036,6 +1036,10 @@ static simple_spinlock_t buffer_debug_lock = SIMPLE_SPINLOCK_INITIALIZER; advance(o); } + template + buffer::list::iterator_impl::iterator_impl(const buffer::list::iterator& i) + : iterator_impl(i.bl, i.off, i.p, i.p_off) {} + template void buffer::list::iterator_impl::advance(int o) { @@ -1083,12 +1087,6 @@ static simple_spinlock_t buffer_debug_lock = SIMPLE_SPINLOCK_INITIALIZER; advance(o); } - template - bool buffer::list::iterator_impl::operator!=(const buffer::list::iterator_impl& rhs) const - { - return bl != rhs.bl || off != rhs.off; - } - template char buffer::list::iterator_impl::operator*() const { @@ -1315,7 +1313,6 @@ static simple_spinlock_t buffer_debug_lock = SIMPLE_SPINLOCK_INITIALIZER; } } - // -- buffer::list -- buffer::list::list(list&& other) diff --git a/src/include/buffer.h b/src/include/buffer.h index d765a76c4cb68..1b1393ae68dff 100644 --- a/src/include/buffer.h +++ b/src/include/buffer.h @@ -263,6 +263,10 @@ namespace buffer CEPH_BUFFER_API { unsigned _memcopy_count; //the total of memcopy using rebuild(). ptr append_buffer; // where i put small appends. + public: + class iterator; + + private: template class CEPH_BUFFER_DETAILS iterator_impl : public std::iterator { @@ -281,6 +285,7 @@ namespace buffer CEPH_BUFFER_API { unsigned off; // in bl list_iter_t p; unsigned p_off; // in *p + friend class iterator_impl; public: // constructor. position. @@ -289,6 +294,7 @@ namespace buffer CEPH_BUFFER_API { iterator_impl(bl_t *l, unsigned o=0); iterator_impl(bl_t *l, unsigned o, list_iter_t ip, unsigned po) : bl(l), ls(&bl->_buffers), off(o), p(ip), p_off(po) {} + iterator_impl(const list::iterator& i); /// get current iterator offset in buffer::list unsigned get_off() const { return off; } @@ -304,12 +310,11 @@ namespace buffer CEPH_BUFFER_API { void advance(int o); void seek(unsigned o); - bool operator!=(const iterator_impl& rhs) const; char operator*() const; iterator_impl& operator++(); ptr get_current_ptr() const; - bl_t& get_bl() { return *bl; } + bl_t& get_bl() const { return *bl; } // copy data out. // note that these all _append_ to dest! @@ -318,6 +323,15 @@ namespace buffer CEPH_BUFFER_API { void copy(unsigned len, list &dest); void copy(unsigned len, std::string &dest); void copy_all(list &dest); + + friend bool operator==(const iterator_impl& lhs, + const iterator_impl& rhs) { + return &lhs.get_bl() == &rhs.get_bl() && lhs.get_off() == rhs.get_off(); + } + friend bool operator!=(const iterator_impl& lhs, + const iterator_impl& rhs) { + return &lhs.get_bl() != &rhs.get_bl() || lhs.get_off() != rhs.get_off(); + } }; public: @@ -346,6 +360,13 @@ namespace buffer CEPH_BUFFER_API { void copy_in(unsigned len, const char *src); void copy_in(unsigned len, const char *src, bool crc_reset); void copy_in(unsigned len, const list& otherl); + + bool operator==(const iterator& rhs) const { + return bl == rhs.bl && off == rhs.off; + } + bool operator!=(const iterator& rhs) const { + return bl != rhs.bl || off != rhs.off; + } }; private: @@ -631,6 +652,7 @@ inline bufferhash& operator<<(bufferhash& l, bufferlist &r) { l.update(r); return l; } + } #if defined(HAVE_XIO) diff --git a/src/test/bufferlist.cc b/src/test/bufferlist.cc index a6120fd061414..a2646e30407c8 100644 --- a/src/test/bufferlist.cc +++ b/src/test/bufferlist.cc @@ -933,6 +933,18 @@ TEST(BufferListIterator, constructors) { j.advance(-1); EXPECT_EQ('X', *j); } + + // + // const_iterator(const iterator& other) + // + { + bufferlist bl; + bl.append("ABC", 3); + bufferlist::iterator i(&bl); + bufferlist::const_iterator ci(i); + EXPECT_EQ(0u, ci.get_off()); + EXPECT_EQ('A', *ci); + } } TEST(BufferListIterator, empty_create_append_copy) { @@ -946,7 +958,7 @@ TEST(BufferListIterator, empty_create_append_copy) { ASSERT_TRUE(out.contents_equal(bl)); } -TEST(BufferListIterator, operator_equal) { +TEST(BufferListIterator, operator_assign) { bufferlist bl; bl.append("ABC", 3); bufferlist::iterator i(&bl, 1); @@ -1038,6 +1050,49 @@ TEST(BufferListIterator, operator_star) { } } +TEST(BufferListIterator, operator_equal) { + bufferlist bl; + bl.append("ABC", 3); + { + bufferlist::iterator i(&bl); + bufferlist::iterator j(&bl); + EXPECT_EQ(i, j); + } + { + bufferlist::const_iterator ci = bl.begin(); + bufferlist::iterator i = bl.begin(); + EXPECT_EQ(i, ci); + EXPECT_EQ(ci, i); + } +} + +TEST(BufferListIterator, operator_nequal) { + bufferlist bl; + bl.append("ABC", 3); + { + bufferlist::iterator i(&bl); + bufferlist::iterator j(&bl); + EXPECT_NE(++i, j); + } + { + bufferlist::const_iterator ci = bl.begin(); + bufferlist::const_iterator cj = bl.begin(); + ++ci; + EXPECT_NE(ci, cj); + bufferlist::iterator i = bl.begin(); + EXPECT_NE(i, ci); + EXPECT_NE(ci, i); + } + { + // tests begin(), end(), operator++() also + string s("ABC"); + int i = 0; + for (auto c : bl) { + EXPECT_EQ(s[i++], c); + } + } +} + TEST(BufferListIterator, operator_plus_plus) { bufferlist bl; { @@ -1049,7 +1104,7 @@ TEST(BufferListIterator, operator_plus_plus) { bufferlist::iterator i(&bl); ++i; EXPECT_EQ('B', *i); - } + } } TEST(BufferListIterator, get_current_ptr) {