From 12f76357ac9e9d5f6c50074c365bef04cb5d25b8 Mon Sep 17 00:00:00 2001 From: Kefu Chai Date: Tue, 15 May 2018 23:45:26 +0800 Subject: [PATCH] buffer: add cbegin() methods Signed-off-by: Kefu Chai --- src/include/buffer.h | 53 +++++++++++++++++++++++--------------------- 1 file changed, 28 insertions(+), 25 deletions(-) diff --git a/src/include/buffer.h b/src/include/buffer.h index 41b69508675..606025005b0 100644 --- a/src/include/buffer.h +++ b/src/include/buffer.h @@ -181,51 +181,41 @@ namespace buffer CEPH_BUFFER_API { void release(); - public: - class iterator { + template + class iterator_impl { const ptr *bp; ///< parent ptr const char *start; ///< starting pointer into bp->c_str() const char *pos; ///< pointer into bp->c_str() const char *end_ptr; ///< pointer to bp->end_c_str() - bool deep; ///< if true, do not allow shallow ptr copies + const bool deep; ///< if true, do not allow shallow ptr copies - iterator(const ptr *p, size_t offset, bool d) + iterator_impl(std::conditional_t p, + size_t offset, bool d) : bp(p), start(p->c_str() + offset), pos(start), end_ptr(p->end_c_str()), - deep(d) {} + deep(d) + {} friend class ptr; public: - const char *get_pos_add(size_t n) { - const char *r = pos; - pos += n; - if (pos > end_ptr) - throw end_of_buffer(); + using pointer = std::conditional_t; + pointer get_pos_add(size_t n) { + auto r = pos; + advance(n); return r; } - ptr get_ptr(size_t len) { if (deep) { return buffer::copy(get_pos_add(len), len); } else { size_t off = pos - bp->c_str(); - pos += len; - if (pos > end_ptr) - throw end_of_buffer(); + advance(len); return ptr(*bp, off, len); } } - ptr get_preceding_ptr(size_t len) { - if (deep) { - return buffer::copy(get_pos() - len, len); - } else { - size_t off = pos - bp->c_str(); - return ptr(*bp, off - len, len); - } - } void advance(size_t len) { pos += len; @@ -249,6 +239,10 @@ namespace buffer CEPH_BUFFER_API { } }; + public: + using const_iterator = iterator_impl; + using iterator = iterator_impl; + ptr() : _raw(0), _off(0), _len(0) {} // cppcheck-suppress noExplicitConstructor ptr(raw *r); @@ -270,11 +264,17 @@ namespace buffer CEPH_BUFFER_API { void swap(ptr& other) noexcept; ptr& make_shareable(); - iterator begin(size_t offset=0) const { + iterator begin(size_t offset=0) { return iterator(this, offset, false); } - iterator begin_deep(size_t offset=0) const { - return iterator(this, offset, true); + const_iterator begin(size_t offset=0) const { + return const_iterator(this, offset, false); + } + const_iterator cbegin() const { + return begin(); + } + const_iterator begin_deep(size_t offset=0) const { + return const_iterator(this, 0, true); } // misc @@ -822,6 +822,9 @@ namespace buffer CEPH_BUFFER_API { const_iterator begin() const { return const_iterator(this, 0); } + const_iterator cbegin() const { + return begin(); + } const_iterator end() const { return const_iterator(this, _len, _buffers.end(), 0); }