Skip to content

Commit

Permalink
refactor ObjectFactory
Browse files Browse the repository at this point in the history
* remove redundant copy constructor, copy assignment operator, and
destructor in TInfo.
* prefer in-class member initialization.
* default construct std::function, and use `default`
* fix overload resolution
* return early in multiple conditionals
* remove unimplemented removeAll()
* remove typedef that's only used in one place and in private scope
  • Loading branch information
JohnCoconut committed Jul 5, 2019
1 parent 4107942 commit cffee2e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 75 deletions.
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;
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

0 comments on commit cffee2e

Please sign in to comment.