Use the improved Type_mapper to implement parts of Cartesian_converter

This commit is contained in:
Philipp Möller 2012-12-07 12:11:31 +01:00
parent 2c83dd01c8
commit 84d83bf996
1 changed files with 31 additions and 46 deletions

View File

@ -57,30 +57,28 @@ struct Default_converter {
typedef ::CGAL::NT_converter<FT1, FT2> Type; typedef ::CGAL::NT_converter<FT1, FT2> Type;
}; };
template<typename Seq, typename K1, typename K2>
struct Transform_type_mapper {
typedef typename boost::mpl::transform< Seq, typename boost::mpl::lambda< Type_mapper< boost::mpl::_1, K1, K2 > >::type >::type type;
};
// Out will be a variant, source kernel and target kernel // Out will be a variant, source kernel and target kernel
template<typename Conv, typename Out> template<typename Converter, typename Output>
struct Converting_visitor : boost::static_visitor<> { struct Converting_visitor : boost::static_visitor<> {
Converting_visitor(const Conv& conv, Out& out) : conv(&conv), out(&out) {} Converting_visitor(const Converter& conv, Output& out) : conv(&conv), out(&out) {}
const Conv* conv; const Converter* conv;
Out* out; Output* out;
template<typename T> template<typename T>
void operator()(const T& t) { void operator()(const T& t) { *out = conv->operator()(t); }
*out = conv->operator()(t);
}
template<typename T> template<typename T>
void operator()(const std::vector<T>& t) { void operator()(const std::vector<T>& t) {
typedef typename Type_mapper< T, typename Conv::Source_kernel, typename Conv::Target_kernel >::type value_type; typedef typename
std::vector< value_type > tmp(t.size()); Type_mapper< T, typename Converter::Source_kernel,
typename Converter::Target_kernel >::type
value_type;
for(std::size_t i = 0; i < t.size(); ++i) { std::vector< value_type > tmp;
tmp[i] = conv->operator()(t[i]); tmp.reserve(t.size());
for(typename std::vector< T >::iterator it = t.begin();
it != t.end(); ++it) {
tmp.push_back(conv->operator()(*it));
} }
*out = tmp; *out = tmp;
@ -138,49 +136,36 @@ public:
// new list into a variant // new list into a variant
// visit to get the type, and copy construct inside the return type // visit to get the type, and copy construct inside the return type
template<BOOST_VARIANT_ENUM_PARAMS(typename U)> template<BOOST_VARIANT_ENUM_PARAMS(typename U)>
boost::optional< typename
typename boost::make_variant_over< Type_mapper< boost::optional< boost::variant< BOOST_VARIANT_ENUM_PARAMS(U) > >,
typename internal::Transform_type_mapper< K1, K2 >::type
typename boost::mpl::remove< boost::mpl::vector< BOOST_VARIANT_ENUM_PARAMS(U) >,
boost::detail::variant::void_ >::type
, K1, K2 >::type
>::type >
operator()(const boost::optional< boost::variant< BOOST_VARIANT_ENUM_PARAMS(U) > >& o) const { operator()(const boost::optional< boost::variant< BOOST_VARIANT_ENUM_PARAMS(U) > >& o) const {
typedef boost::optional< typedef typename
typename boost::make_variant_over< Type_mapper< boost::optional< boost::variant< BOOST_VARIANT_ENUM_PARAMS(U) > >,
typename internal::Transform_type_mapper< K1, K2 >::type result_type;
typename boost::mpl::remove< boost::mpl::vector< BOOST_VARIANT_ENUM_PARAMS(U) >,
boost::detail::variant::void_ >::type
, K1, K2 >::type
>::type > result_type;
result_type res; result_type res;
if(!o) { if(!o) {
// empty converts to empty // empty converts to empty
return res; return res;
} }
internal::Converting_visitor<Self, result_type> conv_visitor = internal::Converting_visitor<Self, result_type>(*this, res); internal::Converting_visitor<Self, result_type>
conv_visitor = internal::Converting_visitor<Self, result_type>(*this, res);
boost::apply_visitor(conv_visitor, *o); boost::apply_visitor(conv_visitor, *o);
return res; return res;
} }
template<BOOST_VARIANT_ENUM_PARAMS(typename U)> template<BOOST_VARIANT_ENUM_PARAMS(typename U)>
typename boost::make_variant_over< typename
typename internal::Transform_type_mapper< Type_mapper< boost::variant< BOOST_VARIANT_ENUM_PARAMS(U) >,
typename boost::mpl::remove< boost::mpl::vector< BOOST_VARIANT_ENUM_PARAMS(U) >, K1, K2 >::type
boost::detail::variant::void_ >::type
, K1, K2 >::type
>::type
operator()(const boost::variant< BOOST_VARIANT_ENUM_PARAMS(U) > & o) const { operator()(const boost::variant< BOOST_VARIANT_ENUM_PARAMS(U) > & o) const {
typedef typedef typename
typename boost::make_variant_over< Type_mapper< boost::variant< BOOST_VARIANT_ENUM_PARAMS(U) >,
typename internal::Transform_type_mapper< K1, K2 >::type result_type;
typename boost::mpl::remove< boost::mpl::vector< BOOST_VARIANT_ENUM_PARAMS(U) >,
boost::detail::variant::void_ >::type
, K1, K2 >::type
>::type result_type;
result_type res; result_type res;
internal::Converting_visitor<Self, result_type> conv_visitor = internal::Converting_visitor<Self, result_type>(*this, res); internal::Converting_visitor<Self, result_type>
conv_visitor = internal::Converting_visitor<Self, result_type>(*this, res);
boost::apply_visitor(conv_visitor, o); boost::apply_visitor(conv_visitor, o);
return res; return res;
} }