RUDelegateFactory.cpp 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. #include "RUDelegateFactory.h"
  2. NS_RU_BEGIN
  3. static DelegateFactory *_instance = nullptr;
  4. DelegateFactory* DelegateFactory::getInstance()
  5. {
  6. if (_instance == nullptr)
  7. {
  8. _instance = new (std::nothrow) DelegateFactory;
  9. }
  10. return _instance;
  11. }
  12. void DelegateFactory::registerStateDelegateCreator(const std::string& creatorKey, std::function<StateDelegate*(void)> stateDelegateCreator)
  13. {
  14. CCASSERT(_stateDelegateCreators.find(creatorKey) == _stateDelegateCreators.end(), "wtf");
  15. _stateDelegateCreators.emplace(creatorKey,stateDelegateCreator);
  16. }
  17. StateDelegate* DelegateFactory::createStateDelegate(const std::string& creatorKey)
  18. {
  19. StateDelegate* stateDelegate = nullptr;
  20. //如果注册器的key未注册,返回默认注册器
  21. if (creatorKey.empty() || _stateDelegateCreators.find(creatorKey) == _stateDelegateCreators.end()) {
  22. stateDelegate = new StateDelegate();
  23. }else{
  24. auto stateDelegateCreator = _stateDelegateCreators.find(creatorKey)->second;
  25. stateDelegate = stateDelegateCreator();
  26. }
  27. return stateDelegate;
  28. }
  29. NS_RU_END