00001
00010 #ifndef MATERIALFACTORY_H
00011 #define MATERIALFACTORY_H
00012
00013 #include <string>
00014 #include <map>
00015 #include <libxml/parser.h>
00016 #include <libxml/xpath.h>
00017 #include <libxml/tree.h>
00018 #include <sigc++/sigc++.h>
00019
00020 #include "material.h"
00021 #include "renderable.h"
00022 #include "bumpmap.h"
00023
00028 typedef sigc::signal<Material *, std::map<std::string, std::string> > material_create_fn;
00029
00031 typedef sigc::signal<Renderable *, xmlNode *> primitive_create_fn;
00032
00034 typedef sigc::signal<BumpMap *, xmlNode *> bumpmap_create_fn;
00035
00038 typedef sigc::slot<Material *, std::map<std::string, std::string> > material_create_slot;
00039
00042 typedef sigc::slot<Renderable *, xmlNode *> primitive_create_slot;
00043
00046 typedef sigc::slot<BumpMap *, xmlNode *> bumpmap_create_slot;
00047
00048
00051 template <class _ParentType, class _SigType, class _SlotType>
00052 class PluginFactory {
00053 protected:
00054
00056 std::map<std::string, _SigType> m_createFunctions;
00057
00058 public:
00069 _ParentType * create(std::string type, std::map<std::string, std::string> attributes) {
00070 if(m_createFunctions.find(type) != m_createFunctions.end()) {
00071 _ParentType * tmp = m_createFunctions[type].emit(attributes);
00072 if(!tmp)
00073 log_warn("[deprecated] Create function for %s type returned NULL.", type.c_str());
00074 return tmp;
00075 }
00076
00077 log_warn("[deprecated] Could not find signal for %s type.", type.c_str());
00078 return NULL;
00079 }
00080
00081 _ParentType * create(std::string type, xmlNode * node) {
00082 if(m_createFunctions.find(type) != m_createFunctions.end()) {
00083 _ParentType * tmp = m_createFunctions[type].emit(node);
00084 if(!tmp)
00085 log_warn("Create function for <%s> node returned NULL.", node->name);
00086 return tmp;
00087 }
00088
00089 log_warn("Could not find signal for %s type.", type.c_str());
00090 return NULL;
00091 }
00092
00101 bool registerPlugin(std::string type, _SlotType createFunction) {
00102 log_debug("Registering function for type <%s>.", type.c_str());
00103 if(m_createFunctions.find(type) != m_createFunctions.end()) {
00104 log_warn("Function for type <%s> already registered, ignoring.", type.c_str());
00105 return false;
00106 }
00107
00108 log_debug("Adding plugin for <%s> node...", type.c_str());
00109 m_createFunctions[type].connect(createFunction);
00110 log_debug("...done");
00111 }
00112
00116 static PluginFactory<_ParentType, _SigType, _SlotType> * get_instance() {
00117 static PluginFactory<_ParentType, _SigType, _SlotType> * instance;
00118 if(instance == NULL) {
00119 log_debug("Creating new factory");
00120 instance = new PluginFactory<_ParentType, _SigType, _SlotType>();
00121 }
00122 return instance;
00123 }
00124
00125 };
00126
00127 typedef PluginFactory<Material, material_create_fn, material_create_slot> MaterialFactory;
00128 typedef PluginFactory<Renderable, primitive_create_fn, primitive_create_slot> RenderableFactory;
00129 typedef PluginFactory<BumpMap, bumpmap_create_fn, bumpmap_create_slot> BumpMapFactory;
00130
00131 #endif
00132
00133
00134
00135
00136