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
#include
#include
#include <string>
template
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
struct apply
{
typedef lazy_factory_class<
typename boost::mpl::apply2::type,
Entry
> type;
};
};
/* testing */
#include
using namespace boost::flyweights;
typedef flyweight<
std::string,
lazy_factory >
fw_string;
int main()
{
fw_string str("boost");
}