2014-06-04 14:26:44 +08:00
|
|
|
/****************************************************************************
|
2017-02-14 14:36:57 +08:00
|
|
|
Copyright (c) 2013-2017 Chukong Technologies Inc.
|
2014-06-04 14:26:44 +08:00
|
|
|
|
|
|
|
http://www.cocos2d-x.org
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-08-12 17:19:52 +08:00
|
|
|
#include <functional>
|
2014-08-29 15:39:52 +08:00
|
|
|
#include "base/ObjectFactory.h"
|
2014-06-04 14:26:44 +08:00
|
|
|
|
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
|
|
|
ObjectFactory::TInfo::TInfo(void)
|
|
|
|
:_class("")
|
|
|
|
,_fun(nullptr)
|
2014-08-12 17:19:52 +08:00
|
|
|
,_func(nullptr)
|
2014-06-04 14:26:44 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ObjectFactory::TInfo::TInfo(const std::string& type, Instance ins)
|
|
|
|
:_class(type)
|
|
|
|
,_fun(ins)
|
2014-08-12 17:19:52 +08:00
|
|
|
,_func(nullptr)
|
|
|
|
{
|
|
|
|
ObjectFactory::getInstance()->registerType(*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
ObjectFactory::TInfo::TInfo(const std::string& type, InstanceFunc ins)
|
|
|
|
:_class(type)
|
|
|
|
,_fun(nullptr)
|
|
|
|
,_func(ins)
|
2014-06-04 14:26:44 +08:00
|
|
|
{
|
|
|
|
ObjectFactory::getInstance()->registerType(*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
ObjectFactory::TInfo::TInfo(const TInfo &t)
|
|
|
|
{
|
|
|
|
_class = t._class;
|
|
|
|
_fun = t._fun;
|
2014-08-12 17:19:52 +08:00
|
|
|
_func = t._func;
|
2014-06-04 14:26:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ObjectFactory::TInfo::~TInfo(void)
|
|
|
|
{
|
|
|
|
_class = "";
|
|
|
|
_fun = nullptr;
|
2014-08-12 17:19:52 +08:00
|
|
|
_func = nullptr;
|
2014-06-04 14:26:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ObjectFactory::TInfo& ObjectFactory::TInfo::operator= (const TInfo &t)
|
|
|
|
{
|
|
|
|
_class = t._class;
|
|
|
|
_fun = t._fun;
|
2014-08-12 17:19:52 +08:00
|
|
|
_func = t._func;
|
2014-06-04 14:26:44 +08:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ObjectFactory* ObjectFactory::_sharedFactory = nullptr;
|
|
|
|
|
|
|
|
ObjectFactory::ObjectFactory(void)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
ObjectFactory::~ObjectFactory(void)
|
|
|
|
{
|
|
|
|
_typeMap.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
ObjectFactory* ObjectFactory::getInstance()
|
|
|
|
{
|
|
|
|
if ( nullptr == _sharedFactory)
|
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
_sharedFactory = new (std::nothrow) ObjectFactory();
|
2014-06-04 14:26:44 +08:00
|
|
|
}
|
|
|
|
return _sharedFactory;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ObjectFactory::destroyInstance()
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(_sharedFactory);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ref* ObjectFactory::createObject(const std::string &name)
|
|
|
|
{
|
2014-08-21 02:15:27 +08:00
|
|
|
Ref *o = nullptr;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
const TInfo t = _typeMap[name];
|
2014-08-12 17:19:52 +08:00
|
|
|
if (t._fun != nullptr)
|
|
|
|
{
|
|
|
|
o = t._fun();
|
|
|
|
}else if (t._func != nullptr)
|
|
|
|
{
|
|
|
|
o = t._func();
|
|
|
|
}
|
2014-08-21 02:15:27 +08:00
|
|
|
} while (0);
|
2014-06-04 14:26:44 +08:00
|
|
|
|
|
|
|
return o;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ObjectFactory::registerType(const TInfo &t)
|
|
|
|
{
|
2016-11-08 11:50:00 +08:00
|
|
|
_typeMap.emplace(t._class, t);
|
2014-06-04 14:26:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_CC_END
|