Hello, Is it possible to initialize a flyweight lazy or better on user request? I have the problem that I have a custom allocator that is not ready to run before main. Currently, the flyweight initialization is performed on init of the fleiweight_core static_initilaizer member. This scheme does outrule the usage of the flyweight utility in my application. Any help would be appreciated. Best, Johannes ____________ Virus checked by G DATA AntiVirusKit Version: AVF 19.736 from 21.02.2010 Virus news: www.antiviruslab.com
Johannes Brunen escribió:
Hello,
Is it possible to initialize a flyweight lazy or better on user request? I have the problem that I have a custom allocator that is not ready to run before main. Currently, the flyweight initialization is performed on init of the fleiweight_core static_initilaizer member. This scheme does outrule the usage of the flyweight utility in my application.
Any help would be appreciated.
Hi Johannes, You can write a custom lazy factory, just as the example attached. Hope this helps, Joaquín M López Muñoz Telefónica, Investigación y Desarrollo <cid:part1.03080702.04040303@tid.es> #include <boost/flyweight/factory_tag.hpp> #include <boost/flyweight/detail/not_placeholder_expr.hpp> #include <boost/mpl/apply.hpp> #include <string> template<typename BaseFactory,typename Entry> class lazy_factory_class:public boost::flyweights::factory_marker { public: typedef typename BaseFactory::handle_type handle_type; lazy_factory_class():factory_ptr(0){} ~lazy_factory_class(){delete factory_ptr;} handle_type insert(const Entry& x){return factory().insert(x);} void erase(handle_type h){factory().erase(h);} const Entry& entry(handle_type h){return factory().entry(h);} private: BaseFactory* factory_ptr; BaseFactory& factory() { if(!factory_ptr)factory_ptr=new BaseFactory; return *factory_ptr; } }; template< typename BaseFactorySpecifier BOOST_FLYWEIGHT_NOT_A_PLACEHOLDER_EXPRESSION
struct lazy_factory:public boost::flyweights::factory_marker { template<typename Entry,typename Key> struct apply { typedef lazy_factory_class< typename boost::mpl::apply2<BaseFactorySpecifier,Entry,Key>::type, Entry > type; }; }; /* testing */ #include <boost/flyweight.hpp> using namespace boost::flyweights; typedef flyweight< std::string, lazy_factory<hashed_factory<> >
fw_string;
int main() { fw_string str("boost"); }
participants (2)
-
joaquin@tid.es
-
Johannes Brunen