1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
|
#ifndef OBJECT_FACTORY_H
#define OBJECT_FACTORY_H
#include <map>
#include <boost/noncopyable.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/function.hpp>
#include <string>
template <typename T>
class object_factory : private boost::noncopyable
{
public:
static object_factory& instance()
{
static object_factory<T> instance;
return instance;
}
void register_object(const std::string& str, boost::function<boost::shared_ptr<T>(void)> func)
{
m_map[str] = func;
}
boost::shared_ptr<T> create(const std::string& str)
{
return m_map[str]();
}
private:
object_factory() {}
std::map<std::string, boost::function<boost::shared_ptr<T>(void)> > m_map;
};
#endif |
Partager