1.
<root> <ScriptValueStr Key="Test" Value="深南向日葵"/> <ScriptValueStr Key="Message1" Value="深南向日葵1"/> <ScriptValueStr Key="Message2" Value="深南向日葵2"/> <ScriptValueStr Key="Message3" Value="深南向日葵3"/> </root>
2..h
#pragma once #include "cocos2d.h" #include "StructType.h" class ScriptDataManager { public: ScriptDataManager(void); ScriptDataManager(std::string kFileName); ~ScriptDataManager(void); public: static ScriptDataManager* getInterface(); static ScriptDataManager* s_pInterface; void clear(); public: template<class T> static std::map<std::string,T>& S_LIST(); template<class T> static void addValue(std::string kKey,const T& iValue) { std::map<std::string,T>& kList = S_LIST<T>(); typename std::map<std::string,T>::iterator itor = kList.find(kKey); if(itor != kList.end()) { cocos2d::log("have already Value %s",kKey.c_str()); return; } kList.insert(std::pair<std::string,T>(kKey,iValue)); } protected: std::map<std::string,int> m_mapValueInt; std::map<std::string,float> m_mapValueFloat; std::map<std::string,std::string> m_mapValueStr; std::map<std::string,cocos2d::Vec2> m_mapValuePoint; std::map<std::string,cocos2d::Size> m_mapValueSize; }; template<class T> class ScriptData { public: ScriptData(std::string pcName) { std::map<std::string,T>& kList =ScriptDataManager::S_LIST<T>(); typename std::map<std::string,T>::const_iterator iter = kList.find(pcName); if(iter != kList.end()) { m_kValue = (*iter).second; } else { std::string kAssert = std::string(pcName) + " Can't Find ScriptConstValue"; CCAssert(false,kAssert.c_str()); } } const T& Value()const{return m_kValue;} private: T m_kValue; };
3..cpp
#include "ScriptData.h" #include "ScriptXMLparse.h" ScriptDataManager* ScriptDataManager::s_pInterface = NULL; ScriptDataManager::ScriptDataManager(void) { s_pInterface = this; } ScriptDataManager::ScriptDataManager(std::string kFileName) { s_pInterface = this; cocos2d::ScriptXMLparse kXml1(kFileName); } ScriptDataManager::~ScriptDataManager(void) { } ScriptDataManager* ScriptDataManager::getInterface() { if(s_pInterface == NULL) { s_pInterface = new ScriptDataManager(); } return s_pInterface; } void ScriptDataManager::clear() { m_mapValueInt.clear(); m_mapValueFloat.clear(); m_mapValueStr.clear(); m_mapValuePoint.clear(); m_mapValueSize.clear(); } template<> std::map<std::string,float>& ScriptDataManager::S_LIST<float>() { return getInterface()->m_mapValueFloat; } template<> std::map<std::string,int>& ScriptDataManager::S_LIST<int>() { return getInterface()->m_mapValueInt; } template<> std::map<std::string,std::string>& ScriptDataManager::S_LIST<std::string>() { return getInterface()->m_mapValueStr; } template<> std::map<std::string,cocos2d::Vec2>& ScriptDataManager::S_LIST<cocos2d::Vec2>() { return getInterface()->m_mapValuePoint; } template<> std::map<std::string,cocos2d::Size>& ScriptDataManager::S_LIST<cocos2d::Size>() { return getInterface()->m_mapValueSize; }
4.
ScriptData<string>("Message3").Value(); (编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|