mirror of
https://github.com/pybind/pybind11.git
synced 2024-11-11 08:03:55 +00:00
Merge 58d4aaeedc
into 7c4ac91d75
This commit is contained in:
commit
e8a0853968
@ -39,6 +39,15 @@ class type_caster : public type_caster_base<type> {};
|
||||
template <typename type>
|
||||
using make_caster = type_caster<intrinsic_t<type>>;
|
||||
|
||||
template <typename T>
|
||||
struct is_generic_type<T, enable_if_t<std::is_base_of<type_caster_generic, make_caster<T>>::value>>
|
||||
: public std::true_type {};
|
||||
|
||||
template <typename T>
|
||||
struct is_generic_type<T,
|
||||
enable_if_t<!std::is_base_of<type_caster_generic, make_caster<T>>::value>>
|
||||
: public std::false_type {};
|
||||
|
||||
// Shortcut for calling a caster's `cast_op_type` cast operator for casting a type_caster to a T
|
||||
template <typename T>
|
||||
typename make_caster<T>::template cast_op_type<T> cast_op(make_caster<T> &caster) {
|
||||
|
@ -48,6 +48,11 @@ PYBIND11_NAMESPACE_BEGIN(detail)
|
||||
class args_proxy;
|
||||
bool isinstance_generic(handle obj, const std::type_info &tp);
|
||||
|
||||
// Indicates that type is generic and and does not have a specialized
|
||||
// `type_caster<>` specialization. Defined in `cast.h`.
|
||||
template <typename T, typename SFINAE = void>
|
||||
struct is_generic_type;
|
||||
|
||||
// Accessor forward declarations
|
||||
template <typename Policy>
|
||||
class accessor;
|
||||
@ -836,7 +841,7 @@ inline void raise_from(error_already_set &err, PyObject *type, const char *messa
|
||||
/** \ingroup python_builtins
|
||||
\rst
|
||||
Return true if ``obj`` is an instance of ``T``. Type ``T`` must be a subclass of
|
||||
`object` or a class which was exposed to Python as ``py::class_<T>``.
|
||||
`object` or a class which was exposed to Python as ``py::class_<T>`` (generic).
|
||||
\endrst */
|
||||
template <typename T, detail::enable_if_t<std::is_base_of<object, T>::value, int> = 0>
|
||||
bool isinstance(handle obj) {
|
||||
@ -845,6 +850,8 @@ bool isinstance(handle obj) {
|
||||
|
||||
template <typename T, detail::enable_if_t<!std::is_base_of<object, T>::value, int> = 0>
|
||||
bool isinstance(handle obj) {
|
||||
static_assert(detail::is_generic_type<T>::value,
|
||||
"isinstance<T>() requires specialization for this type");
|
||||
return detail::isinstance_generic(obj, typeid(T));
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user