fix Cursor's copy constructor
authorAdam Simpkins <simpkins@fb.com>
Thu, 6 Mar 2014 00:28:09 +0000 (16:28 -0800)
committerDave Watson <davejwatson@fb.com>
Tue, 18 Mar 2014 17:01:34 +0000 (10:01 -0700)
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

index f893d5a437937911e25329eb8ee28beef00b5c09..2e90d0816b843aa80ac3ec09b4b0e6145de962bf 100644 (file)
@@ -199,12 +199,17 @@ class CursorBase {
   // Make all the templated classes friends for copy constructor.
   template <class D, typename B> friend class CursorBase;
 
-  template <class T>
-  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 <class OtherDerived, class OtherBuf>
+  explicit CursorBase(const CursorBase<OtherDerived, OtherBuf>& 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<Cursor, const IOBuf> {
   explicit Cursor(const IOBuf* buf)
     : detail::CursorBase<Cursor, const IOBuf>(buf) {}
 
-  template <class CursorType>
-  explicit Cursor(CursorType& cursor)
+  template <class OtherDerived, class OtherBuf>
+  explicit Cursor(const detail::CursorBase<OtherDerived, OtherBuf>& cursor)
     : detail::CursorBase<Cursor, const IOBuf>(cursor) {}
 };
 
@@ -481,8 +486,8 @@ class RWCursor
     : detail::CursorBase<RWCursor<access>, IOBuf>(buf),
       maybeShared_(true) {}
 
-  template <class CursorType>
-  explicit RWCursor(CursorType& cursor)
+  template <class OtherDerived, class OtherBuf>
+  explicit RWCursor(const detail::CursorBase<OtherDerived, OtherBuf>& cursor)
     : detail::CursorBase<RWCursor<access>, IOBuf>(cursor),
       maybeShared_(true) {}
   /**