RCPtr: Add dynamic_pointer_cast 52/76552/2
authorDave Rigby <daver@couchbase.com>
Wed, 5 Apr 2017 16:46:08 +0000 (17:46 +0100)
committerDave Rigby <daver@couchbase.com>
Tue, 18 Apr 2017 11:50:03 +0000 (11:50 +0000)
Allows dynamic cast from one RCPtr instantiation to another. Based on
same function for std::shared_ptr.

Change-Id: Idc8723ea90dfd2843a3c5b602f5e42fad7a36613
Reviewed-on: http://review.couchbase.org/76552
Tested-by: Build Bot <build@couchbase.com>
Reviewed-by: James Harrison <james.harrison@couchbase.com>
Reviewed-by: Daniel Owen <owend@couchbase.com>
src/atomic.h

index d21068c..89e1438 100644 (file)
@@ -221,6 +221,16 @@ private:
 };
 
 /**
+ * Dynamic cast for RCPtr. Modelled on method of the same name for
+ * std::shared_ptr.
+ */
+template <class T, class U>
+RCPtr<T> dynamic_pointer_cast(const RCPtr<U>& r) {
+    T* p = dynamic_cast<T*>(r.get());
+    return p ? RCPtr<T>(p) : RCPtr<T>();
+}
+
+/**
  * Single-threaded reference counted pointer.
  * "Single-threaded" means that the reference counted pointer should be accessed
  * by only one thread at any time or accesses to the reference counted pointer