|
Boost Users : |
Subject: Re: [Boost-users] flyweight: lazy initialization
From: joaquin_at_[hidden]
Date: 2010-02-23 10:58:26
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_at_[hidden]>
#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");
}
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net