This commit is contained in:
Eric Cousineau 2024-09-20 23:44:13 -07:00 committed by GitHub
commit a38949f545
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 17 additions and 1 deletions

View File

@ -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) {

View File

@ -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));
}