/* Flyweight class. * * Copyright 2006-2007 Joaquín M López Muñoz. * 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) * * See http://www.boost.org/libs/flyweight for library home page. */ #ifndef BOOST_FLYWEIGHT_FLYWEIGHT_HPP #define BOOST_FLYWEIGHT_FLYWEIGHT_HPP #if defined(_MSC_VER)&&(_MSC_VER>=1200) #pragma once #endif #include /* keep it first to prevent nasty warns in MSVC */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #if BOOST_WORKAROUND(BOOST_MSVC,BOOST_TESTED_AT(1400)) #pragma warning(push) #pragma warning(disable:4521) /* multiple copy ctors */ #endif namespace boost{ namespace flyweights{ namespace detail{ /* Used for the detection of unmatched template args in a * flyweight instantiation. */ struct unmatched_arg; } /* namespace flyweights::detail */ template< typename T, typename Arg1,typename Arg2,typename Arg3,typename Arg4,typename Arg5 > class flyweight { private: typedef parameter::parameters< parameter::optional< parameter::deduced >, detail::is_tag >, parameter::optional< parameter::deduced >, is_tracking >, parameter::optional< parameter::deduced >, is_factory >, parameter::optional< parameter::deduced >, is_locking >, parameter::optional< parameter::deduced >, is_holder > > signature; typedef typename signature::bind< Arg1,Arg2,Arg3,Arg4,Arg5 >::type args; typedef typename parameter::binding< args,tag<>,mpl::na >::type tag_type; typedef typename parameter::binding< args,tracking<>,refcounted >::type tracking_policy; typedef typename parameter::binding< args,factory<>,hashed_factory<> >::type factory_specifier; typedef typename parameter::binding< args,locking<>,simple_locking >::type locking_policy; typedef typename parameter::binding< args,holder<>,static_holder >::type holder_specifier; typedef parameter::parameters< parameter::optional< parameter::deduced< detail::unmatched_arg >, mpl::not_< mpl::or_< detail::is_tag, is_tracking, is_factory, is_locking, is_holder > > > > unmatched_signature; typedef typename unmatched_signature::bind< Arg1,Arg2,Arg3,Arg4,Arg5 >::type unmatched_args; typedef typename parameter::binding< unmatched_args,detail::unmatched_arg, detail::unmatched_arg >::type unmatched_arg_detected; BOOST_STATIC_ASSERT(( /* You have passed a type in the specification of a flyweight type that * could not be interpreted as a valid argument. */ is_same::value)); typedef detail::flyweight_core< T,tag_type,tracking_policy, factory_specifier,locking_policy, holder_specifier > core; typedef typename core::handle_type handle_type; public: typedef T value_type; /* static data initialization */ static bool init(){return core::init();} class initializer { public: initializer():b(init()){} private: bool b; }; /* construct/copy/destroy */ flyweight():h(core::insert(T())){} flyweight(const flyweight& x):h(x.h){} flyweight(flyweight& x):h(x.h){} BOOST_FLYWEIGHT_CTORS flyweight& operator=(const T& t){return operator=(flyweight(t));} /* convertibility to underlying type */ const T& get()const{return core::value(h);} operator const T&()const{return get();} /* exact type equality */ friend bool operator==(const flyweight& x,const flyweight& y) { return &x.get()==&y.get(); } /* modifiers */ void swap(flyweight& x){std::swap(h,x.h);} private: handle_type h; }; #define BOOST_FLYWEIGHT_TYPENAME_TEMPL_ARGS(n) \ typename Arg##n##1,typename Arg##n##2,typename Arg##n##3, \ typename Arg##n##4,typename Arg##n##5 #define BOOST_FLYWEIGHT_TEMPL_ARGS(n) \ Arg##n##1,Arg##n##2,Arg##n##3,Arg##n##4,Arg##n##5 /* Comparison. Unlike exact type comparison defined above, intertype * comparison just forwards to the underlying objects. */ template< typename T1,BOOST_FLYWEIGHT_TYPENAME_TEMPL_ARGS(1), typename T2,BOOST_FLYWEIGHT_TYPENAME_TEMPL_ARGS(2) > bool operator==( const flyweight& x, const flyweight& y) { return x.get()==y.get(); } template< typename T1,BOOST_FLYWEIGHT_TYPENAME_TEMPL_ARGS(1), typename T2,BOOST_FLYWEIGHT_TYPENAME_TEMPL_ARGS(2) > bool operator<( const flyweight& x, const flyweight& y) { return x.get() bool operator==( const flyweight& x,const T2& y) { return x.get()==y; } template< typename T1, typename T2,BOOST_FLYWEIGHT_TYPENAME_TEMPL_ARGS(2) > bool operator==( const T1& x,const flyweight& y) { return x==y.get(); } template< typename T1,BOOST_FLYWEIGHT_TYPENAME_TEMPL_ARGS(1), typename T2 > bool operator<( const flyweight& x,const T2& y) { return x.get() bool operator<( const T1& x,const flyweight& y) { return x \ inline bool operator!=(const a1& x,const a2& y) \ { \ return !(x==y); \ } \ \ template \ inline bool operator>(const a1& x,const a2& y) \ { \ return y \ inline bool operator>=(const a1& x,const a2& y) \ { \ return !(x \ inline bool operator<=(const a1& x,const a2& y) \ { \ return !(y, flyweight< T2 BOOST_PP_COMMA() BOOST_FLYWEIGHT_TEMPL_ARGS(2) >) #if !defined(BOOST_NO_FUNCTION_TEMPLATE_ORDERING) BOOST_FLYWEIGHT_COMPLETE_COMP_OPS( typename T1 BOOST_PP_COMMA() BOOST_FLYWEIGHT_TYPENAME_TEMPL_ARGS(1) BOOST_PP_COMMA() typename T2, flyweight< T1 BOOST_PP_COMMA() BOOST_FLYWEIGHT_TEMPL_ARGS(1) >, T2) BOOST_FLYWEIGHT_COMPLETE_COMP_OPS( typename T1 BOOST_PP_COMMA() typename T2 BOOST_PP_COMMA() BOOST_FLYWEIGHT_TYPENAME_TEMPL_ARGS(2), T1, flyweight< T2 BOOST_PP_COMMA() BOOST_FLYWEIGHT_TEMPL_ARGS(2) >) #endif /* !defined(BOOST_NO_FUNCTION_TEMPLATE_ORDERING) */ /* specialized algorithms */ template void swap( flyweight& x, flyweight& y) { x.swap(y); } template< BOOST_TEMPLATED_STREAM_ARGS(ElemType,Traits) BOOST_TEMPLATED_STREAM_COMMA typename T,BOOST_FLYWEIGHT_TYPENAME_TEMPL_ARGS(_) > BOOST_TEMPLATED_STREAM(ostream,ElemType,Traits)& operator<<( BOOST_TEMPLATED_STREAM(ostream,ElemType,Traits)& out, const flyweight& x) { return out< BOOST_TEMPLATED_STREAM(istream,ElemType,Traits)& operator>>( BOOST_TEMPLATED_STREAM(istream,ElemType,Traits)& in, flyweight& x) { T t(x.get()); /* T need not be default ctble but must be copy ctble */ in>>t; x=t; return in; } } /* namespace flyweights */ } /* namespace boost */ #undef BOOST_FLYWEIGHT_COMPLETE_COMP_OPS #undef BOOST_FLYWEIGHT_TEMPL_ARGS #undef BOOST_FLYWEIGHT_TYPENAME_TEMPL_ARGS #include #if BOOST_WORKAROUND(BOOST_MSVC,BOOST_TESTED_AT(1400)) #pragma warning(pop) #endif #endif