add GUIReader::registerTypeAndCallBack for std::function

This commit is contained in:
lite3 2014-08-12 17:19:52 +08:00
parent 69e2b815ed
commit 8f59d8ac93
4 changed files with 50 additions and 2 deletions

View File

@ -23,6 +23,7 @@ THE SOFTWARE.
****************************************************************************/
#include "ObjectFactory.h"
#include <functional>
NS_CC_BEGIN
@ -30,12 +31,22 @@ NS_CC_BEGIN
ObjectFactory::TInfo::TInfo(void)
:_class("")
,_fun(nullptr)
,_func(nullptr)
{
}
ObjectFactory::TInfo::TInfo(const std::string& type, Instance ins)
:_class(type)
,_fun(ins)
,_func(nullptr)
{
ObjectFactory::getInstance()->registerType(*this);
}
ObjectFactory::TInfo::TInfo(const std::string& type, InstanceFunc ins)
:_class(type)
,_fun(nullptr)
,_func(ins)
{
ObjectFactory::getInstance()->registerType(*this);
}
@ -44,18 +55,21 @@ ObjectFactory::TInfo::TInfo(const TInfo &t)
{
_class = t._class;
_fun = t._fun;
_func = t._func;
}
ObjectFactory::TInfo::~TInfo(void)
{
_class = "";
_fun = nullptr;
_func = nullptr;
}
ObjectFactory::TInfo& ObjectFactory::TInfo::operator= (const TInfo &t)
{
_class = t._class;
_fun = t._fun;
_func = t._func;
return *this;
}
@ -92,8 +106,13 @@ Ref* ObjectFactory::createObject(const std::string &name)
do
{
const TInfo t = _typeMap[name];
CC_BREAK_IF(t._fun == nullptr);
o = t._fun();
if (t._fun != nullptr)
{
o = t._fun();
}else if (t._func != nullptr)
{
o = t._func();
}
} while (0);
return o;

View File

@ -36,15 +36,18 @@ class CC_DLL ObjectFactory
{
public:
typedef cocos2d::Ref* (*Instance)(void);
typedef std::function<cocos2d::Ref* (void)> InstanceFunc;
struct CC_DLL TInfo
{
TInfo(void);
TInfo(const std::string& type, Instance ins = NULL);
TInfo(const std::string& type, InstanceFunc ins = NULL);
TInfo(const TInfo &t);
~TInfo(void);
TInfo& operator= (const TInfo &t);
std::string _class;
Instance _fun;
InstanceFunc _func;
};
typedef std::unordered_map<std::string, TInfo> FactoryMap;

View File

@ -177,6 +177,27 @@ void GUIReader::registerTypeAndCallBack(const std::string& classType,
}
}
void GUIReader::registerTypeAndCallBack(const std::string& classType,
ObjectFactory::InstanceFunc ins,
Ref *object,
SEL_ParseEvent callBack)
{
ObjectFactory* factoryCreate = ObjectFactory::getInstance();
ObjectFactory::TInfo t(classType, ins);
factoryCreate->registerType(t);
if (object)
{
_mapObject.insert(ParseObjectMap::value_type(classType, object));
}
if (callBack)
{
_mapParseSelector.insert(ParseCallBackMap::value_type(classType, callBack));
}
}
Widget* GUIReader::widgetFromJsonFile(const char *fileName)
{

View File

@ -72,6 +72,11 @@ public:
cocos2d::ObjectFactory::Instance ins,
Ref* object,
SEL_ParseEvent callBack);
void registerTypeAndCallBack(const std::string& classType,
cocos2d::ObjectFactory::InstanceFunc ins,
Ref* object,
SEL_ParseEvent callBack);
protected:
GUIReader();
~GUIReader();