Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
gaoqiong
pybind11
Commits
614988c8
Commit
614988c8
authored
Sep 05, 2016
by
Wenzel Jakob
Committed by
GitHub
Sep 05, 2016
Browse files
Merge pull request #384 from jagerman/unique-ptr-non-default-deleters
Make unique_ptr's with non-default deleters work
parents
cc4e4065
a6495af8
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
28 additions
and
2 deletions
+28
-2
include/pybind11/cast.h
include/pybind11/cast.h
+2
-2
tests/test_smart_ptr.cpp
tests/test_smart_ptr.cpp
+16
-0
tests/test_smart_ptr.py
tests/test_smart_ptr.py
+10
-0
No files found.
include/pybind11/cast.h
View file @
614988c8
...
...
@@ -498,9 +498,9 @@ protected:
bool
success
=
false
;
};
template
<
typename
type
>
class
type_caster
<
std
::
unique_ptr
<
type
>>
{
template
<
typename
type
,
typename
deleter
>
class
type_caster
<
std
::
unique_ptr
<
type
,
deleter
>>
{
public:
static
handle
cast
(
std
::
unique_ptr
<
type
>
&&
src
,
return_value_policy
policy
,
handle
parent
)
{
static
handle
cast
(
std
::
unique_ptr
<
type
,
deleter
>
&&
src
,
return_value_policy
policy
,
handle
parent
)
{
handle
result
=
type_caster_base
<
type
>::
cast
(
src
.
get
(),
policy
,
parent
);
if
(
result
)
src
.
release
();
...
...
tests/test_smart_ptr.cpp
View file @
614988c8
...
...
@@ -69,6 +69,18 @@ private:
int
value
;
};
class
MyObject4
{
public:
MyObject4
(
int
value
)
:
value
{
value
}
{
print_created
(
this
);
}
int
value
;
private:
~
MyObject4
()
{
print_destroyed
(
this
);
}
};
/// Make pybind aware of the ref-counted wrapper type (s)
PYBIND11_DECLARE_HOLDER_TYPE
(
T
,
ref
<
T
>
);
PYBIND11_DECLARE_HOLDER_TYPE
(
T
,
std
::
shared_ptr
<
T
>
);
...
...
@@ -143,6 +155,10 @@ test_initializer smart_ptr([](py::module &m) {
m
.
def
(
"print_myobject3_3"
,
&
print_myobject3_3
);
m
.
def
(
"print_myobject3_4"
,
&
print_myobject3_4
);
py
::
class_
<
MyObject4
,
std
::
unique_ptr
<
MyObject4
,
py
::
nodelete
>>
(
m
,
"MyObject4"
)
.
def
(
py
::
init
<
int
>
())
.
def_readwrite
(
"value"
,
&
MyObject4
::
value
);
py
::
implicitly_convertible
<
py
::
int_
,
MyObject1
>
();
// Expose constructor stats for the ref type
...
...
tests/test_smart_ptr.py
View file @
614988c8
...
...
@@ -113,3 +113,13 @@ def test_smart_ptr(capture):
# assert cstats.move_constructions >= 0 # Doesn't invoke any
assert
cstats
.
copy_assignments
==
30
assert
cstats
.
move_assignments
==
0
def
test_unique_nodelete
(
capture
):
from
pybind11_tests
import
MyObject4
o
=
MyObject4
(
23
)
assert
o
.
value
==
23
cstats
=
ConstructorStats
.
get
(
MyObject4
)
assert
cstats
.
alive
()
==
1
del
o
cstats
=
ConstructorStats
.
get
(
MyObject4
)
assert
cstats
.
alive
()
==
1
# Leak, but that's intentional
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment