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
3e4fe6c0
Commit
3e4fe6c0
authored
Sep 11, 2016
by
Jason Rhinelander
Browse files
Store a static type_caster rather than the basic type
parent
f3f53e2b
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
17 additions
and
17 deletions
+17
-17
include/pybind11/cast.h
include/pybind11/cast.h
+14
-14
include/pybind11/pybind11.h
include/pybind11/pybind11.h
+3
-3
No files found.
include/pybind11/cast.h
View file @
3e4fe6c0
...
...
@@ -867,8 +867,8 @@ template <typename type> using cast_is_temporary_value_reference = bool_constant
!
std
::
is_base_of
<
type_caster_generic
,
make_caster
<
type
>>::
value
>
;
template
<
typename
T
>
make_caster
<
T
>
load_type
(
const
handle
&
handle
)
{
make_caster
<
T
>
conv
;
// Basic python -> C++ casting; throws if casting fails
template
<
typename
TypeCaster
>
TypeCaster
&
load_type
(
TypeCaster
&
conv
,
const
handle
&
handle
)
{
if
(
!
conv
.
load
(
handle
,
true
))
{
#if defined(NDEBUG)
throw
cast_error
(
"Unable to cast Python instance to C++ type (compile in debug mode for details)"
);
...
...
@@ -879,6 +879,12 @@ template <typename T> make_caster<T> load_type(const handle &handle) {
}
return
conv
;
}
// Wrapper around the above that also constructs and returns a type_caster
template
<
typename
T
>
make_caster
<
T
>
load_type
(
const
handle
&
handle
)
{
make_caster
<
T
>
conv
;
load_type
(
conv
,
handle
);
return
conv
;
}
NAMESPACE_END
(
detail
)
...
...
@@ -943,22 +949,16 @@ template <> inline void object::cast() && { return; }
NAMESPACE_BEGIN
(
detail
)
struct
overload_nothing
{};
// Placeholder type for the unneeded (and dead code) static variable in the OVERLOAD_INT macro
template
<
typename
ret_type
>
using
overload_local_t
=
conditional_t
<
cast_is_temporary_value_reference
<
ret_type
>::
value
,
intrinsic_t
<
ret_type
>
,
overload_nothing
>
;
template
<
typename
T
>
enable_if_t
<
std
::
is_lvalue_reference
<
T
>::
value
,
T
>
storage_cast
(
intrinsic_t
<
T
>
&
v
)
{
return
v
;
}
template
<
typename
T
>
enable_if_t
<
std
::
is_pointer
<
T
>::
value
,
T
>
storage_cast
(
intrinsic_t
<
T
>
&
v
)
{
return
&
v
;
}
struct
overload_unused
{};
// Placeholder type for the unneeded (and dead code) static variable in the OVERLOAD_INT macro
template
<
typename
ret_type
>
using
overload_caster_t
=
conditional_t
<
cast_is_temporary_value_reference
<
ret_type
>::
value
,
make_caster
<
ret_type
>
,
overload_unused
>
;
// Trampoline use: for reference/pointer types to value-converted values, we do a value cast, then
// store the result in the given variable. For other types, this is a no-op.
template
<
typename
T
>
enable_if_t
<
cast_is_temporary_value_reference
<
T
>::
value
,
T
>
cast_ref
(
object
&&
o
,
intrinsic_t
<
T
>
&
storage
)
{
using
type_caster
=
make_caster
<
T
>
;
using
itype
=
intrinsic_t
<
T
>
;
storage
=
std
::
move
(
load_type
<
T
>
(
o
).
operator
typename
type_caster
::
template
cast_op_type
<
itype
>());
return
storage_cast
<
T
>
(
storage
);
template
<
typename
T
>
enable_if_t
<
cast_is_temporary_value_reference
<
T
>::
value
,
T
>
cast_ref
(
object
&&
o
,
make_caster
<
T
>
&
caster
)
{
return
load_type
(
caster
,
o
).
operator
typename
make_caster
<
T
>::
template
cast_op_type
<
T
>();
}
template
<
typename
T
>
enable_if_t
<!
cast_is_temporary_value_reference
<
T
>::
value
,
T
>
cast_ref
(
object
&&
,
overload_
nothing
&
)
{
template
<
typename
T
>
enable_if_t
<!
cast_is_temporary_value_reference
<
T
>::
value
,
T
>
cast_ref
(
object
&&
,
overload_
unused
&
)
{
pybind11_fail
(
"Internal error: cast_ref fallback invoked"
);
}
// Trampoline use: Having a pybind11::cast with an invalid reference type is going to static_assert, even
...
...
include/pybind11/pybind11.h
View file @
3e4fe6c0
...
...
@@ -1486,10 +1486,10 @@ template <class T> function get_overload(const T *this_ptr, const char *name) {
pybind11::gil_scoped_acquire gil; \
pybind11::function overload = pybind11::get_overload(static_cast<const cname *>(this), name); \
if (overload) { \
pybind11::object
o = overload(__VA_ARGS__); \
auto
o = overload(__VA_ARGS__); \
if (pybind11::detail::cast_is_temporary_value_reference<ret_type>::value) { \
static pybind11::detail::overload_
local
_t<ret_type>
local_value
; \
return pybind11::detail::cast_ref<ret_type>(std::move(o),
local_value
); \
static pybind11::detail::overload_
caster
_t<ret_type>
caster
; \
return pybind11::detail::cast_ref<ret_type>(std::move(o),
caster
); \
} \
else return pybind11::detail::cast_safe<ret_type>(std::move(o)); \
} \
...
...
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