Skip to content

Commit

Permalink
python: Remove remaining uses of boost::type_traits and indirect_traits
Browse files Browse the repository at this point in the history
The traits in boost/detail/indirect_traits.hpp have been copied into
detail/indirect_traits.hpp. Those and other type traits from boost
have been reimplemented using C++17 standard library facilities.

(Internal change: 2341416)
  • Loading branch information
sunyab authored and pixar-oss committed Sep 19, 2024
1 parent 6a4d45c commit 09f232d
Show file tree
Hide file tree
Showing 15 changed files with 192 additions and 36 deletions.
2 changes: 1 addition & 1 deletion pxr/external/boost/python/converter/arg_to_python.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ namespace detail
reject_raw_object_helper<T,yes_convertible>::error(
python::detail::convertible<PyObject const volatile*>::check((T*)0));

typedef typename remove_cv<T>::type value_type;
typedef typename std::remove_cv<T>::type value_type;

reject_raw_object_helper<T,no_convertible>::error(
python::detail::convertible<unspecialized*>::check(
Expand Down
5 changes: 2 additions & 3 deletions pxr/external/boost/python/detail/caller.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@
# include "pxr/external/boost/python/type_id.hpp"
# include "pxr/external/boost/python/handle.hpp"

# include <boost/detail/indirect_traits.hpp>

# include "pxr/external/boost/python/detail/indirect_traits.hpp"
# include "pxr/external/boost/python/detail/invoke.hpp"
# include "pxr/external/boost/python/detail/signature.hpp"
# include "pxr/external/boost/python/detail/preprocessor.hpp"
Expand Down Expand Up @@ -120,7 +119,7 @@ template<class Policies, class Sig> const signature_element* get_ret()
static const signature_element ret = {
(detail::is_void<rtype>::value ? "void" : type_id<rtype>().name())
, &detail::converter_target_type<result_converter>::get_pytype
, boost::detail::indirect_traits::is_reference_to_non_const<rtype>::value
, indirect_traits::is_reference_to_non_const<rtype>::value
};

return &ret;
Expand Down
155 changes: 152 additions & 3 deletions pxr/external/boost/python/detail/indirect_traits.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,160 @@
#include <boost/python/detail/indirect_traits.hpp>
#else

# include <boost/detail/indirect_traits.hpp>
#include <type_traits>

namespace PXR_BOOST_NAMESPACE { namespace python {
namespace indirect_traits = boost::detail::indirect_traits;
}} // namespace PXR_BOOST_NAMESPACE::python::detail

namespace indirect_traits {

template <class T>
struct is_reference_to_const : std::false_type
{
};

template <class T>
struct is_reference_to_const<T const&> : std::true_type
{
};

template <class T>
struct is_reference_to_function : std::false_type
{
};

template <class T>
struct is_reference_to_function<T&> : std::is_function<T>
{
};

template <class T>
struct is_pointer_to_function : std::false_type
{
};

// There's no such thing as a pointer-to-cv-function, so we don't need
// specializations for those
template <class T>
struct is_pointer_to_function<T*> : std::is_function<T>
{
};

template <class T>
struct is_reference_to_member_function_pointer_impl : std::false_type
{
};

template <class T>
struct is_reference_to_member_function_pointer_impl<T&>
: std::is_member_function_pointer<typename std::remove_cv<T>::type>
{
};


template <class T>
struct is_reference_to_member_function_pointer
: is_reference_to_member_function_pointer_impl<T>
{
};

template <class T>
struct is_reference_to_function_pointer_aux
: std::bool_constant<
std::is_reference<T>::value &&
is_pointer_to_function<
typename std::remove_cv<
typename std::remove_reference<T>::type
>::type
>::value
>
{
// There's no such thing as a pointer-to-cv-function, so we don't need specializations for those
};

template <class T>
struct is_reference_to_function_pointer
: std::conditional<
is_reference_to_function<T>::value
, std::false_type
, is_reference_to_function_pointer_aux<T>
>::type
{
};

template <class T>
struct is_reference_to_non_const
: std::bool_constant<
std::is_lvalue_reference<T>::value &&
!is_reference_to_const<T>::value
>
{
};

template <class T>
struct is_reference_to_volatile : std::false_type
{
};

template <class T>
struct is_reference_to_volatile<T volatile&> : std::true_type
{
};

template <class T>
struct is_reference_to_pointer : std::false_type
{
};

template <class T>
struct is_reference_to_pointer<T*&> : std::true_type
{
};

template <class T>
struct is_reference_to_pointer<T* const&> : std::true_type
{
};

template <class T>
struct is_reference_to_pointer<T* volatile&> : std::true_type
{
};

template <class T>
struct is_reference_to_pointer<T* const volatile&> : std::true_type
{
};

template <class T>
struct is_reference_to_class
: std::bool_constant<
std::is_lvalue_reference<T>::value &&
std::is_class<
typename std::remove_cv<
typename std::remove_reference<T>::type
>::type
>::value
>
{
};

template <class T>
struct is_pointer_to_class
: std::bool_constant<
std::is_pointer<T>::value &&
std::is_class<
typename std::remove_cv<
typename std::remove_pointer<T>::type
>::type
>::value
>
{
};


}

}} // namespace PXR_BOOST_NAMESPACE::python

#endif // PXR_USE_INTERNAL_BOOST_PYTHON
#endif // PXR_EXTERNAL_BOOST_PYTHON_DETAIL_INDIRECT_TRAITS_HPP
24 changes: 17 additions & 7 deletions pxr/external/boost/python/detail/type_traits.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@
#else

# include <type_traits>
# include <boost/type_traits/is_base_and_derived.hpp>
# include <boost/type_traits/alignment_traits.hpp>
# include <boost/type_traits/has_trivial_copy.hpp>


namespace PXR_BOOST_NAMESPACE { namespace python { namespace detail {

Expand Down Expand Up @@ -59,9 +55,23 @@ namespace PXR_BOOST_NAMESPACE { namespace python { namespace detail {
typedef std::integral_constant<bool, true> true_;
typedef std::integral_constant<bool, false> false_;

using boost::is_base_and_derived;
using boost::type_with_alignment;
using boost::has_trivial_copy;
// This was previously boost::is_base_and_derived, which was once
// user-facing but now appears to be an undocumented implementation
// detail in boost/type_traits.
//
// The boost trait is *not* equivalent to std::is_base_of. Most
// critically, boost::is_base_and_derived<T, T>::value is false,
// while std::is_base_of<T, T>::value is true. We accommodate that
// difference below.
//
// boost::is_base_and_derived also handles inaccessible
// or ambiguous base classes, whereas std::is_base_of does not.
// This does not appear to be relevant for this library.
template <class Base, class Derived>
using is_base_and_derived = std::bool_constant<
std::is_base_of_v<Base, Derived> && !std::is_same_v<Base, Derived>
>;

}}} // namespace PXR_BOOST_NAMESPACE::python::detail


Expand Down
4 changes: 2 additions & 2 deletions pxr/external/boost/python/numpy/dtype.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,12 @@ template <int bits> dtype get_float_dtype();

template <int bits> dtype get_complex_dtype();

template <typename T, bool isInt=boost::is_integral<T>::value>
template <typename T, bool isInt=std::is_integral<T>::value>
struct builtin_dtype;

template <typename T>
struct builtin_dtype<T,true> {
static dtype get() { return get_int_dtype< 8*sizeof(T), boost::is_unsigned<T>::value >(); }
static dtype get() { return get_int_dtype< 8*sizeof(T), std::is_unsigned<T>::value >(); }
};

template <>
Expand Down
6 changes: 3 additions & 3 deletions pxr/external/boost/python/object/class_metadata.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ struct class_metadata
python::detail::mpl2::or_<
has_back_reference<T>
, PXR_BOOST_NAMESPACE::python::detail::is_same<held_type_arg,T>
, is_base_and_derived<T,wrapped>
, python::detail::is_base_and_derived<T,wrapped>
>::value
> use_back_reference;

Expand Down Expand Up @@ -221,8 +221,8 @@ struct class_metadata

inline static void register_aux(void*)
{
typedef typename is_base_and_derived<T,wrapped>::type use_callback;
class_metadata::register_aux2((T*)0, python::detail::mpl2::bool_<use_callback::value>());
typedef typename python::detail::is_base_and_derived<T,wrapped>::type use_callback;
class_metadata::register_aux2((T*)0, use_callback());
}

template <class T2, class Callback>
Expand Down
4 changes: 2 additions & 2 deletions pxr/external/boost/python/object/inheritance.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ struct implicit_cast_generator
template <class Source, class Target>
struct cast_generator
: python::detail::mpl2::if_<
PXR_BOOST_NAMESPACE::python::detail::is_base_and_derived<Target,Source>
python::detail::is_base_and_derived<Target,Source>
, implicit_cast_generator<Source,Target>
, dynamic_cast_generator<Source,Target>
>
Expand All @@ -123,7 +123,7 @@ struct cast_generator

template <class Source, class Target>
inline void register_conversion(
bool is_downcast = ::boost::is_base_and_derived<Source,Target>::value
bool is_downcast = python::detail::is_base_and_derived<Source,Target>::value
// These parameters shouldn't be used; they're an MSVC bug workaround
, Source* = 0, Target* = 0)
{
Expand Down
9 changes: 2 additions & 7 deletions pxr/external/boost/python/object/instance.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,9 @@ struct instance
PyObject* weakrefs;
instance_holder* objects;

typedef typename PXR_BOOST_NAMESPACE::python::detail::type_with_alignment<
PXR_BOOST_NAMESPACE::python::detail::alignment_of<Data>::value
>::type align_t;

union
struct
{
align_t align;
char bytes[sizeof(Data)];
alignas(Data) char bytes[sizeof(Data)];
} storage;
};

Expand Down
2 changes: 1 addition & 1 deletion pxr/external/boost/python/object/iterator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ struct iterator_range
struct next
{
typedef typename python::detail::mpl2::if_<
is_reference<
std::is_reference<
typename traits_t::reference
>
, typename traits_t::reference
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ namespace PXR_BOOST_NAMESPACE { namespace python {

typedef detail::mpl2::or_<
detail::mpl2::bool_<NoProxy>
, detail::mpl2::not_<is_class<Data> >
, detail::mpl2::not_<std::is_class<Data> >
, typename detail::mpl2::or_<
detail::is_same<Data, std::string>
, detail::is_same<Data, std::complex<float> >
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,9 @@ namespace PXR_BOOST_NAMESPACE { namespace python {
elem_name += class_name_extractor();
elem_name += "_entry";


typedef typename detail::mpl2::if_<
detail::mpl2::and_<is_class<data_type>, detail::mpl2::bool_<!NoProxy> >
detail::mpl2::and_<std::is_class<data_type>, detail::mpl2::bool_<!NoProxy> >
, return_internal_reference<>
, default_call_policies
>::type get_data_return_policy;
Expand All @@ -110,7 +111,7 @@ namespace PXR_BOOST_NAMESPACE { namespace python {

static
typename detail::mpl2::if_<
detail::mpl2::and_<is_class<data_type>, detail::mpl2::bool_<!NoProxy> >
detail::mpl2::and_<std::is_class<data_type>, detail::mpl2::bool_<!NoProxy> >
, data_type&
, data_type
>::type
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ namespace PXR_BOOST_NAMESPACE { namespace python {

static
typename detail::mpl2::if_<
is_class<data_type>
std::is_class<data_type>
, data_type&
, data_type
>::type
Expand Down
3 changes: 2 additions & 1 deletion pxr/external/boost/python/test/select_arg_to_python_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "pxr/external/boost/python/handle.hpp"
#include "pxr/external/boost/python/object.hpp"
#include <iostream>
#include <type_traits>

// gcc 2.95.x and MIPSpro 7.3.1.3 linker seem to demand this definition
#if ((defined(__GNUC__) && __GNUC__ < 3)) \
Expand All @@ -30,7 +31,7 @@ int result;
template <class T, class U>
void assert_same(U* = 0, T* = 0)
{
static_assert((boost::is_same<T,U>::value));
static_assert((std::is_same<T,U>::value));

}

Expand Down
3 changes: 2 additions & 1 deletion pxr/external/boost/python/test/select_from_python_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "pxr/external/boost/python/converter/arg_from_python.hpp"
#include "pxr/external/boost/python/type_id.hpp"
#include <iostream>
#include <type_traits>

// gcc 2.95.x, MIPSpro 7.3.1.3 and IBM XL for Linux linker seem to demand this definition
#if (defined(__GNUC__) && (__GNUC__ < 3)) \
Expand All @@ -25,7 +26,7 @@ PXR_BOOST_PYTHON_DECL bool handle_exception_impl(function0<void>)
int result;

#define ASSERT_SAME(T1,T2) \
if (!is_same< T1, T2 >::value) { \
if (!std::is_same< T1, T2 >::value) { \
std::cout << "*********************\n"; \
std::cout << python::type_id< T1 >() << " != " << python::type_id< T2 >() << "\n"; \
std::cout << "*********************\n"; \
Expand Down
2 changes: 1 addition & 1 deletion pxr/external/boost/python/to_python_value.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ struct object_manager_get_pytype<true>
PyObject* operator()(argument_type) const;
#ifndef PXR_BOOST_PYTHON_NO_PY_SIGNATURES
typedef detail::mpl2::bool_<is_handle<T>::value> is_t_handle;
typedef boost::detail::indirect_traits::is_reference_to_const<T> is_t_const;
typedef indirect_traits::is_reference_to_const<T> is_t_const;
PyTypeObject const* get_pytype() const {
return get_pytype_aux((is_t_handle*)0);
}
Expand Down

0 comments on commit 09f232d

Please sign in to comment.