Chris@16: /* Flyweight class. Chris@16: * Chris@101: * Copyright 2006-2014 Joaquin M Lopez Munoz. Chris@16: * Distributed under the Boost Software License, Version 1.0. Chris@16: * (See accompanying file LICENSE_1_0.txt or copy at Chris@16: * http://www.boost.org/LICENSE_1_0.txt) Chris@16: * Chris@16: * See http://www.boost.org/libs/flyweight for library home page. Chris@16: */ Chris@16: Chris@16: #ifndef BOOST_FLYWEIGHT_FLYWEIGHT_HPP Chris@16: #define BOOST_FLYWEIGHT_FLYWEIGHT_HPP Chris@16: Chris@101: #if defined(_MSC_VER) Chris@16: #pragma once Chris@16: #endif Chris@16: Chris@16: #include /* keep it first to prevent nasty warns in MSVC */ Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@101: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@101: #if !defined(BOOST_NO_SFINAE)&&!defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST) Chris@101: #include Chris@101: #include Chris@101: #include Chris@101: #endif Chris@101: Chris@16: #if BOOST_WORKAROUND(BOOST_MSVC,BOOST_TESTED_AT(1400)) Chris@16: #pragma warning(push) Chris@101: #pragma warning(disable:4520) /* multiple default ctors */ Chris@16: #pragma warning(disable:4521) /* multiple copy ctors */ Chris@16: #endif Chris@16: Chris@16: namespace boost{ Chris@16: Chris@16: namespace flyweights{ Chris@16: Chris@16: namespace detail{ Chris@16: Chris@16: /* Used for the detection of unmatched template args in a Chris@16: * flyweight instantiation. Chris@16: */ Chris@16: Chris@16: struct unmatched_arg; Chris@16: Chris@16: /* Boost.Parameter structures for use in flyweight. Chris@16: * NB: these types are derived from instead of typedef'd to force their Chris@16: * instantiation, which solves http://bugs.sun.com/view_bug.do?bug_id=6782987 Chris@16: * as found out by Simon Atanasyan. Chris@16: */ Chris@16: Chris@16: struct flyweight_signature: Chris@16: parameter::parameters< Chris@16: parameter::optional< Chris@16: parameter::deduced >, Chris@16: detail::is_tag Chris@16: >, Chris@16: parameter::optional< Chris@16: parameter::deduced >, Chris@16: is_tracking Chris@16: >, Chris@16: parameter::optional< Chris@16: parameter::deduced >, Chris@16: is_factory Chris@16: >, Chris@16: parameter::optional< Chris@16: parameter::deduced >, Chris@16: is_locking Chris@16: >, Chris@16: parameter::optional< Chris@16: parameter::deduced >, Chris@16: is_holder Chris@16: > Chris@16: > Chris@16: {}; Chris@16: Chris@16: struct flyweight_unmatched_signature: Chris@16: parameter::parameters< Chris@16: parameter::optional< Chris@16: parameter::deduced< Chris@16: detail::unmatched_arg Chris@16: >, Chris@16: mpl::not_< Chris@16: mpl::or_< Chris@16: detail::is_tag, Chris@16: is_tracking, Chris@16: is_factory, Chris@16: is_locking, Chris@16: is_holder Chris@16: > Chris@16: > Chris@16: > Chris@16: > Chris@16: {}; Chris@16: Chris@16: } /* namespace flyweights::detail */ Chris@16: Chris@16: template< Chris@16: typename T, Chris@16: typename Arg1,typename Arg2,typename Arg3,typename Arg4,typename Arg5 Chris@16: > Chris@16: class flyweight Chris@16: { Chris@16: private: Chris@16: typedef typename mpl::if_< Chris@16: detail::is_value, Chris@16: T, Chris@16: detail::default_value_policy Chris@16: >::type value_policy; Chris@16: typedef typename detail:: Chris@16: flyweight_signature::bind< Chris@16: Arg1,Arg2,Arg3,Arg4,Arg5 Chris@16: >::type args; Chris@16: typedef typename parameter::binding< Chris@16: args,tag<>,mpl::na Chris@16: >::type tag_type; Chris@16: typedef typename parameter::binding< Chris@16: args,tracking<>,refcounted Chris@16: >::type tracking_policy; Chris@16: typedef typename parameter::binding< Chris@16: args,factory<>,hashed_factory<> Chris@16: >::type factory_specifier; Chris@16: typedef typename parameter::binding< Chris@16: args,locking<>,simple_locking Chris@16: >::type locking_policy; Chris@16: typedef typename parameter::binding< Chris@16: args,holder<>,static_holder Chris@16: >::type holder_specifier; Chris@16: Chris@16: typedef typename detail:: Chris@16: flyweight_unmatched_signature::bind< Chris@16: Arg1,Arg2,Arg3,Arg4,Arg5 Chris@16: >::type unmatched_args; Chris@16: typedef typename parameter::binding< Chris@16: unmatched_args,detail::unmatched_arg, Chris@16: detail::unmatched_arg Chris@16: >::type unmatched_arg_detected; Chris@16: Chris@16: /* You have passed a type in the specification of a flyweight type that Chris@16: * could not be interpreted as a valid argument. Chris@16: */ Chris@16: BOOST_MPL_ASSERT_MSG( Chris@16: (is_same::value), Chris@16: INVALID_ARGUMENT_TO_FLYWEIGHT, Chris@16: (flyweight)); Chris@16: Chris@16: typedef detail::flyweight_core< Chris@16: value_policy,tag_type,tracking_policy, Chris@16: factory_specifier,locking_policy, Chris@16: holder_specifier Chris@16: > core; Chris@16: typedef typename core::handle_type handle_type; Chris@16: Chris@16: public: Chris@16: typedef typename value_policy::key_type key_type; Chris@16: typedef typename value_policy::value_type value_type; Chris@16: Chris@16: /* static data initialization */ Chris@16: Chris@16: static bool init(){return core::init();} Chris@16: Chris@16: class initializer Chris@16: { Chris@16: public: Chris@16: initializer():b(init()){} Chris@16: private: Chris@16: bool b; Chris@16: }; Chris@16: Chris@16: /* construct/copy/destroy */ Chris@101: Chris@101: flyweight():h(core::insert()){} Chris@16: Chris@101: #define BOOST_FLYWEIGHT_PERFECT_FWD_CTR_BODY(args) \ Chris@101: :h(core::insert(BOOST_FLYWEIGHT_FORWARD(args))){} Chris@101: Chris@101: BOOST_FLYWEIGHT_PERFECT_FWD_WITH_ARGS( Chris@101: explicit flyweight, Chris@101: BOOST_FLYWEIGHT_PERFECT_FWD_CTR_BODY) Chris@101: Chris@101: #undef BOOST_FLYWEIGHT_PERFECT_FWD_CTR_BODY Chris@101: Chris@101: #if !defined(BOOST_NO_SFINAE)&&!defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST) Chris@101: template Chris@101: flyweight( Chris@101: std::initializer_list list, Chris@101: typename boost::enable_if< Chris@101: boost::is_convertible,key_type> >::type* =0): Chris@101: h(core::insert(list)){} Chris@101: #endif Chris@101: Chris@16: flyweight(const flyweight& x):h(x.h){} Chris@16: flyweight(flyweight& x):h(x.h){} Chris@16: Chris@101: #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) Chris@101: flyweight(const flyweight&& x):h(x.h){} Chris@101: flyweight(flyweight&& x):h(x.h){} Chris@101: #endif Chris@16: Chris@101: #if !defined(BOOST_NO_SFINAE)&&!defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST) Chris@101: template Chris@101: typename boost::enable_if< Chris@101: boost::is_convertible,key_type>,flyweight&>::type Chris@101: operator=(std::initializer_list list) Chris@101: { Chris@101: return operator=(flyweight(list)); Chris@101: } Chris@101: #endif Chris@16: Chris@16: flyweight& operator=(const flyweight& x){h=x.h;return *this;} Chris@16: flyweight& operator=(const value_type& x){return operator=(flyweight(x));} Chris@16: Chris@101: #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) Chris@101: flyweight& operator=(value_type&& x) Chris@101: { Chris@101: return operator=(flyweight(std::move(x))); Chris@101: } Chris@101: #endif Chris@101: Chris@16: /* convertibility to underlying type */ Chris@16: Chris@16: const key_type& get_key()const{return core::key(h);} Chris@16: const value_type& get()const{return core::value(h);} Chris@16: operator const value_type&()const{return get();} Chris@16: Chris@16: /* exact type equality */ Chris@16: Chris@16: friend bool operator==(const flyweight& x,const flyweight& y) Chris@16: { Chris@16: return &x.get()==&y.get(); Chris@16: } Chris@16: Chris@16: /* modifiers */ Chris@16: Chris@16: void swap(flyweight& x){boost::swap(h,x.h);} Chris@16: Chris@16: private: Chris@16: handle_type h; Chris@16: }; Chris@16: Chris@16: #define BOOST_FLYWEIGHT_TYPENAME_TEMPL_ARGS(n) \ Chris@16: typename Arg##n##1,typename Arg##n##2,typename Arg##n##3, \ Chris@16: typename Arg##n##4,typename Arg##n##5 Chris@16: #define BOOST_FLYWEIGHT_TEMPL_ARGS(n) \ Chris@16: Arg##n##1,Arg##n##2,Arg##n##3,Arg##n##4,Arg##n##5 Chris@16: Chris@16: /* Comparison. Unlike exact type comparison defined above, intertype Chris@16: * comparison just forwards to the underlying objects. Chris@16: */ Chris@16: Chris@16: template< Chris@16: typename T1,BOOST_FLYWEIGHT_TYPENAME_TEMPL_ARGS(1), Chris@16: typename T2,BOOST_FLYWEIGHT_TYPENAME_TEMPL_ARGS(2) Chris@16: > Chris@16: bool operator==( Chris@16: const flyweight& x, Chris@16: const flyweight& y) Chris@16: { Chris@16: return x.get()==y.get(); Chris@16: } Chris@16: Chris@16: template< Chris@16: typename T1,BOOST_FLYWEIGHT_TYPENAME_TEMPL_ARGS(1), Chris@16: typename T2,BOOST_FLYWEIGHT_TYPENAME_TEMPL_ARGS(2) Chris@16: > Chris@16: bool operator<( Chris@16: const flyweight& x, Chris@16: const flyweight& y) Chris@16: { Chris@16: return x.get() Chris@16: bool operator==( Chris@16: const flyweight& x,const T2& y) Chris@16: { Chris@16: return x.get()==y; Chris@16: } Chris@16: Chris@16: template< Chris@16: typename T1, Chris@16: typename T2,BOOST_FLYWEIGHT_TYPENAME_TEMPL_ARGS(2) Chris@16: > Chris@16: bool operator==( Chris@16: const T1& x,const flyweight& y) Chris@16: { Chris@16: return x==y.get(); Chris@16: } Chris@16: Chris@16: template< Chris@16: typename T1,BOOST_FLYWEIGHT_TYPENAME_TEMPL_ARGS(1), Chris@16: typename T2 Chris@16: > Chris@16: bool operator<( Chris@16: const flyweight& x,const T2& y) Chris@16: { Chris@16: return x.get() Chris@16: bool operator<( Chris@16: const T1& x,const flyweight& y) Chris@16: { Chris@16: return x \ Chris@16: inline bool operator!=(const a1& x,const a2& y) \ Chris@16: { \ Chris@16: return !(x==y); \ Chris@16: } \ Chris@16: \ Chris@16: template \ Chris@16: inline bool operator>(const a1& x,const a2& y) \ Chris@16: { \ Chris@16: return y \ Chris@16: inline bool operator>=(const a1& x,const a2& y) \ Chris@16: { \ Chris@16: return !(x \ Chris@16: inline bool operator<=(const a1& x,const a2& y) \ Chris@16: { \ Chris@16: return !(y, Chris@16: flyweight< Chris@16: T2 BOOST_PP_COMMA() BOOST_FLYWEIGHT_TEMPL_ARGS(2) Chris@16: >) Chris@16: Chris@16: #if !defined(BOOST_NO_FUNCTION_TEMPLATE_ORDERING) Chris@16: BOOST_FLYWEIGHT_COMPLETE_COMP_OPS( Chris@16: typename T1 BOOST_PP_COMMA() Chris@16: BOOST_FLYWEIGHT_TYPENAME_TEMPL_ARGS(1) BOOST_PP_COMMA() Chris@16: typename T2, Chris@16: flyweight< Chris@16: T1 BOOST_PP_COMMA() BOOST_FLYWEIGHT_TEMPL_ARGS(1) Chris@16: >, Chris@16: T2) Chris@16: Chris@16: BOOST_FLYWEIGHT_COMPLETE_COMP_OPS( Chris@16: typename T1 BOOST_PP_COMMA() Chris@16: typename T2 BOOST_PP_COMMA() Chris@16: BOOST_FLYWEIGHT_TYPENAME_TEMPL_ARGS(2), Chris@16: T1, Chris@16: flyweight< Chris@16: T2 BOOST_PP_COMMA() BOOST_FLYWEIGHT_TEMPL_ARGS(2) Chris@16: >) Chris@16: #endif /* !defined(BOOST_NO_FUNCTION_TEMPLATE_ORDERING) */ Chris@16: Chris@16: /* specialized algorithms */ Chris@16: Chris@16: template Chris@16: void swap( Chris@16: flyweight& x, Chris@16: flyweight& y) Chris@16: { Chris@16: x.swap(y); Chris@16: } Chris@16: Chris@16: template< Chris@16: BOOST_TEMPLATED_STREAM_ARGS(ElemType,Traits) Chris@16: BOOST_TEMPLATED_STREAM_COMMA Chris@16: typename T,BOOST_FLYWEIGHT_TYPENAME_TEMPL_ARGS(_) Chris@16: > Chris@16: BOOST_TEMPLATED_STREAM(ostream,ElemType,Traits)& operator<<( Chris@16: BOOST_TEMPLATED_STREAM(ostream,ElemType,Traits)& out, Chris@16: const flyweight& x) Chris@16: { Chris@16: return out< Chris@16: BOOST_TEMPLATED_STREAM(istream,ElemType,Traits)& operator>>( Chris@16: BOOST_TEMPLATED_STREAM(istream,ElemType,Traits)& in, Chris@16: flyweight& x) Chris@16: { Chris@16: typedef typename flyweight< Chris@16: T,BOOST_FLYWEIGHT_TEMPL_ARGS(_) Chris@16: >::value_type value_type; Chris@16: Chris@16: /* value_type need not be default ctble but must be copy ctble */ Chris@16: value_type t(x.get()); Chris@16: in>>t; Chris@16: x=t; Chris@16: return in; Chris@16: } Chris@16: Chris@16: } /* namespace flyweights */ Chris@16: Chris@16: } /* namespace boost */ Chris@16: Chris@101: #if !defined(BOOST_FLYWEIGHT_DISABLE_HASH_SUPPORT) Chris@101: Chris@101: /* hash support */ Chris@101: Chris@101: #if !defined(BOOST_NO_CXX11_HDR_FUNCTIONAL) Chris@101: namespace std{ Chris@101: Chris@101: template Chris@101: struct hash > Chris@101: { Chris@101: typedef std::size_t result_type; Chris@101: typedef boost::flyweight< Chris@101: T,BOOST_FLYWEIGHT_TEMPL_ARGS(_)> argument_type; Chris@101: Chris@101: result_type operator()(const argument_type& x)const Chris@101: { Chris@101: typedef typename argument_type::value_type value_type; Chris@101: Chris@101: std::hash h; Chris@101: return h(&x.get()); Chris@101: } Chris@101: }; Chris@101: Chris@101: } /* namespace std */ Chris@101: #endif /* !defined(BOOST_NO_CXX11_HDR_FUNCTIONAL) */ Chris@101: Chris@101: namespace boost{ Chris@101: #if !defined(BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP) Chris@101: namespace flyweights{ Chris@101: #endif Chris@101: Chris@101: template Chris@101: std::size_t hash_value(const flyweight& x) Chris@101: { Chris@101: typedef typename flyweight< Chris@101: T,BOOST_FLYWEIGHT_TEMPL_ARGS(_) Chris@101: >::value_type value_type; Chris@101: Chris@101: boost::hash h; Chris@101: return h(&x.get()); Chris@101: } Chris@101: Chris@101: #if !defined(BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP) Chris@101: } /* namespace flyweights */ Chris@101: #endif Chris@101: } /* namespace boost */ Chris@101: #endif /* !defined(BOOST_FLYWEIGHT_DISABLE_HASH_SUPPORT) */ Chris@101: Chris@16: #undef BOOST_FLYWEIGHT_COMPLETE_COMP_OPS Chris@16: #undef BOOST_FLYWEIGHT_TEMPL_ARGS Chris@16: #undef BOOST_FLYWEIGHT_TYPENAME_TEMPL_ARGS Chris@16: Chris@16: #if BOOST_WORKAROUND(BOOST_MSVC,BOOST_TESTED_AT(1400)) Chris@16: #pragma warning(pop) Chris@16: #endif Chris@16: Chris@16: #endif