Re: Templates vs factories
My previous message was lost somehow, so I repost it.
I solved both problems which you highlighted:
1. ODR violation problem. It's solved by using anonymous namespace.
2. Unique id generation problem. It's solved by using
boost.preprocessor.
Here is complete implementation.
========== Factory.h ==============
#ifndef FACTORY_H_
#define FACTORY_H_
#include <memory>
#include <string>
#include <boost/ptr_container/ptr_map.hpp>
#include <boost/preprocessor/slot/slot.hpp>
template <class T>
struct Interface
{
virtual ~Interface() {}
};
template <class T>
struct Creator
{
virtual ~Creator() {}
virtual std::auto_ptr<Interface<T> > Create() const = 0;
};
namespace
{
template <class T>
struct Factory;
template <class T, size_t I>
struct Registrar
{
static void Register(Factory<T> &) {}
};
template <class T>
struct Factory
{
Factory()
{
Registrar<T, 0>::Register(*this);
}
std::auto_ptr<Interface<T> > Create(const char * name) const
{
CreatorMap::const_iterator i = creators_.find(name);
if (i == creators_.end())
return std::auto_ptr<Interface<T> >();
else
return i->Create();
}
void Register(const char * name, std::auto_ptr<Creator<T> > creator)
{
std::string sname(name);
creators_.insert(sname, creator.release());
}
static Factory & Instance()
{
static Factory factory;
return factory;
}
private:
typedef boost::ptr_map<std::string, Creator<T> > CreatorMap;
CreatorMap creators_;
};
}
template <class T, template <class> class Impl>
struct CreatorImpl : Creator<T>
{
virtual std::auto_ptr<Interface<T> > Create() const
{
return std::auto_ptr<Interface<T> >(new Impl<T>);
}
};
#define BOOST_PP_VALUE 0
#include BOOST_PP_ASSIGN_SLOT(1)
#endif //FACTORY_H_
============= RegisterType.h ================
#include <boost/preprocessor/cat.hpp>
#include <boost/preprocessor/stringize.hpp>
#ifndef TYPE_NAME
# error You should define TYPE_NAME before including RegisterType.h
#endif
namespace
{
template <class T>
struct Registrar<T, BOOST_PP_SLOT(1)>
{
static void Register(Factory<T> & f)
{
auto_ptr<Creator<T> > creator(new CreatorImpl<T, TYPE_NAME>);
f.Register(BOOST_PP_STRINGIZE(TYPE_NAME), creator);
Registrar<T, BOOST_PP_SLOT(1) + 1>::Register(f);
}
};
}
#define BOOST_PP_VALUE BOOST_PP_SLOT(1) + 1
#include BOOST_PP_ASSIGN_SLOT(1)
#undef TYPE_NAME
============== Foo.h ===============
#ifndef FOO_H_
#define FOO_H_
#include "Factory.h"
template <class T>
struct Foo : Interface<T>
{
};
#define TYPE_NAME Foo
#include "RegisterType.h"
#endif //FOO_H_
=============== Bar.h ==================
#ifndef BAR_H_
#define BAR_H_
#include "Factory.h"
template <class T>
struct Bar : Interface<T>
{
};
#define TYPE_NAME Bar
#include "RegisterType.h"
#endif //BAR_H_
=============== UnitA.cpp =================
#include <iostream>
#include "Foo.h"
using namespace std;
void UnitA()
{
auto_ptr<Interface<int> >
foo(Factory<int>::Instance().Create("Foo"));
auto_ptr<Interface<int> >
bar(Factory<int>::Instance().Create("Bar"));
cout << "UnitA: " << foo.get() << endl;
cout << "UnitA: " << bar.get() << endl;
}
============== UnitB.cpp ===================
#include <iostream>
#include "Foo.h"
#include "Bar.h"
using namespace std;
void UnitB()
{
auto_ptr<Interface<int> >
foo(Factory<int>::Instance().Create("Foo"));
auto_ptr<Interface<int> >
bar(Factory<int>::Instance().Create("Bar"));
cout << "UnitB: " << foo.get() << endl;
cout << "UnitB: " << bar.get() << endl;
}
=============== Main.cpp ==================
void UnitA();
void UnitB();
int main(int argc, char * argv[])
{
UnitA();
UnitB();
}
==========================================
Let's see how it works. Registrar is declared in anonymous namespace
=> we have different implementations of Registrar in different
translation unit and it does not violate ORD. I placed Factory to
anonymous namespace as well because it uses Registrar => it has
different implementation in different translation units.
Now we have separate factories for each translation unit. Each factory
can create only objects which headers included in that translation
units. UnitA will successfully create Foo but will fail to create Bar.
UnitB will successfully create both Foo and Bar.
HTH,
Roman Perepelitsa.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]