Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor ObjectFactory #19902

Merged
merged 1 commit into from
Jul 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 10 additions & 59 deletions cocos/base/ObjectFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,70 +24,27 @@ THE SOFTWARE.
****************************************************************************/

#include "base/ObjectFactory.h"
#include <functional>
#include <utility>


NS_CC_BEGIN

ObjectFactory::TInfo::TInfo()
:_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, const InstanceFunc& ins)
:_class(type)
,_fun(nullptr)
,_func(ins)
:_class(type)
,_func(ins)
{
ObjectFactory::getInstance()->registerType(*this);
}

ObjectFactory::TInfo::TInfo(const TInfo &t)
{
_class = t._class;
_fun = t._fun;
_func = t._func;
}

ObjectFactory::TInfo::~TInfo()
{
_class = "";
_fun = nullptr;
_func = nullptr;
}

ObjectFactory::TInfo& ObjectFactory::TInfo::operator= (const TInfo &t)
{
_class = t._class;
_fun = t._fun;
_func = t._func;
return *this;
}


ObjectFactory* ObjectFactory::_sharedFactory = nullptr;

ObjectFactory::ObjectFactory()
{

}

ObjectFactory::~ObjectFactory()
{
_typeMap.clear();
}

ObjectFactory* ObjectFactory::getInstance()
{
if ( nullptr == _sharedFactory)
Expand All @@ -104,20 +61,14 @@ void ObjectFactory::destroyInstance()

Ref* ObjectFactory::createObject(const std::string &name)
{
Ref *o = nullptr;
do
{
const TInfo t = _typeMap[name];
if (t._fun != nullptr)
{
o = t._fun();
}else if (t._func != nullptr)
{
o = t._func();
}
} while (0);

return o;
const TInfo t = _typeMap[name];
if (t._fun != nullptr) {
return t._fun();
}
if (t._func != nullptr) {
return t._func();
}
return nullptr;
}

void ObjectFactory::registerType(const TInfo &t)
Expand Down
27 changes: 11 additions & 16 deletions cocos/base/ObjectFactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ THE SOFTWARE.
#ifndef __TRIGGERFACTORY_H__
#define __TRIGGERFACTORY_H__

#include <string>
#include <unordered_map>
#include <functional>
#include "base/CCRef.h"
#include "platform/CCPlatformMacros.h"
#include <functional>
#include <string>
#include <unordered_map>

NS_CC_BEGIN

Expand All @@ -41,31 +41,26 @@ class CC_DLL ObjectFactory
typedef std::function<cocos2d::Ref* ()> InstanceFunc;
struct CC_DLL TInfo
{
TInfo();
TInfo(const std::string& type, Instance ins = nullptr);
TInfo(const std::string& type, const InstanceFunc& ins = nullptr);
TInfo(const TInfo &t);
~TInfo();
TInfo& operator= (const TInfo &t);
TInfo() = default;
TInfo(const std::string& type, Instance ins);
TInfo(const std::string& type, const InstanceFunc& ins);
std::string _class;
Instance _fun;
Instance _fun = nullptr;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this class has both c pointer function and std::function? std::function should be enough.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_fun is used in lua bindings. I am not sure how it works.

ccs.TInfo = class("TInfo")
ccs.TInfo._className = ""
ccs.TInfo._fun = nil
function ccs.TInfo:ctor(c,f)
-- @param {String|ccs.TInfo}c
-- @param {Function}f
if nil ~= f then
self._className = c
self._fun = f
else
self._className = c._className
self._fun = c._fun
end

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that link cocos2d-x/cocos/scripting/lua-bindings/script/cocostudio/CocoStudio.lua is just the same implementation but in Lua.
So we can remove _fun from C++ and have only std::function

InstanceFunc _func;
};
typedef std::unordered_map<std::string, TInfo> FactoryMap;

static ObjectFactory* getInstance();
static void destroyInstance();
cocos2d::Ref* createObject(const std::string &name);

cocos2d::Ref* createObject(const std::string &name);
void registerType(const TInfo &t);
void removeAll();

protected:
ObjectFactory();
virtual ~ObjectFactory();
ObjectFactory() = default;
virtual ~ObjectFactory() = default;
private:
static ObjectFactory *_sharedFactory;
FactoryMap _typeMap;
std::unordered_map<std::string, TInfo> _typeMap;
};

NS_CC_END
Expand Down