Commit bdcc37cd authored by Robert Bradshaw's avatar Robert Bradshaw

Shared pointer fixes.

Coersion to subclasses prefered in overload resolution to coersion to nullptr_t.
parent 1b7a91a2
...@@ -3572,6 +3572,14 @@ class CppClassType(CType): ...@@ -3572,6 +3572,14 @@ class CppClassType(CType):
return 1 return 1
return 0 return 0
def subclass_dist(self, super_type):
if self.same_as_resolved_type(super_type):
return 0
elif not self.base_classes:
return float('inf')
else:
return 1 + min(b.subclass_dist(super_type) for b in self.base_classes)
def same_as_resolved_type(self, other_type): def same_as_resolved_type(self, other_type):
if other_type.is_cpp_class: if other_type.is_cpp_class:
if self == other_type: if self == other_type:
...@@ -4135,7 +4143,7 @@ def best_match(arg_types, functions, pos=None, env=None, args=None): ...@@ -4135,7 +4143,7 @@ def best_match(arg_types, functions, pos=None, env=None, args=None):
needed_coercions = {} needed_coercions = {}
for index, (func, func_type) in enumerate(candidates): for index, (func, func_type) in enumerate(candidates):
score = [0,0,0,0] score = [0,0,0,0,0,0,0]
for i in range(min(actual_nargs, len(func_type.args))): for i in range(min(actual_nargs, len(func_type.args))):
src_type = arg_types[i] src_type = arg_types[i]
dst_type = func_type.args[i].type dst_type = func_type.args[i].type
...@@ -4169,6 +4177,13 @@ def best_match(arg_types, functions, pos=None, env=None, args=None): ...@@ -4169,6 +4177,13 @@ def best_match(arg_types, functions, pos=None, env=None, args=None):
(src_type.is_float and dst_type.is_float)): (src_type.is_float and dst_type.is_float)):
score[2] += abs(dst_type.rank + (not dst_type.signed) - score[2] += abs(dst_type.rank + (not dst_type.signed) -
(src_type.rank + (not src_type.signed))) + 1 (src_type.rank + (not src_type.signed))) + 1
elif dst_type.is_ptr and src_type.is_ptr:
if dst_type.base_type == c_void_type:
score[4] += 1
elif src_type.base_type.is_cpp_class and src_type.base_type.is_subclass(dst_type.base_type):
score[6] += src_type.base_type.subclass_dist(dst_type.base_type)
else:
score[5] += 1
elif not src_type.is_pyobject: elif not src_type.is_pyobject:
score[1] += 1 score[1] += 1
else: else:
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
# mode: run # mode: run
# tag: cpp, werror # tag: cpp, werror
from libcpp.memory cimport unique_ptr, shared_ptr, default_delete from libcpp.memory cimport unique_ptr, shared_ptr, default_delete
from libcpp cimport nullptr from libcpp cimport nullptr
cdef extern from "cpp_smart_ptr_helper.h": cdef extern from "cpp_smart_ptr_helper.h":
...@@ -33,7 +33,7 @@ def test_unique_ptr(): ...@@ -33,7 +33,7 @@ def test_unique_ptr():
x_ptr2.reset() x_ptr2.reset()
assert alloc_count == 1 assert alloc_count == 1
assert dealloc_count == 1 assert dealloc_count == 1
alloc_count = 0 alloc_count = 0
dealloc_count = 0 dealloc_count = 0
cdef unique_ptr[CountAllocDealloc,FreePtr[CountAllocDealloc]] x_ptr3 cdef unique_ptr[CountAllocDealloc,FreePtr[CountAllocDealloc]] x_ptr3
...@@ -41,3 +41,37 @@ def test_unique_ptr(): ...@@ -41,3 +41,37 @@ def test_unique_ptr():
assert x_ptr3.get() != nullptr; assert x_ptr3.get() != nullptr;
x_ptr3.reset() x_ptr3.reset()
assert x_ptr3.get() == nullptr; assert x_ptr3.get() == nullptr;
def test_shared_ptr():
"""
>>> test_shared_ptr()
"""
cdef int alloc_count = 0, dealloc_count = 0
cdef shared_ptr[CountAllocDealloc] ptr = shared_ptr[CountAllocDealloc](
new CountAllocDealloc(&alloc_count, &dealloc_count))
assert alloc_count == 1
assert dealloc_count == 0
cdef shared_ptr[CountAllocDealloc] ptr2 = ptr
assert alloc_count == 1
assert dealloc_count == 0
ptr.reset()
assert alloc_count == 1
assert dealloc_count == 0
ptr2.reset()
assert alloc_count == 1
assert dealloc_count == 1
cdef cppclass A:
pass
cdef cppclass B(A):
pass
cdef cppclass C(B):
pass
cdef shared_ptr[A] holding_subclass = shared_ptr[A](new C())
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