1010#include < boost/python.hpp>
1111#include < boost/variant.hpp>
1212#include < boost/mpl/for_each.hpp>
13+ #include < boost/mpl/vector.hpp>
1314
1415#ifdef EIGENPY_WITH_CXX17_SUPPORT
1516#include < variant>
@@ -23,6 +24,10 @@ namespace details {
2324template <typename ResultType, typename Variant>
2425struct VariantVisitorType {};
2526
27+ // / Allow to get all alternatives in a boost::mpl vector
28+ template <typename Variant>
29+ struct VariantAlternatives {};
30+
2631#ifdef EIGENPY_WITH_CXX17_SUPPORT
2732
2833// / std::variant implementation
@@ -31,13 +36,18 @@ struct VariantVisitorType<ResultType, std::variant<Alternatives...> > {
3136 typedef std::variant<Alternatives...> variant_type;
3237 typedef ResultType result_type;
3338
34- template <typename Visitor>
35- static result_type visit (Visitor&& visitor, Alternatives&&... alternatives ) {
39+ template <typename Visitor, typename Visitable >
40+ static result_type visit (Visitor&& visitor, Visitable&& v ) {
3641 return std::visit (std::forward<Visitor>(visitor),
37- std::forward<Alternatives>(alternatives)... );
42+ std::forward<Visitable>(v) );
3843 }
3944};
4045
46+ template <typename ... Alternatives>
47+ struct VariantAlternatives <std::variant<Alternatives...>>{
48+ typedef boost::mpl::vector<Alternatives...> types;
49+ };
50+
4151#endif
4252
4353// / boost::variant implementation
@@ -53,6 +63,11 @@ struct VariantVisitorType<ResultType, boost::variant<Alternatives...> >
5363 }
5464};
5565
66+ template <typename ... Alternatives>
67+ struct VariantAlternatives <boost::variant<Alternatives...>>{
68+ typedef typename boost::variant<Alternatives...>::types types;
69+ };
70+
5671// / Convert {boost,std}::variant<class...> alternative to a Python object.
5772// / This converter copy the alternative.
5873template <typename Variant>
@@ -61,8 +76,8 @@ struct VariantValueToObject : VariantVisitorType<PyObject*, Variant> {
6176 typedef typename Base::result_type result_type;
6277 typedef typename Base::variant_type variant_type;
6378
64- static result_type convert (const variant_type& gm ) {
65- return Base::visit (VariantValueToObject (), gm );
79+ static result_type convert (const variant_type& v ) {
80+ return Base::visit (VariantValueToObject (), v );
6681 }
6782
6883 template <typename T>
@@ -81,8 +96,8 @@ struct VariantRefToObject : VariantVisitorType<PyObject*, Variant> {
8196 typedef typename Base::result_type result_type;
8297 typedef typename Base::variant_type variant_type;
8398
84- static result_type convert (const variant_type& gm ) {
85- return Base::visit (VariantRefToObject (), gm );
99+ static result_type convert (const variant_type& v ) {
100+ return Base::visit (VariantRefToObject (), v );
86101 }
87102
88103 template <typename T>
@@ -102,8 +117,8 @@ struct VariantConverter {
102117 template <class T >
103118 struct apply {
104119 struct type {
105- PyObject* operator ()(const variant_type& gm ) const {
106- return VariantRefToObject<variant_type>::convert (gm );
120+ PyObject* operator ()(const variant_type& v ) const {
121+ return VariantRefToObject<variant_type>::convert (v );
107122 }
108123
109124#ifndef BOOST_PYTHON_NO_PY_SIGNATURES
@@ -169,8 +184,9 @@ struct VariantConverter {
169184
170185 static void registration () {
171186 typedef details::VariantValueToObject<variant_type> variant_to_value;
187+ typedef typename details::VariantAlternatives<variant_type>::types types;
172188 boost::python::to_python_converter<variant_type, variant_to_value>();
173- boost::mpl::for_each<typename variant_type:: types>(
189+ boost::mpl::for_each<types>(
174190 details::VariantImplicitlyConvertible<variant_type>());
175191 }
176192};
0 commit comments