I'm writing template factory that stores function pointer to create user objects.
I would like to support user Creation function with and without argument (for now, one or zero argument will do).
(and i can't use boost or c11 unfortunately )
template< typename T, typename K, typename D >
//T (Type) is a polymorphic type
//K (Key) should have operator < and be copyable
//D (Data argument for creator function) can be copyable
class Factory
{
public:
typedef std::tr1::shared_ptr<T> shared_ptr;
//auto gen Ctor and Dtor
inline void Add( const K& key_, T* (*CreatorFunc)(D) );
inline shared_ptr Create(const K& key_, const D& initData_ ) const;
private:
std::map<K, T* (*)(D) > m_creator;
};
I would love it if the user can use it like this:
class c1
{
public:
explicit c1(const string& st);
...
};
class c2
{
public:
explicit c2();
...
};
c1* CreateC1(const string& st){ return new c1(st);}
c2* CreateC2(){ return new c2;}
...
//Factory<type, key, arguments>
Factory<c1, int, string> f;
f.Add(0, CreateC1);
f.Create(0, "string Arg");
//Factory<type, key>
Factory<c2, int> f2;
f2.Add(0, CreateC2);
f2.Create(0);
i did manage to get this to work with some ugly template specialization. i feel like i'm going at it the wrong way.
my solution:
class EmptyClass {};
template< typename T, typename K, typename D = EmptyClass>
class Factory
{
public:
typedef std::tr1::shared_ptr<T> shared_ptr;
inline void Add( const K& key_, T* (*CreatorFunc)(D) );
inline shared_ptr Create(const K& key_, const D& initData_ ) const;
private:
std::map<K, T* (*)(D) > m_creator;
};
template<>
template< typename T, typename K >
class Factory<T, K, EmptyClass >
{
public:
typedef std::tr1::shared_ptr<T> shared_ptr;
inline void Add( const K& key_, T* (*CreatorFunc)() );
inline shared_ptr Create(const K& key_) const;
private:
std::map<K, T* (*)() > m_creator;
};
feel free to criticize anything else if you wany
If you can use variadic template maybe this would work for you:
A possible implementation might look like this: