diff options
Diffstat (limited to 'base/memory')
-rw-r--r-- | base/memory/scoped_ptr.h | 25 | ||||
-rw-r--r-- | base/memory/scoped_ptr_unittest.nc | 11 |
2 files changed, 36 insertions, 0 deletions
diff --git a/base/memory/scoped_ptr.h b/base/memory/scoped_ptr.h index 77a2039..bc38c8d 100644 --- a/base/memory/scoped_ptr.h +++ b/base/memory/scoped_ptr.h @@ -96,8 +96,30 @@ #include <stddef.h> #include <stdlib.h> +#include "base/basictypes.h" #include "base/compiler_specific.h" #include "base/move.h" +#include "base/template_util.h" + +namespace base { + +namespace subtle { +class RefCountedBase; +class RefCountedThreadSafeBase; +} // namespace subtle + +namespace internal { + +template <typename T> struct IsNotRefCounted { + enum { + value = !base::is_convertible<T*, base::subtle::RefCountedBase*>::value && + !base::is_convertible<T*, base::subtle::RefCountedThreadSafeBase*>:: + value + }; +}; + +} // namespace internal +} // namespace base // A scoped_ptr<T> is like a T*, except that the destructor of scoped_ptr<T> // automatically deletes the pointer it holds (if any). @@ -112,6 +134,9 @@ template <class C> class scoped_ptr { MOVE_ONLY_TYPE_FOR_CPP_03(scoped_ptr, RValue) + COMPILE_ASSERT(base::internal::IsNotRefCounted<C>::value, + C_is_refcounted_type_and_needs_scoped_refptr); + public: // The element type diff --git a/base/memory/scoped_ptr_unittest.nc b/base/memory/scoped_ptr_unittest.nc index 841d03c..30a332e 100644 --- a/base/memory/scoped_ptr_unittest.nc +++ b/base/memory/scoped_ptr_unittest.nc @@ -4,6 +4,7 @@ #include "base/basictypes.h" #include "base/memory/scoped_ptr.h" +#include "base/memory/ref_counted.h" namespace { @@ -13,6 +14,9 @@ class Parent { class Child : public Parent { }; +class RefCountedClass : public base::RefCountedThreadSafe<RefCountedClass> { +}; + } // namespace #if defined(NCTEST_NO_PASSAS_DOWNCAST) // [r"invalid conversion from"] @@ -21,4 +25,11 @@ scoped_ptr<Child> DowncastUsingPassAs(scoped_ptr<Parent> object) { return object.PassAs<Child>(); } +#elif defined(NCTEST_NO_REF_COUNTED_SCOPED_PTR) // [r"creating array with negative size"] + +// scoped_ptr<> should not work for ref-counted objects. +void WontCompile() { + scoped_ptr<RefCountedClass> x; +} + #endif |