2014-06-04 14:26:44 +08:00
|
|
|
/****************************************************************************
|
2018-01-29 16:25:32 +08:00
|
|
|
Copyright (c) 2013-2016 Chukong Technologies Inc.
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
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
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
ObjectFactory::TInfo::TInfo() : _class(""), _fun(nullptr), _func(nullptr) {}
|
2014-06-04 14:26:44 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
ObjectFactory::TInfo::TInfo(const std::string& type, Instance ins) : _class(type), _fun(ins), _func(nullptr)
|
2014-08-12 17:19:52 +08:00
|
|
|
{
|
|
|
|
ObjectFactory::getInstance()->registerType(*this);
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
ObjectFactory::TInfo::TInfo(const TInfo& t)
|
2014-06-04 14:26:44 +08:00
|
|
|
{
|
|
|
|
_class = t._class;
|
2021-12-25 10:04:45 +08:00
|
|
|
_fun = t._fun;
|
|
|
|
_func = t._func;
|
2014-06-04 14:26:44 +08:00
|
|
|
}
|
|
|
|
|
2019-07-22 09:38:46 +08:00
|
|
|
ObjectFactory::TInfo::~TInfo()
|
2014-06-04 14:26:44 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
_class = "";
|
|
|
|
_fun = nullptr;
|
|
|
|
_func = nullptr;
|
2014-06-04 14:26:44 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
ObjectFactory::TInfo& ObjectFactory::TInfo::operator=(const TInfo& t)
|
2014-06-04 14:26:44 +08:00
|
|
|
{
|
|
|
|
_class = t._class;
|
2021-12-25 10:04:45 +08:00
|
|
|
_fun = t._fun;
|
|
|
|
_func = t._func;
|
2014-06-04 14:26:44 +08:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
ObjectFactory* ObjectFactory::_sharedFactory = nullptr;
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
ObjectFactory::ObjectFactory() {}
|
2014-06-04 14:26:44 +08:00
|
|
|
|
2019-07-22 09:38:46 +08:00
|
|
|
ObjectFactory::~ObjectFactory()
|
2014-06-04 14:26:44 +08:00
|
|
|
{
|
|
|
|
_typeMap.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
ObjectFactory* ObjectFactory::getInstance()
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (nullptr == _sharedFactory)
|
2014-06-04 14:26:44 +08:00
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
_sharedFactory = new ObjectFactory();
|
2014-06-04 14:26:44 +08:00
|
|
|
}
|
|
|
|
return _sharedFactory;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ObjectFactory::destroyInstance()
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(_sharedFactory);
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
Ref* ObjectFactory::createObject(const std::string& name)
|
2014-06-04 14:26:44 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
Ref* o = nullptr;
|
|
|
|
do
|
2014-08-21 02:15:27 +08:00
|
|
|
{
|
|
|
|
const TInfo t = _typeMap[name];
|
2014-08-12 17:19:52 +08:00
|
|
|
if (t._fun != nullptr)
|
|
|
|
{
|
|
|
|
o = t._fun();
|
2021-12-25 10:04:45 +08:00
|
|
|
}
|
|
|
|
else if (t._func != nullptr)
|
2014-08-12 17:19:52 +08:00
|
|
|
{
|
|
|
|
o = t._func();
|
|
|
|
}
|
2014-08-21 02:15:27 +08:00
|
|
|
} while (0);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2014-06-04 14:26:44 +08:00
|
|
|
return o;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void ObjectFactory::registerType(const TInfo& t)
|
2014-06-04 14:26:44 +08:00
|
|
|
{
|
2016-11-08 11:50:00 +08:00
|
|
|
_typeMap.emplace(t._class, t);
|
2014-06-04 14:26:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_CC_END
|