Skip to content

Commit

Permalink
Use enable_if_t for clif_type_caster.
Browse files Browse the repository at this point in the history
Tested with
`$ blaze test -k //third_party/clif/testing/...`
`$ blaze test //waymo/onboard/roadgraph/python:spiro_test` (for absl::optional)
`$ blaze test //monitoring/utilization/borg/prodiff/aggregate/python:capacitor_to_arrow_test` (for abstract types)
`$ blaze test //learning/deepmind/tensorflow/tensorfn/python:xla_function_test` (for abstract types)

PiperOrigin-RevId: 525883036
  • Loading branch information
wangxf authored and wangxf123456 committed May 18, 2023
1 parent 4a06af8 commit 9394cd6
Showing 1 changed file with 37 additions and 35 deletions.
72 changes: 37 additions & 35 deletions clif/pybind11/runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -179,31 +179,8 @@ namespace detail {
return Clif_PyObjFrom(src, {}); \
} \

template <typename Type,
bool has_customized_optional_conversion =
clif_pybind11::HasAbslOptionalPyObjAsFalseIfAbstract<Type>(),
bool has_customized_pointer_to_pointer_conversion =
!has_customized_optional_conversion &&
clif_pybind11::HasPyObjAs<Type*>(),
bool has_customized_unique_ptr_conversion =
!has_customized_optional_conversion &&
!has_customized_pointer_to_pointer_conversion &&
clif_pybind11::HasNonClifPyObjAs<std::unique_ptr<Type>>(0),
bool has_customized_shared_ptr_conversion =
!has_customized_optional_conversion &&
!has_customized_pointer_to_pointer_conversion &&
!has_customized_unique_ptr_conversion &&
clif_pybind11::HasPyObjAs<std::shared_ptr<Type>>(),
bool is_type_abstract =
!has_customized_optional_conversion &&
!has_customized_pointer_to_pointer_conversion &&
!has_customized_unique_ptr_conversion &&
!has_customized_shared_ptr_conversion &&
std::is_abstract_v<Type>>
struct clif_type_caster;

template <typename Type>
struct clif_type_caster<Type, false, false, false, false, false> {
template <class Type, typename SFINAE = void>
struct clif_type_caster {
public:
PYBIND11_TYPE_CASTER(Type, const_name<Type>());

Expand All @@ -217,8 +194,11 @@ struct clif_type_caster<Type, false, false, false, false, false> {
}
};

template <typename Type>
struct clif_type_caster<Type, true, false, false, false, false> {
template <class Type>
struct clif_type_caster<
Type,
typename std::enable_if_t<
clif_pybind11::HasAbslOptionalPyObjAsFalseIfAbstract<Type>()>> {
public:
static constexpr auto name = const_name<Type>();

Expand All @@ -240,8 +220,12 @@ struct clif_type_caster<Type, true, false, false, false, false> {
std::optional<Type> value;
};

template <typename Type>
struct clif_type_caster<Type, false, true, false, false, false> {
template <class Type>
struct clif_type_caster<
Type,
typename std::enable_if_t<
!clif_pybind11::HasAbslOptionalPyObjAsFalseIfAbstract<Type>() &&
clif_pybind11::HasPyObjAs<Type*>()>> {
public:
static constexpr auto name = const_name<Type>();

Expand All @@ -268,8 +252,13 @@ struct clif_type_caster<Type, false, true, false, false, false> {
Type* value;
};

template <typename Type>
struct clif_type_caster<Type, false, false, true, false, false> {
template <class Type>
struct clif_type_caster<
Type,
typename std::enable_if_t<
!clif_pybind11::HasAbslOptionalPyObjAsFalseIfAbstract<Type>() &&
!clif_pybind11::HasPyObjAs<Type*>() &&
clif_pybind11::HasNonClifPyObjAs<std::unique_ptr<Type>>(0)>> {
public:
static constexpr auto name = const_name<Type>();

Expand Down Expand Up @@ -300,8 +289,14 @@ struct clif_type_caster<Type, false, false, true, false, false> {
std::unique_ptr<Type> value;
};

template <typename Type>
struct clif_type_caster<Type, false, false, false, true, false> {
template <class Type>
struct clif_type_caster<
Type,
typename std::enable_if_t<
!clif_pybind11::HasAbslOptionalPyObjAsFalseIfAbstract<Type>() &&
!clif_pybind11::HasPyObjAs<Type*>() &&
!clif_pybind11::HasNonClifPyObjAs<std::unique_ptr<Type>>(0) &&
clif_pybind11::HasPyObjAs<std::shared_ptr<Type>>()>> {
public:
static constexpr auto name = const_name<Type>();

Expand Down Expand Up @@ -332,8 +327,15 @@ struct clif_type_caster<Type, false, false, false, true, false> {
};


template <typename Type>
struct clif_type_caster<Type, false, false, false, false, true> {
template <class Type>
struct clif_type_caster<
Type,
typename std::enable_if_t<
!clif_pybind11::HasAbslOptionalPyObjAsFalseIfAbstract<Type>() &&
!clif_pybind11::HasPyObjAs<Type*>() &&
!clif_pybind11::HasNonClifPyObjAs<std::unique_ptr<Type>>(0) &&
!clif_pybind11::HasPyObjAs<std::shared_ptr<Type>>() &&
std::is_abstract_v<Type>>> {
public:
static constexpr auto name = const_name<Type>();

Expand Down

0 comments on commit 9394cd6

Please sign in to comment.