12345678910111213141516171819202122232425262728293031323334353637 |
- #include "RUDelegateFactory.h"
- NS_RU_BEGIN
- static DelegateFactory *_instance = nullptr;
- DelegateFactory* DelegateFactory::getInstance()
- {
- if (_instance == nullptr)
- {
- _instance = new (std::nothrow) DelegateFactory;
- }
- return _instance;
- }
- void DelegateFactory::registerStateDelegateCreator(const std::string& creatorKey, std::function<StateDelegate*(void)> stateDelegateCreator)
- {
- CCASSERT(_stateDelegateCreators.find(creatorKey) == _stateDelegateCreators.end(), "wtf");
- _stateDelegateCreators.emplace(creatorKey,stateDelegateCreator);
- }
- StateDelegate* DelegateFactory::createStateDelegate(const std::string& creatorKey)
- {
- StateDelegate* stateDelegate = nullptr;
-
- //如果注册器的key未注册,返回默认注册器
- if (creatorKey.empty() || _stateDelegateCreators.find(creatorKey) == _stateDelegateCreators.end()) {
- stateDelegate = new StateDelegate();
- }else{
- auto stateDelegateCreator = _stateDelegateCreators.find(creatorKey)->second;
- stateDelegate = stateDelegateCreator();
- }
- return stateDelegate;
- }
- NS_RU_END
|