Skip to content

Commit 8201b58

Browse files
committed
refactor ObjectFactory
* 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 * implement removeAll() * remove typedef that's only used in one place and in private scope
1 parent 4107942 commit 8201b58

File tree

2 files changed

+26
-74
lines changed

2 files changed

+26
-74
lines changed

cocos/base/ObjectFactory.cpp

Lines changed: 15 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -24,70 +24,27 @@ THE SOFTWARE.
2424
****************************************************************************/
2525

2626
#include "base/ObjectFactory.h"
27-
#include <functional>
2827
#include <utility>
2928

3029

3130
NS_CC_BEGIN
3231

33-
ObjectFactory::TInfo::TInfo()
34-
:_class("")
35-
,_fun(nullptr)
36-
,_func(nullptr)
37-
{
38-
}
39-
4032
ObjectFactory::TInfo::TInfo(const std::string& type, Instance ins)
4133
:_class(type)
4234
,_fun(ins)
43-
,_func(nullptr)
4435
{
4536
ObjectFactory::getInstance()->registerType(*this);
4637
}
4738

4839
ObjectFactory::TInfo::TInfo(const std::string& type, const InstanceFunc& ins)
49-
:_class(type)
50-
,_fun(nullptr)
51-
,_func(ins)
40+
:_class(type)
41+
,_func(ins)
5242
{
5343
ObjectFactory::getInstance()->registerType(*this);
5444
}
5545

56-
ObjectFactory::TInfo::TInfo(const TInfo &t)
57-
{
58-
_class = t._class;
59-
_fun = t._fun;
60-
_func = t._func;
61-
}
62-
63-
ObjectFactory::TInfo::~TInfo()
64-
{
65-
_class = "";
66-
_fun = nullptr;
67-
_func = nullptr;
68-
}
69-
70-
ObjectFactory::TInfo& ObjectFactory::TInfo::operator= (const TInfo &t)
71-
{
72-
_class = t._class;
73-
_fun = t._fun;
74-
_func = t._func;
75-
return *this;
76-
}
77-
78-
7946
ObjectFactory* ObjectFactory::_sharedFactory = nullptr;
8047

81-
ObjectFactory::ObjectFactory()
82-
{
83-
84-
}
85-
86-
ObjectFactory::~ObjectFactory()
87-
{
88-
_typeMap.clear();
89-
}
90-
9148
ObjectFactory* ObjectFactory::getInstance()
9249
{
9350
if ( nullptr == _sharedFactory)
@@ -104,25 +61,24 @@ void ObjectFactory::destroyInstance()
10461

10562
Ref* ObjectFactory::createObject(const std::string &name)
10663
{
107-
Ref *o = nullptr;
108-
do
109-
{
110-
const TInfo t = _typeMap[name];
111-
if (t._fun != nullptr)
112-
{
113-
o = t._fun();
114-
}else if (t._func != nullptr)
115-
{
116-
o = t._func();
117-
}
118-
} while (0);
119-
120-
return o;
64+
const TInfo t = _typeMap[name];
65+
if (t._fun != nullptr) {
66+
return t._fun();
67+
}
68+
if (t._func != nullptr) {
69+
return t._func();
70+
}
71+
return nullptr;
12172
}
12273

12374
void ObjectFactory::registerType(const TInfo &t)
12475
{
12576
_typeMap.emplace(t._class, t);
12677
}
12778

79+
void ObjectFactory::removeAll()
80+
{
81+
_typeMap.clear();
82+
}
83+
12884
NS_CC_END

cocos/base/ObjectFactory.h

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@ THE SOFTWARE.
2626
#ifndef __TRIGGERFACTORY_H__
2727
#define __TRIGGERFACTORY_H__
2828

29-
#include <string>
30-
#include <unordered_map>
31-
#include <functional>
3229
#include "base/CCRef.h"
3330
#include "platform/CCPlatformMacros.h"
31+
#include <functional>
32+
#include <string>
33+
#include <unordered_map>
3434

3535
NS_CC_BEGIN
3636

@@ -41,31 +41,27 @@ class CC_DLL ObjectFactory
4141
typedef std::function<cocos2d::Ref* ()> InstanceFunc;
4242
struct CC_DLL TInfo
4343
{
44-
TInfo();
45-
TInfo(const std::string& type, Instance ins = nullptr);
46-
TInfo(const std::string& type, const InstanceFunc& ins = nullptr);
47-
TInfo(const TInfo &t);
48-
~TInfo();
49-
TInfo& operator= (const TInfo &t);
44+
TInfo() = default;
45+
TInfo(const std::string& type, Instance ins);
46+
TInfo(const std::string& type, const InstanceFunc& ins);
5047
std::string _class;
51-
Instance _fun;
48+
Instance _fun = nullptr;
5249
InstanceFunc _func;
5350
};
54-
typedef std::unordered_map<std::string, TInfo> FactoryMap;
5551

5652
static ObjectFactory* getInstance();
5753
static void destroyInstance();
58-
cocos2d::Ref* createObject(const std::string &name);
5954

55+
cocos2d::Ref* createObject(const std::string &name);
6056
void registerType(const TInfo &t);
6157
void removeAll();
6258

6359
protected:
64-
ObjectFactory();
65-
virtual ~ObjectFactory();
60+
ObjectFactory() = default;
61+
virtual ~ObjectFactory() = default;
6662
private:
6763
static ObjectFactory *_sharedFactory;
68-
FactoryMap _typeMap;
64+
std::unordered_map<std::string, TInfo> _typeMap;
6965
};
7066

7167
NS_CC_END

0 commit comments

Comments
 (0)