]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
buffer: add symmetry operator==() and operator!=() 7974/head
authorKefu Chai <kchai@redhat.com>
Tue, 8 Mar 2016 11:41:51 +0000 (19:41 +0800)
committerKefu Chai <kchai@redhat.com>
Wed, 9 Mar 2016 07:35:17 +0000 (15:35 +0800)
* 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 <kchai@redhat.com>
src/common/buffer.cc
src/include/buffer.h
src/test/bufferlist.cc

index d656419ac8cc2e18b8e0c59f4c63efb00a9b9536..0368979998cf7000fe2eb7b5cc35ff64e3298673 100644 (file)
@@ -1036,6 +1036,10 @@ static simple_spinlock_t buffer_debug_lock = SIMPLE_SPINLOCK_INITIALIZER;
     advance(o);
   }
 
+  template<bool is_const>
+  buffer::list::iterator_impl<is_const>::iterator_impl(const buffer::list::iterator& i)
+    : iterator_impl<is_const>(i.bl, i.off, i.p, i.p_off) {}
+
   template<bool is_const>
   void buffer::list::iterator_impl<is_const>::advance(int o)
   {
@@ -1083,12 +1087,6 @@ static simple_spinlock_t buffer_debug_lock = SIMPLE_SPINLOCK_INITIALIZER;
     advance(o);
   }
 
-  template<bool is_const>
-  bool buffer::list::iterator_impl<is_const>::operator!=(const buffer::list::iterator_impl<is_const>& rhs) const
-  {
-    return bl != rhs.bl || off != rhs.off;
-  }
-
   template<bool is_const>
   char buffer::list::iterator_impl<is_const>::operator*() const
   {
@@ -1315,7 +1313,6 @@ static simple_spinlock_t buffer_debug_lock = SIMPLE_SPINLOCK_INITIALIZER;
     }
   }
 
-
   // -- buffer::list --
 
   buffer::list::list(list&& other)
index d765a76c4cb683696edbff51966c1ff183327bfc..1b1393ae68dff75730c5335e1dca4e7f4492e30c 100644 (file)
@@ -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 <bool is_const>
     class CEPH_BUFFER_DETAILS iterator_impl
       : public std::iterator<std::forward_iterator_tag, char> {
@@ -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<true>;
 
     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)
index a6120fd0614141e7583f1f71cf3c1b5456fa72b6..a2646e30407c8081e6e155cb9d4e525b9f08cff7 100644 (file)
@@ -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) {