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:
# Temporaries used for exception handling break generated code
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 @@
# tag: cpp, werror, cpp11
# 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
cdef extern from "cpp_smart_ptr_helper.h":
......@@ -71,6 +71,7 @@ def test_const_shared_ptr():
cdef cppclass A:
void some_method(): # Force this to be a polymorphic class for dynamic cast.
pass
cdef cppclass B(A):
......@@ -80,3 +81,15 @@ cdef cppclass C(B):
pass
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 {
template<typename T>
struct FreePtr {
void operator()( T * t ) noexcept
void operator()( T * t )
{
if(t != nullptr) {
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