// Boost.TypeErasure library // // Copyright 2011 Steven Watanabe // // Distributed under the Boost Software License Version 1.0. (See // accompanying file LICENSE_1_0.txt or copy at // http://www.boost.org/LICENSE_1_0.txt) // // $Id$ #ifndef BOOST_TYPE_ERASURE_DETAIL_NORMALIZE_HPP_INCLUDED #define BOOST_TYPE_ERASURE_DETAIL_NORMALIZE_HPP_INCLUDED #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include namespace boost { namespace type_erasure { template struct deduced; template struct same_type; namespace detail { struct substitution_map_tag {}; // a wrapper around an mpl::map that // defaults to the identity map. template struct substitution_map { typedef substitution_map_tag tag; typedef M map_type; }; } } namespace mpl { template<> struct at_impl< ::boost::type_erasure::detail::substitution_map_tag> { template struct apply { typedef typename ::boost::mpl::eval_if< ::boost::mpl::has_key, ::boost::mpl::at, ::boost::mpl::identity >::type type; }; }; template<> struct has_key_impl< ::boost::type_erasure::detail::substitution_map_tag> { template struct apply : boost::mpl::true_ {}; }; } namespace type_erasure { namespace detail { // Given the arguments to same_type, determines // which should be the key and which should be // the value in the substitution map. template struct select_pair { BOOST_MPL_ASSERT((::boost::is_same)); typedef void type; }; template struct select_pair > { typedef ::boost::mpl::pair< ::boost::type_erasure::deduced, T> type; }; template struct select_pair< ::boost::type_erasure::deduced, U> { typedef ::boost::mpl::pair< ::boost::type_erasure::deduced, U> type; }; template struct select_pair< ::boost::type_erasure::deduced, ::boost::type_erasure::deduced > { typedef ::boost::mpl::pair< ::boost::type_erasure::deduced, ::boost::type_erasure::deduced > type; }; #ifndef BOOST_TYPE_ERASURE_USE_MP11 // given a partial substitution map from same_type, // resolves a placeholder as far as possible. template struct resolve_same_type { typedef typename ::boost::mpl::eval_if< ::boost::mpl::has_key, ::boost::type_erasure::detail::resolve_same_type< M, typename ::boost::mpl::at::type >, ::boost::mpl::identity >::type type; }; // M is a map of placeholder substitutions template struct normalize_placeholder { typedef typename ::boost::mpl::eval_if< ::boost::mpl::has_key, ::boost::type_erasure::detail::normalize_placeholder< M, typename ::boost::mpl::at::type >, ::boost::mpl::identity >::type type; }; template struct normalize_placeholder > { typedef typename ::boost::mpl::eval_if< ::boost::mpl::has_key, ::boost::type_erasure::detail::normalize_placeholder< M, typename ::boost::mpl::at::type >, ::boost::type_erasure::detail::normalize_deduced< M, T > >::type type; }; // Takes a mpl::map of placeholder substitutions and // fully resolves it. i.e. a -> b, b -> c, becomes // a -> c, b -> c. Also resolves deduced placeholders // whose arguments are all resolved. template struct create_placeholder_map { typedef typename ::boost::mpl::fold< M, ::boost::mpl::map0<>, ::boost::mpl::insert< ::boost::mpl::_1, ::boost::mpl::pair< ::boost::mpl::first< ::boost::mpl::_2>, ::boost::type_erasure::detail::normalize_placeholder > > > >::type type; }; template struct convert_deduced { typedef typename ::boost::type_erasure::detail::rebind_placeholders_in_argument< typename P::first, Bindings >::type result1; typedef typename ::boost::mpl::at::type result; typedef typename ::boost::mpl::eval_if< ::boost::mpl::has_key, ::boost::mpl::identity, ::boost::mpl::insert > >::type type; BOOST_MPL_ASSERT((boost::is_same::type, result>)); }; template struct convert_deductions { typedef typename ::boost::mpl::fold< M, Bindings, ::boost::type_erasure::detail::convert_deduced< Bindings, ::boost::mpl::_2, ::boost::mpl::_1, Sub > >::type type; }; template struct add_deduced { typedef typename ::boost::type_erasure::detail::rebind_placeholders_in_argument< typename P::first, Bindings >::type result; typedef typename ::boost::mpl::eval_if< ::boost::mpl::has_key, ::boost::mpl::identity, ::boost::mpl::insert > >::type type; BOOST_MPL_ASSERT((boost::is_same::type, result>)); }; template struct add_deductions { typedef typename ::boost::mpl::fold< M, Bindings, ::boost::type_erasure::detail::add_deduced< Bindings, ::boost::mpl::_2, ::boost::mpl::_1 > >::type type; }; // Fold Op for normalize_concept_impl template struct insert_concept { typedef ::boost::mpl::pair< typename ::boost::mpl::insert::type, typename Out::second > type; }; template struct insert_concept > { typedef typename ::boost::type_erasure::detail::resolve_same_type< typename Out::second, T >::type t1; typedef typename ::boost::type_erasure::detail::resolve_same_type< typename Out::second, U >::type t2; typedef ::boost::mpl::pair< typename Out::first, typename ::boost::mpl::eval_if< ::boost::is_same, ::boost::mpl::identity, ::boost::mpl::insert< typename Out::second, typename ::boost::type_erasure::detail::select_pair< t1, t2 >::type > >::type > type; }; // flattens a concept returning an mpl::pair // - first is an MPL sequence containing the leaf concepts // - second is an MPL map of the placeholder substitutions // used to resolve same_type. template, ::boost::mpl::map0<> > > struct normalize_concept_impl { typedef typename ::boost::mpl::eval_if< ::boost::mpl::is_sequence, ::boost::mpl::fold >, ::boost::type_erasure::detail::insert_concept >::type type; }; struct append_typeinfo { template struct apply { typedef typename ::boost::mpl::insert< Set, ::boost::type_erasure::typeid_ >::type type; }; }; // Seq should be a flattened MPL sequence of leaf concepts. // adds typeid_

for every placeholder used. template struct add_typeinfo { typedef typename ::boost::mpl::fold< Seq, ::boost::mpl::set0<>, ::boost::type_erasure::detail::get_placeholders< ::boost::mpl::_2, ::boost::mpl::_1 > >::type placeholders; typedef typename ::boost::mpl::fold< placeholders, Seq, ::boost::type_erasure::detail::append_typeinfo >::type type; }; template struct get_placeholder_normalization_map { typedef typename ::boost::type_erasure::detail::create_placeholder_map< typename normalize_concept_impl::type::second >::type type; }; // Flattens a Concept to an mpl::vector of primitive // concepts. Resolves same_type and deduced placeholders. template struct normalize_concept { typedef typename normalize_concept_impl::type impl; typedef typename ::boost::type_erasure::detail::create_placeholder_map< typename impl::second >::type substitutions; typedef typename ::boost::mpl::fold< typename impl::first, ::boost::mpl::set0<>, ::boost::mpl::insert< ::boost::mpl::_1, ::boost::type_erasure::detail::rebind_placeholders< ::boost::mpl::_2, ::boost::type_erasure::detail::substitution_map > > >::type basic; typedef typename ::boost::mpl::eval_if< ::boost::type_erasure::is_relaxed, ::boost::type_erasure::detail::add_typeinfo, ::boost::mpl::identity >::type concept_set; typedef typename ::boost::mpl::copy< concept_set, ::boost::mpl::back_inserter< ::boost::mpl::vector0<> > >::type type; }; // Returns an MPL sequence containing all the concepts // in Concept. If Concept is considered as a DAG, // the result will be sorted topologically. template< class Concept, class Map = typename ::boost::type_erasure::detail::create_placeholder_map< typename ::boost::type_erasure::detail::normalize_concept_impl< Concept >::type::second >::type, class Out = ::boost::mpl::set0<> > struct collect_concepts { typedef typename ::boost::type_erasure::detail::rebind_placeholders< Concept, ::boost::type_erasure::detail::substitution_map >::type transformed; typedef typename ::boost::mpl::eval_if< ::boost::mpl::is_sequence, ::boost::mpl::fold >, ::boost::mpl::identity >::type type1; typedef typename ::boost::mpl::eval_if< ::boost::is_same, ::boost::mpl::identity, ::boost::mpl::insert< type1, transformed > >::type type; }; #else template struct resolve_same_type_impl; template using resolve_same_type_t = typename ::boost::type_erasure::detail::resolve_same_type_impl< (::boost::mp11::mp_map_contains::value) >::template apply; template<> struct resolve_same_type_impl { template using apply = ::boost::type_erasure::detail::resolve_same_type_t< M, ::boost::mp11::mp_second< ::boost::mp11::mp_map_find > >; }; template<> struct resolve_same_type_impl { template using apply = T; }; // given a partial substitution map from same_type, // resolves a placeholder as far as possible. template using resolve_same_type = ::boost::mpl::identity< ::boost::type_erasure::detail::resolve_same_type_t >; // M is a map of placeholder substitutions template struct normalize_placeholder_impl; template using normalize_placeholder_t = typename ::boost::type_erasure::detail::normalize_placeholder_impl< ::boost::mp11::mp_map_contains::value >::template apply; template<> struct normalize_placeholder_impl { template using apply = ::boost::type_erasure::detail::normalize_placeholder_t< M, typename ::boost::mp11::mp_second< ::boost::mp11::mp_map_find > >; }; template struct normalize_deduced_impl { template using apply = T; }; template class F, class... T> struct normalize_deduced_impl< ::boost::type_erasure::deduced > > { template using apply = typename ::boost::type_erasure::deduced...> >::type; }; template<> struct normalize_placeholder_impl { template using apply = typename ::boost::type_erasure::detail::normalize_deduced_impl::template apply; }; template using normalize_placeholder = ::boost::mpl::identity< ::boost::type_erasure::detail::normalize_placeholder_t >; // Takes a mpl::map of placeholder substitutions and // fully resolves it. i.e. a -> b, b -> c, becomes // a -> c, b -> c. Also resolves deduced placeholders // whose arguments are all resolved. template struct create_placeholder_map { template using transform_one = ::boost::mpl::pair< typename ::boost::mpl::first

::type, ::boost::type_erasure::detail::normalize_placeholder_t< M, typename ::boost::mpl::second

::type > >; typedef ::boost::mp11::mp_transform< transform_one, M > type; }; template using create_placeholder_map_t = typename ::boost::type_erasure::detail::create_placeholder_map::type; template struct convert_deduced { typedef ::boost::type_erasure::detail::rebind_placeholders_in_argument_t< typename P::first, Bindings > result1; typedef ::boost::mp11::mp_second< ::boost::mp11::mp_map_find > result; typedef ::boost::mp11::mp_map_insert > type; BOOST_MPL_ASSERT((boost::is_same< ::boost::mp11::mp_second< ::boost::mp11::mp_map_find >, result>)); }; template struct convert_deduced_f { template using apply = typename ::boost::type_erasure::detail::convert_deduced::type; }; template using convert_deductions_t = ::boost::mp11::mp_fold< M, ::boost::type_erasure::detail::make_mp_list, ::boost::type_erasure::detail::convert_deduced_f::template apply >; template using convert_deductions = ::boost::mpl::identity< ::boost::type_erasure::detail::convert_deductions_t >; template struct add_deduced { typedef typename ::boost::type_erasure::detail::rebind_placeholders_in_argument< typename P::first, Bindings >::type result; typedef ::boost::mp11::mp_map_insert > type; BOOST_MPL_ASSERT((boost::is_same< ::boost::mp11::mp_second< ::boost::mp11::mp_map_find >, result>)); }; template struct add_deduced_f { template using apply = typename ::boost::type_erasure::detail::add_deduced::type; }; template using add_deductions_t = ::boost::mp11::mp_fold< M, ::boost::type_erasure::detail::make_mp_list, ::boost::type_erasure::detail::add_deduced_f< ::boost::type_erasure::detail::make_mp_list >::template apply >; template using add_deductions = ::boost::mpl::identity< ::boost::type_erasure::detail::add_deductions_t >; // Fold Op for normalize_concept_impl template struct insert_concept_impl { template using apply = ::boost::mpl::pair< ::boost::mp11::mp_set_push_back, typename Out::second >; }; template using mpl_insert = typename ::boost::mpl::insert::type; template using insert_concept_same_type = ::boost::mpl::pair< typename Out::first, typename ::boost::type_erasure::detail::eval_if< ::boost::is_same::value, ::boost::type_erasure::detail::first, ::boost::mp11::mp_map_insert, // FIXME: is this supposed to be a replace? typename Out::second, typename ::boost::type_erasure::detail::select_pair< T1, T2 >::type > >; template struct insert_concept_impl< ::boost::type_erasure::same_type > { template using apply = ::boost::type_erasure::detail::insert_concept_same_type< ::boost::type_erasure::detail::resolve_same_type_t< typename Out::second, T >, ::boost::type_erasure::detail::resolve_same_type_t< typename Out::second, U >, Out >; }; template struct normalize_concept_impl_test; template using normalize_concept_impl_f = typename ::boost::type_erasure::detail::normalize_concept_impl_test< ::boost::mpl::is_sequence::value >::template apply; template<> struct normalize_concept_impl_test { template using apply = ::boost::mp11::mp_fold< ::boost::type_erasure::detail::make_mp_list, Out, ::boost::type_erasure::detail::normalize_concept_impl_f >; }; template<> struct normalize_concept_impl_test { template using apply = typename ::boost::type_erasure::detail::insert_concept_impl::template apply; }; template using normalize_concept_impl_t = ::boost::type_erasure::detail::normalize_concept_impl_f< ::boost::mpl::pair< ::boost::mp11::mp_list<>, ::boost::mp11::mp_list<> >, Concept >; template using normalize_concept_impl = ::boost::mpl::identity< ::boost::type_erasure::detail::normalize_concept_impl_t >; template using get_all_placeholders_impl = typename ::boost::type_erasure::detail::get_placeholders::type; template using get_all_placeholders = ::boost::mp11::mp_fold< Seq, ::boost::mp11::mp_list<>, ::boost::type_erasure::detail::get_all_placeholders_impl >; template using make_identity_pair = ::boost::mpl::pair; template using make_identity_placeholder_map = ::boost::mp11::mp_transform< ::boost::type_erasure::detail::make_identity_pair, ::boost::type_erasure::detail::get_all_placeholders< typename normalize_concept_impl_t::first > >; template using append_type_info = ::boost::mp11::mp_set_push_back< S, ::boost::type_erasure::typeid_ >; template using add_typeinfo_t = ::boost::mp11::mp_fold< get_all_placeholders, Seq, ::boost::type_erasure::detail::append_type_info >; // Seq should be a flattened mp_list sequence of leaf concepts. // adds typeid_

for every placeholder used. template using add_typeinfo = ::boost::mpl::identity >; template struct normalize_concept_substitute_f { template using apply = ::boost::mp11::mp_set_push_back::type >; }; template using normalize_concept_adjustments = ::boost::type_erasure::detail::eval_if< ::boost::type_erasure::is_relaxed::value, ::boost::type_erasure::detail::add_typeinfo_t, ::boost::type_erasure::detail::first, ::boost::mp11::mp_fold< typename Pair::first, ::boost::mp11::mp_list<>, ::boost::type_erasure::detail::normalize_concept_substitute_f< ::boost::type_erasure::detail::create_placeholder_map_t< typename Pair::second > >::template apply > >; template using get_placeholder_normalization_map_t = ::boost::type_erasure::detail::create_placeholder_map_t< typename normalize_concept_impl_t::second >; template using get_placeholder_normalization_map = ::boost::type_erasure::detail::create_placeholder_map< typename normalize_concept_impl_t::second >; // Flattens a Concept to an mpl::vector of primitive // concepts. Resolves same_type and deduced placeholders. template using normalize_concept_t = ::boost::type_erasure::detail::normalize_concept_adjustments< Concept, boost::type_erasure::detail::normalize_concept_impl_t >; template using normalize_concept = ::boost::mpl::identity< ::boost::type_erasure::detail::normalize_concept_t >; template struct collect_concepts_f; template using collect_concepts_recursive = ::boost::mp11::mp_fold< ::boost::type_erasure::detail::make_mp_list, Out, ::boost::type_erasure::detail::collect_concepts_f::template apply >; template using collect_concepts_impl = ::boost::type_erasure::detail::eval_if< ::boost::is_same::value, ::boost::type_erasure::detail::first, ::boost::mp11::mp_set_push_front, ::boost::type_erasure::detail::eval_if< ::boost::mpl::is_sequence::value, ::boost::type_erasure::detail::collect_concepts_recursive, ::boost::type_erasure::detail::first, Out, Concept, Map >, Transformed >; template::second >, class Out = ::boost::mp11::mp_list<> > using collect_concepts_t = collect_concepts_impl::type >; template struct collect_concepts_f { template using apply = ::boost::type_erasure::detail::collect_concepts_t; }; // Returns an MPL sequence containing all the concepts // in Concept. If Concept is considered as a DAG, // the result will be sorted topologically. template using collect_concepts = ::boost::mpl::identity< ::boost::type_erasure::detail::collect_concepts_t >; #endif } } } #endif