// play_with_any.cpp : Defines the entry point for the console application. // #include #include #include #include #include #include #include "TailoredAny.h" typedef std::basic_string tstring; //////////////////////////////////////////////////////////////////////////////////////////////////// // 'typical' properties structure template struct Properties; template<> struct Properties { bool bool1; int int1; int int2; int int3; int int4; double double1; tstring string1; }; template struct Properties { Variant_ bool1; Variant_ int1; Variant_ int2; Variant_ int3; Variant_ int4; Variant_ double1; Variant_ string1; }; const tstring text(_T("Some new string property")); #define ASSIGN_PROPERTIES(p) \ p.bool1 = true;\ p.int1 = 1;\ p.int1 = 2;\ p.int3 = 3;\ p.int4 = 4;\ p.double1 = 3.14;\ p.string1 = text; //////////////////////////////////////////////////////////////////////////////////////////////////// // custom type info strategy template struct TypeInfoTraits { enum { type = -1 }; }; template<> struct TypeInfoTraits { enum { type = 0 }; }; template<> struct TypeInfoTraits { enum { type = 1 }; }; template<> struct TypeInfoTraits { enum { type = 2 }; }; template<> struct TypeInfoTraits { enum { type = 3 }; }; template<> struct TypeInfoTraits { enum { type = 4 }; }; class CustomTypeInfoStrategy { static int TypeImpl(Loki::Int2Type<-1>); template static int TypeImpl(Loki::Int2Type) { return N; } public: typedef int TypeInfo; template static TypeInfo Type() { return TypeImpl(Loki::Int2Type::type>()); } template static TypeInfo Type(const T&) { return Type(); } }; //////////////////////////////////////////////////////////////////////////////////////////////////// typedef std::vector > SIMPLE_VEC; typedef std::vector > BOOST_ANY_VEC; // must be the same as with boost::any (1) typedef std::vector > > TAILORED_ANY_VEC; // using with Loki::SmallObject for allocation (2) typedef std::vector > > > TAILORED_ANY_VEC_M; const size_t N = 0x10000; //////////////////////////////////////////////////////////////////////////////////////////////////// // creation void CreateS(SIMPLE_VEC& v) { SIMPLE_VEC(N).swap(v); } void CreateA(BOOST_ANY_VEC& v) { BOOST_ANY_VEC(N).swap(v); } void CreateT(TAILORED_ANY_VEC& v) { TAILORED_ANY_VEC(N).swap(v); } void CreateTm(TAILORED_ANY_VEC_M& v) { TAILORED_ANY_VEC_M(N).swap(v); } //////////////////////////////////////////////////////////////////////////////////////////////////// // assignment void AssignS(SIMPLE_VEC& v) { for(size_t i = v.size(); i--;) { SIMPLE_VEC::value_type& p = v[i]; ASSIGN_PROPERTIES(p) } } void AssignA(BOOST_ANY_VEC& v) { for(size_t i = v.size(); i--;) { BOOST_ANY_VEC::value_type& p = v[i]; ASSIGN_PROPERTIES(p) } } void AssignT(TAILORED_ANY_VEC& v) { for(size_t i = v.size(); i--;) { TAILORED_ANY_VEC::value_type& p = v[i]; ASSIGN_PROPERTIES(p) } } void AssignTm(TAILORED_ANY_VEC_M& v) { for(size_t i = v.size(); i--;) { TAILORED_ANY_VEC_M::value_type& p = v[i]; ASSIGN_PROPERTIES(p) } } //////////////////////////////////////////////////////////////////////////////////////////////////// int _tmain(int argc, _TCHAR* argv[]) { SIMPLE_VEC v1(0); BOOST_ANY_VEC v2(0); TAILORED_ANY_VEC v3(0); TAILORED_ANY_VEC_M v4(0); CreateS(v1); CreateA(v2); CreateT(v3); CreateTm(v4); AssignS(v1); AssignA(v2); AssignT(v3); AssignTm(v4); return 0; } ////////////////////////////////////////////////////////////////////////////////////////////////////