#include #include template struct key_value_pair { key_value_pair(const Key& k_):k(k_),v(0){} ~key_value_pair(){delete v;} Key k; mutable Value* v; }; template struct adapted_entry:Entry { adapted_entry(const key_value_pair& x):Entry(x){} operator const Key&()const { return static_cast&>(*this).k; } }; template class keyed_flyweight_core; template struct keyed_flyweight_core_tracking_helper { private: typedef keyed_flyweight_core core; typedef typename core::handle_type handle_type; typedef typename core::entry_type entry_type; public: static const entry_type& entry(const handle_type& h) { return core::factory().entry(h); } template static void erase(const handle_type& h,Checker check) { typedef typename core::lock_type lock_type; lock_type lock(core::mutex()); if(check(h))core::factory().erase(h); } }; template class keyed_flyweight_core { public: typedef boost::flyweights::refcounted tracking_policy; typedef key_value_pair key_value_pair_type; typedef typename boost::mpl::apply1< typename tracking_policy::entry_type, key_value_pair_type >::type base_entry_type; typedef adapted_entry< Key,Value,base_entry_type > entry_type; typedef boost::flyweights::hashed_factory<> factory_specifier; typedef typename boost::mpl::apply2< factory_specifier, entry_type, Key >::type base_factory_type; typedef typename boost::mpl::apply2< typename tracking_policy::handle_type, typename base_factory_type::handle_type, keyed_flyweight_core_tracking_helper< Key,Value > >::type handle_type; typedef boost::flyweights::detail::handle_factory_adaptor< base_factory_type, handle_type,entry_type > factory_type; typedef boost::flyweights::simple_locking locking_policy; typedef typename locking_policy::mutex_type mutex_type; typedef typename locking_policy::lock_type lock_type; static bool init(){return &(factory())!=0;} static handle_type insert(const Key& x) { lock_type lock(mutex()); handle_type h=factory().insert(entry_type(x)); if(!static_cast(factory().entry(h)).v){ static_cast(factory().entry(h)).v= new Value(x); } return h; } static const Value& value(const handle_type& h) { return *(static_cast(factory().entry(h)).v); } static factory_type& factory() { (void)static_force_holder_get; return holder_type::get().factory; } static mutex_type& mutex() { (void)static_force_holder_get; return holder_type::get().mutex; } private: struct holder_arg { factory_type factory; mutex_type mutex; }; typedef boost::flyweights::static_holder holder_specifier; typedef typename boost::mpl::apply1< holder_specifier, holder_arg >::type holder_type; static bool static_force_holder_get; }; template bool keyed_flyweight_core::static_force_holder_get= &(keyed_flyweight_core::holder_type::get())!=0; template class keyed_flyweight { private: typedef keyed_flyweight_core< Key,T > core; typedef typename core::handle_type handle_type; public: typedef T value_type; keyed_flyweight():h(core::insert(Key())){} keyed_flyweight(const Key& k):h(core::insert(k)){} keyed_flyweight(const keyed_flyweight& x):h(x.h){} keyed_flyweight& operator=(const Key& k){return operator=(flyweight(k));} const T& get()const{return core::value(h);} operator const T&()const{return get();} void swap(keyed_flyweight& x){std::swap(h,x.h);} private: handle_type h; }; struct character { character(char c):c(c){} char c; }; int main() { typedef keyed_flyweight fw_t; fw_t x1('a'); fw_t x2('a'); fw_t x3('b'); std::cout<