00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #ifndef PG_FACTORY_H
00030 #define PG_FACTORY_H
00031
00032 #include <map>
00033 #include <string>
00034
00035 #include "pgsingleton.h"
00036
00042 class PG_Widget;
00043
00044 template< class T, class PT = PG_Widget >
00045 class PG_FactoryObject {
00046 public:
00047 static T* CreateObject(PT* parent) {
00048 return new T(parent);
00049 }
00050
00051 static T* CreateObject0() {
00052 return new T;
00053 }
00054 };
00055
00056 template<class H>
00057 class PG_FactoryHolder : public PG_Singleton< PG_FactoryHolder<H> > {
00058 public:
00059
00060 typedef PG_Widget* (*CREATEFN)(PG_Widget* parent);
00061
00062 template< class T, class PT >
00063 static void RegisterClass(const H& classname) {
00064 PG_Singleton< PG_FactoryHolder<H> >::GetInstance().RegisterCreateFn(classname, (CREATEFN)&PG_FactoryObject<T, PT>::CreateObject);
00065 }
00066
00067 template< class T >
00068 static void RegisterClass(const H& classname) {
00069 PG_Singleton< PG_FactoryHolder<H> >::GetInstance().RegisterCreateFn(classname, (CREATEFN)&PG_FactoryObject<T>::CreateObject);
00070 }
00071
00072 template< class T >
00073 static void RegisterClass0(const H& classname) {
00074 PG_Singleton< PG_FactoryHolder<H> >::GetInstance().RegisterCreateFn(classname, (CREATEFN)&PG_FactoryObject<T>::CreateObject0);
00075 }
00076
00077 static PG_Widget* CreateObject(const H& classname, PG_Widget* parent = NULL) {
00078 CREATEFN create = PG_Singleton< PG_FactoryHolder<H> >::GetInstance().creator_map[classname];
00079
00080 if(create == NULL) {
00081 return NULL;
00082 }
00083
00084 return create(parent);
00085 }
00086
00087 static PG_Widget* CreateObject0(const H& classname, PG_Widget* parent = NULL) {
00088 CREATEFN create = PG_Singleton< PG_FactoryHolder<H> >::GetInstance().creator_map[classname];
00089
00090 if(create == NULL) {
00091 return NULL;
00092 }
00093
00094 return create(parent);
00095 }
00096
00097 protected:
00098
00099 inline void RegisterCreateFn(const H& classname, CREATEFN fn) {
00100 creator_map[classname] = fn;
00101 }
00102
00103 std::map< H, CREATEFN > creator_map;
00104
00105 friend class PG_Singleton< PG_FactoryHolder<H> >;
00106
00107 };
00108
00109 typedef PG_FactoryHolder<std::string> PG_Factory;
00110
00111 #endif // PG_FACTORY_H