Commit e2d0d129 authored by Robert Bradshaw's avatar Robert Bradshaw

Add C++11 smart pointer casts.

parent 23c68023
...@@ -107,3 +107,9 @@ cdef extern from "<memory>" namespace "std" nogil: ...@@ -107,3 +107,9 @@ cdef extern from "<memory>" namespace "std" nogil:
# Temporaries used for exception handling break generated code # Temporaries used for exception handling break generated code
unique_ptr[T] make_unique[T](...) # except + unique_ptr[T] make_unique[T](...) # except +
# No checking on the compatibility of T and U.
cdef shared_ptr[T] static_pointer_cast[T, U](const shared_ptr[U]&)
cdef shared_ptr[T] dynamic_pointer_cast[T, U](const shared_ptr[U]&)
cdef shared_ptr[T] const_pointer_cast[T, U](const shared_ptr[U]&)
cdef shared_ptr[T] reinterpret_pointer_cast[T, U](const shared_ptr[U]&)
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
# tag: cpp, werror, cpp11 # tag: cpp, werror, cpp11
# distutils: extra_compile_args=-std=c++0x # distutils: extra_compile_args=-std=c++0x
from libcpp.memory cimport unique_ptr, shared_ptr, default_delete from libcpp.memory cimport unique_ptr, shared_ptr, default_delete, dynamic_pointer_cast
from libcpp cimport nullptr from libcpp cimport nullptr
cdef extern from "cpp_smart_ptr_helper.h": cdef extern from "cpp_smart_ptr_helper.h":
...@@ -71,7 +71,8 @@ def test_const_shared_ptr(): ...@@ -71,7 +71,8 @@ def test_const_shared_ptr():
cdef cppclass A: cdef cppclass A:
pass void some_method(): # Force this to be a polymorphic class for dynamic cast.
pass
cdef cppclass B(A): cdef cppclass B(A):
pass pass
...@@ -80,3 +81,15 @@ cdef cppclass C(B): ...@@ -80,3 +81,15 @@ cdef cppclass C(B):
pass pass
cdef shared_ptr[A] holding_subclass = shared_ptr[A](new C()) cdef shared_ptr[A] holding_subclass = shared_ptr[A](new C())
def test_dynamic_pointer_cast():
"""
>>> test_dynamic_pointer_cast()
"""
cdef shared_ptr[B] b = shared_ptr[B](new B())
cdef shared_ptr[A] a = dynamic_pointer_cast[A, B](b)
assert a.get() == b.get()
a = shared_ptr[A](new A())
b = dynamic_pointer_cast[B, A](a)
assert b.get() == NULL
...@@ -14,7 +14,7 @@ class CountAllocDealloc { ...@@ -14,7 +14,7 @@ class CountAllocDealloc {
template<typename T> template<typename T>
struct FreePtr { struct FreePtr {
void operator()( T * t ) noexcept void operator()( T * t )
{ {
if(t != nullptr) { if(t != nullptr) {
delete t; delete t;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment