ultimatepp/bazaar/PolyXML/ClassFactory.h
micio 4f9689dada PolyXML - Change method name of Create to CreateInstance
Avoids clashes with Ctrl::Create() method when deriving from Ctrl classes

git-svn-id: svn://ultimatepp.org/upp/trunk@1891 f0d560ea-af0d-0410-9eb7-867de7ffcac7
2010-01-14 07:54:54 +00:00

50 lines
1.7 KiB
C++

#ifndef _ClassFactory_h_
#define _ClassFactory_h_
#include <Core/Core.h>
NAMESPACE_UPP;
template<class T> class WithFactory
{
private:
typedef One<T> (*CreateFunc)();
typedef VectorMap<String, CreateFunc>mapType;
static mapType &classMap() { static mapType cMap; return cMap; }
static VectorMap<String, String> &typeMap() { static VectorMap<String, String> tMap; return tMap; }
static VectorMap<String, String> &descMap() { static VectorMap<String, String> dMap; return dMap; }
static VectorMap<String, int> &indexMap() { static VectorMap<String, int> iMap; return iMap; }
template<class D> static One<T> __Create(void) { return One<T>((T *)new D); }
public:
template<class D> static void Register(const String &name, const String &desc = "", int idx = 0)
{
classMap().Add(name, __Create<D>);
typeMap().Add(typeid(D).name(), name);
descMap().Add(name, desc);
indexMap().Add(name, idx);
}
static One<T> CreateInstance(const String &className) { return classMap().Get(className)(); }
static T *CreatePtr(String const &className) { return classMap().Get(className)().Detach(); }
static Vector<String> const &Classes(void) { return classMap().GetKeys(); }
static String const &GetClassDescription(const String &className) { return descMap().Get(className); }
static int const &GetClassIndex(const String &className) { return indexMap().Get(className); }
String const &IsA(void) { return typeMap().Get(typeid(*this).name()); }
virtual ~WithFactory() {}
};
#ifdef COMPILER_MSC
#define REGISTERCLASS(type, ...) \
INITBLOCK { \
type::Register<type>(#type, __VA_ARGS__); \
}
#else
#define REGISTERCLASS(type, ...) \
INITBLOCK { \
type::Register<type>(#type, ##__VA_ARGS__); \
}
#endif
END_UPP_NAMESPACE;
#endif