From: <axl...@us...> - 2008-12-03 11:51:00
|
Revision: 29 http://hgengine.svn.sourceforge.net/hgengine/?rev=29&view=rev Author: axlecrusher Date: 2008-12-03 11:46:22 +0000 (Wed, 03 Dec 2008) Log Message: ----------- Add a node factory and a function to load from XML Modified Paths: -------------- Mercury2/src/MercuryNode.cpp Mercury2/src/MercuryNode.h Modified: Mercury2/src/MercuryNode.cpp =================================================================== --- Mercury2/src/MercuryNode.cpp 2008-12-03 11:43:29 UTC (rev 28) +++ Mercury2/src/MercuryNode.cpp 2008-12-03 11:46:22 UTC (rev 29) @@ -3,6 +3,8 @@ using namespace std; +REGISTER_NODE_TYPE(MercuryNode); + MercuryNode::MercuryNode() :m_parent(NULL) { @@ -88,6 +90,47 @@ (*i)->RecursiveUpdate(dTime); } +void MercuryNode::LoadFromXML(const XMLNode& node) +{ + //Not much to do here except run through all the children nodes + for (XMLNode child = node.Child(); child.IsValid(); child = child.NextNode()) + { + if ( child.Name() == "node" ) + { + MercuryNode* node = NodeFactory::GetInstance().Generate( child.Attribute("type") ); + if (!node) node = new MercuryNode(); + node->LoadFromXML( child ); + this->AddChild( node ); + } + } +} + +NodeFactory& NodeFactory::GetInstance() +{ + static NodeFactory* instance = NULL; + if (!instance) + instance = new NodeFactory; + return *instance; + +} + +bool NodeFactory::RegisterFactoryCallback(const std::string& type, Callback0R<MercuryNode*> functor) +{ + string t = ToUpper( type ); + std::pair<std::string, Callback0R<MercuryNode*> > pp(t, functor); + m_factoryCallbacks.push_back( pp ); + return true; +} + +MercuryNode* NodeFactory::Generate(const std::string& type) +{ + string t = ToUpper( type ); + std::list< std::pair< std::string, Callback0R<MercuryNode*> > >::iterator i; + for (i = m_factoryCallbacks.begin(); i != m_factoryCallbacks.end(); ++i) + if (i->first == t) return i->second(); + return NULL; +} + /*************************************************************************** * Copyright (C) 2008 by Joshua Allen * * * Modified: Mercury2/src/MercuryNode.h =================================================================== --- Mercury2/src/MercuryNode.h 2008-12-03 11:43:29 UTC (rev 28) +++ Mercury2/src/MercuryNode.h 2008-12-03 11:46:22 UTC (rev 29) @@ -4,6 +4,7 @@ #include <list> #include <Callback.h> #include <typeinfo> +#include <XMLParser.h> /** This is the basic node of the scene graph. It is not intended to be instanced. Each node exists as a single entity in the scene graph. @@ -44,14 +45,35 @@ ///Provides callback ability when a child node is removed (parent, child) arguement order std::list< Callback2< MercuryNode*, MercuryNode* > > OnRemoveChild; + + ///Loads a node from an XMLNode representing itself + virtual void LoadFromXML(const XMLNode& node); GENRTTI(MercuryNode); - + protected: std::list< MercuryNode* > m_children; //These nodes are unique, not instanced MercuryNode* m_parent; }; +class NodeFactory +{ + public: + static NodeFactory& GetInstance(); + bool RegisterFactoryCallback(const std::string& type, Callback0R<MercuryNode*>); + MercuryNode* Generate(const std::string& type); + + private: + std::list< std::pair< std::string, Callback0R<MercuryNode*> > > m_factoryCallbacks; +}; + +static InstanceCounter<NodeFactory> NFcounter("NodeFactory"); + +#define REGISTER_NODE_TYPE(class)\ + MercuryNode* FactoryFunct##class() { return new class(); } \ + Callback0R<MercuryNode*> factoryclbk( FactoryFunct##class ); \ + bool GlobalRegisterSuccess##class = NodeFactory::GetInstance().RegisterFactoryCallback("#class", factoryclbk); + #endif /*************************************************************************** This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |