Skip to content
Snippets Groups Projects
Unverified Commit ab4677ad authored by Joris Vaillant's avatar Joris Vaillant
Browse files

core: Fix build issue with std::variant

parent bfb0c322
No related branches found
No related tags found
Loading
Pipeline #35868 passed with warnings
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
#include <boost/python.hpp> #include <boost/python.hpp>
#include <boost/variant.hpp> #include <boost/variant.hpp>
#include <boost/mpl/for_each.hpp> #include <boost/mpl/for_each.hpp>
#include <boost/mpl/vector.hpp>
#ifdef EIGENPY_WITH_CXX17_SUPPORT #ifdef EIGENPY_WITH_CXX17_SUPPORT
#include <variant> #include <variant>
...@@ -23,6 +24,10 @@ namespace details { ...@@ -23,6 +24,10 @@ namespace details {
template <typename ResultType, typename Variant> template <typename ResultType, typename Variant>
struct VariantVisitorType {}; struct VariantVisitorType {};
/// Allow to get all alternatives in a boost::mpl vector
template <typename Variant>
struct VariantAlternatives{};
#ifdef EIGENPY_WITH_CXX17_SUPPORT #ifdef EIGENPY_WITH_CXX17_SUPPORT
/// std::variant implementation /// std::variant implementation
...@@ -31,13 +36,18 @@ struct VariantVisitorType<ResultType, std::variant<Alternatives...> > { ...@@ -31,13 +36,18 @@ struct VariantVisitorType<ResultType, std::variant<Alternatives...> > {
typedef std::variant<Alternatives...> variant_type; typedef std::variant<Alternatives...> variant_type;
typedef ResultType result_type; typedef ResultType result_type;
template <typename Visitor> template <typename Visitor, typename Visitable>
static result_type visit(Visitor&& visitor, Alternatives&&... alternatives) { static result_type visit(Visitor&& visitor, Visitable&& v) {
return std::visit(std::forward<Visitor>(visitor), return std::visit(std::forward<Visitor>(visitor),
std::forward<Alternatives>(alternatives)...); std::forward<Visitable>(v));
} }
}; };
template<typename... Alternatives>
struct VariantAlternatives<std::variant<Alternatives...>>{
typedef boost::mpl::vector<Alternatives...> types;
};
#endif #endif
/// boost::variant implementation /// boost::variant implementation
...@@ -53,6 +63,11 @@ struct VariantVisitorType<ResultType, boost::variant<Alternatives...> > ...@@ -53,6 +63,11 @@ struct VariantVisitorType<ResultType, boost::variant<Alternatives...> >
} }
}; };
template<typename... Alternatives>
struct VariantAlternatives<boost::variant<Alternatives...>>{
typedef typename boost::variant<Alternatives...>::types types;
};
/// Convert {boost,std}::variant<class...> alternative to a Python object. /// Convert {boost,std}::variant<class...> alternative to a Python object.
/// This converter copy the alternative. /// This converter copy the alternative.
template <typename Variant> template <typename Variant>
...@@ -61,8 +76,8 @@ struct VariantValueToObject : VariantVisitorType<PyObject*, Variant> { ...@@ -61,8 +76,8 @@ struct VariantValueToObject : VariantVisitorType<PyObject*, Variant> {
typedef typename Base::result_type result_type; typedef typename Base::result_type result_type;
typedef typename Base::variant_type variant_type; typedef typename Base::variant_type variant_type;
static result_type convert(const variant_type& gm) { static result_type convert(const variant_type& v) {
return Base::visit(VariantValueToObject(), gm); return Base::visit(VariantValueToObject(), v);
} }
template <typename T> template <typename T>
...@@ -81,8 +96,8 @@ struct VariantRefToObject : VariantVisitorType<PyObject*, Variant> { ...@@ -81,8 +96,8 @@ struct VariantRefToObject : VariantVisitorType<PyObject*, Variant> {
typedef typename Base::result_type result_type; typedef typename Base::result_type result_type;
typedef typename Base::variant_type variant_type; typedef typename Base::variant_type variant_type;
static result_type convert(const variant_type& gm) { static result_type convert(const variant_type& v) {
return Base::visit(VariantRefToObject(), gm); return Base::visit(VariantRefToObject(), v);
} }
template <typename T> template <typename T>
...@@ -102,8 +117,8 @@ struct VariantConverter { ...@@ -102,8 +117,8 @@ struct VariantConverter {
template <class T> template <class T>
struct apply { struct apply {
struct type { struct type {
PyObject* operator()(const variant_type& gm) const { PyObject* operator()(const variant_type& v) const {
return VariantRefToObject<variant_type>::convert(gm); return VariantRefToObject<variant_type>::convert(v);
} }
#ifndef BOOST_PYTHON_NO_PY_SIGNATURES #ifndef BOOST_PYTHON_NO_PY_SIGNATURES
...@@ -169,8 +184,9 @@ struct VariantConverter { ...@@ -169,8 +184,9 @@ struct VariantConverter {
static void registration() { static void registration() {
typedef details::VariantValueToObject<variant_type> variant_to_value; typedef details::VariantValueToObject<variant_type> variant_to_value;
typedef typename details::VariantAlternatives<variant_type>::types types;
boost::python::to_python_converter<variant_type, variant_to_value>(); boost::python::to_python_converter<variant_type, variant_to_value>();
boost::mpl::for_each<typename variant_type::types>( boost::mpl::for_each<types>(
details::VariantImplicitlyConvertible<variant_type>()); details::VariantImplicitlyConvertible<variant_type>());
} }
}; };
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment