Boost logo

Boost Users :

Subject: Re: [Boost-users] how to make a vector to derivedClass::create() methods
From: Victor Whiskey Yankee (victor.whiskey.yankee_at_[hidden])
Date: 2009-02-07 21:54:45


Victor.Whiskey.Yankee wrote:
>
>
> > Thank you for your patience.
> > I am really wanting to make a factory that holds a
> > std::map where the pair.first is a std::string and
> > the pair.second is somehow a pointer to a constructor or create()
> > method to any of several derived classes>.
> >
> > I thought expressing in terms of vector might be simplified and enough
> > for my understanding.
> >
>
> I see. How about using Boost.Function like this:
>
> std::map<std::string, boost::function<Base*()> > factory_impl;
>
> //...
>
> factory_impl["Derived1"] = boost::lambda::new_ptr<Derived1>();
>
> // ...
>
> Base* b = factory_impl["Derived1"]()

Thank you Steve for your solution, it is working fine. I have a nice
thread-safe singleton factory now, and it is generic
using templates.

I am now trying to make my derived classes register with the factory
automatically, but cannot figure it out.
I basically have this:

template<typename BaseType, typename keyType>
class GenericFactory
{
public:
    GenericFactory();

    boost::shared_ptr<BaseType> create(const keyType& typeName);

    template<typeName DerivedType>
    void registrate(const keyType& _key);
};

As said, I can register my derived classes with the above just fnie.

I have tried writing a help class like I've seened "RegisterInFactory",
but it escapes me.

Thanks,
Vic


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