From c8ad790f81721ce7faee85274ff5ae80124ce674 Mon Sep 17 00:00:00 2001 From: Adam Simpkins Date: Wed, 5 Mar 2014 16:28:09 -0800 Subject: [PATCH] fix Cursor's copy constructor Summary: The Cursor class didn't provide a copy constructor accepting a "const Cursor&". The existing constructor only accepted references to non-const Cursor. This updates the constructor to also accept "const Cursor&". I also made the template parameter checking more strict, so that it only accepts other CursorBase classes. This prevents the compiler from thinking that a Cursor can be constructed from any other possible type when performing overload resolution. Test Plan: Ran all of the folly cursor tests. Reviewed By: davejwatson@fb.com FB internal diff: D1208428 --- folly/io/Cursor.h | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/folly/io/Cursor.h b/folly/io/Cursor.h index f893d5a4..2e90d081 100644 --- a/folly/io/Cursor.h +++ b/folly/io/Cursor.h @@ -199,12 +199,17 @@ class CursorBase { // Make all the templated classes friends for copy constructor. template friend class CursorBase; - template - explicit CursorBase(const T& cursor) { - crtBuf_ = cursor.crtBuf_; - offset_ = cursor.offset_; - buffer_ = cursor.buffer_; - } + /* + * Copy constructor. + * + * This also allows constructing a CursorBase from other derived types. + * For instance, this allows constructing a Cursor from an RWPrivateCursor. + */ + template + explicit CursorBase(const CursorBase& cursor) + : crtBuf_(cursor.crtBuf_), + offset_(cursor.offset_), + buffer_(cursor.buffer_) {} // reset cursor to point to a new buffer. void reset(BufType* buf) { @@ -461,8 +466,8 @@ class Cursor : public detail::CursorBase { explicit Cursor(const IOBuf* buf) : detail::CursorBase(buf) {} - template - explicit Cursor(CursorType& cursor) + template + explicit Cursor(const detail::CursorBase& cursor) : detail::CursorBase(cursor) {} }; @@ -481,8 +486,8 @@ class RWCursor : detail::CursorBase, IOBuf>(buf), maybeShared_(true) {} - template - explicit RWCursor(CursorType& cursor) + template + explicit RWCursor(const detail::CursorBase& cursor) : detail::CursorBase, IOBuf>(cursor), maybeShared_(true) {} /** -- 2.34.1