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
d54d6d8c
Unverified
Commit
d54d6d8c
authored
Jul 01, 2020
by
Yannick Jadoul
Committed by
GitHub
Jul 01, 2020
Browse files
Adding pybind11::cast overload for rvalue references (#1260)
* Adding pybind11::cast overload for rvalue references
parent
fc3a4490
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
12 additions
and
5 deletions
+12
-5
include/pybind11/cast.h
include/pybind11/cast.h
+7
-4
tests/test_smart_ptr.cpp
tests/test_smart_ptr.cpp
+2
-1
tests/test_smart_ptr.py
tests/test_smart_ptr.py
+3
-0
No files found.
include/pybind11/cast.h
View file @
d54d6d8c
...
@@ -1725,13 +1725,16 @@ T cast(const handle &handle) { return T(reinterpret_borrow<object>(handle)); }
...
@@ -1725,13 +1725,16 @@ T cast(const handle &handle) { return T(reinterpret_borrow<object>(handle)); }
// C++ type -> py::object
// C++ type -> py::object
template
<
typename
T
,
detail
::
enable_if_t
<!
detail
::
is_pyobject
<
T
>
::
value
,
int
>
=
0
>
template
<
typename
T
,
detail
::
enable_if_t
<!
detail
::
is_pyobject
<
T
>
::
value
,
int
>
=
0
>
object
cast
(
const
T
&
value
,
return_value_policy
policy
=
return_value_policy
::
automatic_reference
,
object
cast
(
T
&
&
value
,
return_value_policy
policy
=
return_value_policy
::
automatic_reference
,
handle
parent
=
handle
())
{
handle
parent
=
handle
())
{
using
no_ref_T
=
typename
std
::
remove_reference
<
T
>::
type
;
if
(
policy
==
return_value_policy
::
automatic
)
if
(
policy
==
return_value_policy
::
automatic
)
policy
=
std
::
is_pointer
<
T
>::
value
?
return_value_policy
::
take_ownership
:
return_value_policy
::
copy
;
policy
=
std
::
is_pointer
<
no_ref_T
>::
value
?
return_value_policy
::
take_ownership
:
std
::
is_lvalue_reference
<
T
>::
value
?
return_value_policy
::
copy
:
return_value_policy
::
move
;
else
if
(
policy
==
return_value_policy
::
automatic_reference
)
else
if
(
policy
==
return_value_policy
::
automatic_reference
)
policy
=
std
::
is_pointer
<
T
>::
value
?
return_value_policy
::
reference
:
return_value_policy
::
copy
;
policy
=
std
::
is_pointer
<
no_ref_T
>::
value
?
return_value_policy
::
reference
:
return
reinterpret_steal
<
object
>
(
detail
::
make_caster
<
T
>::
cast
(
value
,
policy
,
parent
));
std
::
is_lvalue_reference
<
T
>::
value
?
return_value_policy
::
copy
:
return_value_policy
::
move
;
return
reinterpret_steal
<
object
>
(
detail
::
make_caster
<
T
>::
cast
(
std
::
forward
<
T
>
(
value
),
policy
,
parent
));
}
}
template
<
typename
T
>
T
handle
::
cast
()
const
{
return
pybind11
::
cast
<
T
>
(
*
this
);
}
template
<
typename
T
>
T
handle
::
cast
()
const
{
return
pybind11
::
cast
<
T
>
(
*
this
);
}
...
...
tests/test_smart_ptr.cpp
View file @
d54d6d8c
...
@@ -291,7 +291,8 @@ TEST_SUBMODULE(smart_ptr, m) {
...
@@ -291,7 +291,8 @@ TEST_SUBMODULE(smart_ptr, m) {
~
C
()
{
print_destroyed
(
this
);
}
~
C
()
{
print_destroyed
(
this
);
}
};
};
py
::
class_
<
C
,
custom_unique_ptr
<
C
>>
(
m
,
"TypeWithMoveOnlyHolder"
)
py
::
class_
<
C
,
custom_unique_ptr
<
C
>>
(
m
,
"TypeWithMoveOnlyHolder"
)
.
def_static
(
"make"
,
[]()
{
return
custom_unique_ptr
<
C
>
(
new
C
);
});
.
def_static
(
"make"
,
[]()
{
return
custom_unique_ptr
<
C
>
(
new
C
);
})
.
def_static
(
"make_as_object"
,
[]()
{
return
py
::
cast
(
custom_unique_ptr
<
C
>
(
new
C
));
});
// test_holder_with_addressof_operator
// test_holder_with_addressof_operator
struct
TypeForHolderWithAddressOf
{
struct
TypeForHolderWithAddressOf
{
...
...
tests/test_smart_ptr.py
View file @
d54d6d8c
...
@@ -218,7 +218,10 @@ def test_shared_ptr_from_this_and_references():
...
@@ -218,7 +218,10 @@ def test_shared_ptr_from_this_and_references():
def
test_move_only_holder
():
def
test_move_only_holder
():
a
=
m
.
TypeWithMoveOnlyHolder
.
make
()
a
=
m
.
TypeWithMoveOnlyHolder
.
make
()
b
=
m
.
TypeWithMoveOnlyHolder
.
make_as_object
()
stats
=
ConstructorStats
.
get
(
m
.
TypeWithMoveOnlyHolder
)
stats
=
ConstructorStats
.
get
(
m
.
TypeWithMoveOnlyHolder
)
assert
stats
.
alive
()
==
2
del
b
assert
stats
.
alive
()
==
1
assert
stats
.
alive
()
==
1
del
a
del
a
assert
stats
.
alive
()
==
0
assert
stats
.
alive
()
==
0
...
...
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