//ChangeLog: // 2011-01-17.1445CST // copied from post with headers: /* From: Paul Newsgroups: gmane.comp.lib.boost.user Subject: Re: large variant performance compared (50 elements) Date: Mon, 17 Jan 2011 21:18:54 +0100 Lines: 66 */ #ifndef VARIANT_TEST_SIZE #define VARIANT_TEST_SIZE 5 #endif #define VARIANT_TEST_CLOPTR 0 #define VARIANT_TEST_ONE_OF_MAYBE (VARIANT_TEST_CLOPTR+1) #define VARIANT_TEST_BOOST (VARIANT_TEST_ONE_OF_MAYBE+1) #ifndef VARIANT_TEST_IMPL #define VARIANT_TEST_IMPL VARIANT_TEST_BOOST #endif #include #include #include #include #if VARIANT_TEST_CLOPTR == VARIANT_TEST_IMPL #include "CLoPtrVariant.hpp" namespace var_impl = Layout; #pragma message "impl=CLoPtrVariant" #elif VARIANT_TEST_ONE_OF_MAYBE == VARIANT_TEST_IMPL #include #include #include namespace boost { namespace composite_storage { namespace pack { namespace multiple_dispatch { namespace testing { template < typename > struct make_variant_over ; template < typename... Components > struct make_variant_over < ::boost::mpl::vector > { typedef container < tags::one_of_maybe , mpl::integral_c , Components... > type ; }; template < typename Visitor , typename... Visitable > typename Visitor::result_type apply_visitor ( Visitor& visitor , Visitable&... visitable ) { return reify_apply(visitor,visitable...); } //Base class for visitor classes template class static_visitor { public: typedef R result_type; protected: // for use as base class only static_visitor() { } ~static_visitor() { } }; }//exit testing namespace }//exit multiple_dispatch namespace }//exit pack namespace }//exit composite_storage namespace }//exit boost namespace namespace var_impl = boost::composite_storage::pack::multiple_dispatch::testing; #pragma message "impl=OneOfMaybe" #elif VARIANT_TEST_BOOST == VARIANT_TEST_IMPL #include namespace var_impl = boost; #pragma message "impl=boost::variant" #endif #include #include #include #include class CVisitBinary : public var_impl::static_visitor { public: template bool operator()(const T&, const U&) const { return false; } template bool operator()(const T&, const T&) const { return true; } }; template < typename Typelist > void TestVariantBinary() { typedef typename var_impl::make_variant_over::type variant_t; int const ndx1=0; int const ndx2=1; typedef typename boost::mpl::at_c::type comp_type1; typedef typename boost::mpl::at_c::type comp_type2; comp_type1 comp_valu1; comp_type2 comp_valu2; #if VARIANT_TEST_ONE_OF_MAYBE == VARIANT_TEST_IMPL variant_t v1_1; v1_1.template inject(comp_valu1); variant_t v1_2; v1_2.template inject(comp_valu1); variant_t v2_1; v2_1.template inject(comp_valu2); #else variant_t v1_1(comp_valu1); variant_t v1_2(comp_valu1); variant_t v2_1(comp_valu2); #endif CVisitBinary visitor; BOOST_CHECK( var_impl::apply_visitor(visitor, v1_1, v1_2)); //Same type BOOST_CHECK( var_impl::apply_visitor(visitor, v1_1, v1_1)); //Same type, same instance BOOST_CHECK(!var_impl::apply_visitor(visitor, v1_1, v2_1)); //Different types } //Generate 200 types (class C1, C2, ... C200) #define BOOST_PP_LOCAL_MACRO(n) \ class BOOST_PP_CAT(I, n) \ { \ public: \ }; \ \ class BOOST_PP_CAT(C, n) \ : public BOOST_PP_CAT(I, n) \ { \ public: \ int i; \ }; #define BOOST_PP_LOCAL_LIMITS (0, VARIANT_TEST_SIZE) #include BOOST_PP_LOCAL_ITERATE() //Generate mpl sequences #define SHARED_PTR_C(z, n, data) boost::shared_ptr typedef boost::mpl::vector MplVectorCI_t; void test() { TestVariantBinary(); //2 phase visitation giving 100 paths }