ICommandManager.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #ifndef CRYINCLUDE_EDITOR_INCLUDE_ICOMMANDMANAGER_H
  9. #define CRYINCLUDE_EDITOR_INCLUDE_ICOMMANDMANAGER_H
  10. #pragma once
  11. #include "Command.h"
  12. typedef void (* TPfnDeleter)(void*);
  13. class ICommandManager
  14. {
  15. public:
  16. virtual ~ICommandManager() = default;
  17. virtual bool AddCommand(CCommand* pCommand, TPfnDeleter deleter = nullptr) = 0;
  18. virtual bool UnregisterCommand(const char* module, const char* name) = 0;
  19. virtual bool AttachUIInfo(const char* fullCmdName, const CCommand0::SUIInfo& uiInfo) = 0;
  20. virtual bool IsRegistered(const char* module, const char* name) const = 0;
  21. virtual bool IsRegistered(const char* cmdLine) const = 0;
  22. virtual bool IsRegistered(int commandId) const = 0;
  23. };
  24. /// A set of helper template methods for an easy registration of commands
  25. namespace CommandManagerHelper
  26. {
  27. bool RegisterCommand(ICommandManager* pCmdMgr, const char* module, const char* name,
  28. const char* description, const char* example,
  29. const AZStd::function<void()>& functor);
  30. template <typename RT>
  31. bool RegisterCommand(ICommandManager* pCmdMgr, const char* module, const char* name,
  32. const char* description, const char* example,
  33. const AZStd::function<RT()>& functor);
  34. template <LIST(1, typename P)>
  35. bool RegisterCommand(ICommandManager* pCmdMgr, const char* module, const char* name,
  36. const char* description, const char* example,
  37. const AZStd::function<void(LIST(1, P))>& functor);
  38. template <LIST(1, typename P), typename RT>
  39. bool RegisterCommand(ICommandManager* pCmdMgr, const char* module, const char* name,
  40. const char* description, const char* example,
  41. const AZStd::function<RT(LIST(1, P))>& functor);
  42. template <LIST(2, typename P)>
  43. bool RegisterCommand(ICommandManager* pCmdMgr, const char* module, const char* name,
  44. const char* description, const char* example,
  45. const AZStd::function<void(LIST(2, P))>& functor);
  46. template <LIST(2, typename P), typename RT>
  47. bool RegisterCommand(ICommandManager* pCmdMgr, const char* module, const char* name,
  48. const char* description, const char* example,
  49. const AZStd::function<RT(LIST(2, P))>& functor);
  50. template <LIST(3, typename P)>
  51. bool RegisterCommand(ICommandManager* pCmdMgr, const char* module, const char* name,
  52. const char* description, const char* example,
  53. const AZStd::function<void(LIST(3, P))>& functor);
  54. template <LIST(3, typename P), typename RT>
  55. bool RegisterCommand(ICommandManager* pCmdMgr, const char* module, const char* name,
  56. const char* description, const char* example,
  57. const AZStd::function<RT(LIST(3, P))>& functor);
  58. template <LIST(4, typename P)>
  59. bool RegisterCommand(ICommandManager* pCmdMgr, const char* module, const char* name,
  60. const char* description, const char* example,
  61. const AZStd::function<void(LIST(4, P))>& functor);
  62. template <LIST(4, typename P), typename RT>
  63. bool RegisterCommand(ICommandManager* pCmdMgr, const char* module, const char* name,
  64. const char* description, const char* example,
  65. const AZStd::function<RT(LIST(4, P))>& functor);
  66. template <LIST(5, typename P)>
  67. bool RegisterCommand(ICommandManager* pCmdMgr, const char* module, const char* name,
  68. const char* description, const char* example,
  69. const AZStd::function<void(LIST(5, P))>& functor);
  70. template <LIST(6, typename P)>
  71. bool RegisterCommand(ICommandManager* pCmdMgr, const char* module, const char* name,
  72. const char* description, const char* example,
  73. const AZStd::function<void(LIST(6, P))>& functor);
  74. namespace Private
  75. {
  76. template <typename FunctorType, typename CommandType>
  77. bool RegisterCommand(ICommandManager* pCmdMgr, const char* module, const char* name,
  78. const char* description, const char* example,
  79. const FunctorType& functor);
  80. }
  81. };
  82. template <typename FunctorType, typename CommandType>
  83. bool CommandManagerHelper::Private::RegisterCommand(ICommandManager* pCmdMgr,
  84. const char* module, const char* name,
  85. const char* description, const char* example,
  86. const FunctorType& functor)
  87. {
  88. assert(functor);
  89. if (!functor)
  90. {
  91. return false;
  92. }
  93. CommandType* pCommand
  94. = new CommandType(module, name, description, example, functor);
  95. if (pCmdMgr->AddCommand(pCommand) == false)
  96. {
  97. delete pCommand;
  98. return false;
  99. }
  100. return true;
  101. }
  102. inline
  103. bool CommandManagerHelper::RegisterCommand(ICommandManager* pCmdMgr, const char* module, const char* name,
  104. const char* description, const char* example,
  105. const AZStd::function<void()>& functor)
  106. {
  107. return Private::RegisterCommand<AZStd::function<void()>, CCommand0>(pCmdMgr, module, name, description, example, functor);
  108. }
  109. template <typename RT>
  110. bool CommandManagerHelper::RegisterCommand(ICommandManager* pCmdMgr, const char* module, const char* name,
  111. const char* description, const char* example,
  112. const AZStd::function<RT()>& functor)
  113. {
  114. return Private::RegisterCommand<AZStd::function<RT()>, CCommand0wRet<RT> >(pCmdMgr, module, name, description, example, functor);
  115. }
  116. template <LIST(1, typename P)>
  117. bool CommandManagerHelper::RegisterCommand(ICommandManager* pCmdMgr, const char* module, const char* name,
  118. const char* description, const char* example,
  119. const AZStd::function<void(LIST(1, P))>& functor)
  120. {
  121. return Private::RegisterCommand<AZStd::function<void(LIST(1, P))>, CCommand1<LIST(1, P)> >(pCmdMgr, module, name, description, example, functor);
  122. }
  123. template <LIST(1, typename P), typename RT>
  124. bool CommandManagerHelper::RegisterCommand(ICommandManager* pCmdMgr, const char* module, const char* name,
  125. const char* description, const char* example,
  126. const AZStd::function<RT(LIST(1, P))>& functor)
  127. {
  128. return Private::RegisterCommand<AZStd::function<RT(LIST(1, P))>, CCommand1wRet<LIST(1, P), RT> >(pCmdMgr, module, name, description, example, functor);
  129. }
  130. template <LIST(2, typename P)>
  131. bool CommandManagerHelper::RegisterCommand(ICommandManager* pCmdMgr, const char* module, const char* name,
  132. const char* description, const char* example,
  133. const AZStd::function<void(LIST(2, P))>& functor)
  134. {
  135. return Private::RegisterCommand<AZStd::function<void(LIST(2, P))>, CCommand2<LIST(2, P)> >(pCmdMgr, module, name, description, example, functor);
  136. }
  137. template <LIST(2, typename P), typename RT>
  138. bool CommandManagerHelper::RegisterCommand(ICommandManager* pCmdMgr, const char* module, const char* name,
  139. const char* description, const char* example,
  140. const AZStd::function<RT(LIST(2, P))>& functor)
  141. {
  142. return Private::RegisterCommand<AZStd::function<RT(LIST(2, P))>, CCommand2wRet<LIST(2, P), RT> >(pCmdMgr, module, name, description, example, functor);
  143. }
  144. template <LIST(3, typename P)>
  145. bool CommandManagerHelper::RegisterCommand(ICommandManager* pCmdMgr, const char* module, const char* name,
  146. const char* description, const char* example,
  147. const AZStd::function<void(LIST(3, P))>& functor)
  148. {
  149. return Private::RegisterCommand<AZStd::function<void(LIST(3, P))>, CCommand3<LIST(3, P)> >(pCmdMgr, module, name, description, example, functor);
  150. }
  151. template <LIST(3, typename P), typename RT>
  152. bool CommandManagerHelper::RegisterCommand(ICommandManager* pCmdMgr, const char* module, const char* name,
  153. const char* description, const char* example,
  154. const AZStd::function<RT(LIST(3, P))>& functor)
  155. {
  156. return Private::RegisterCommand<AZStd::function<RT(LIST(3, P))>, CCommand3wRet<LIST(3, P), RT> >(pCmdMgr, module, name, description, example, functor);
  157. }
  158. template <LIST(4, typename P)>
  159. bool CommandManagerHelper::RegisterCommand(ICommandManager* pCmdMgr, const char* module, const char* name,
  160. const char* description, const char* example,
  161. const AZStd::function<void(LIST(4, P))>& functor)
  162. {
  163. return Private::RegisterCommand<AZStd::function<void(LIST(4, P))>, CCommand4<LIST(4, P)> >(pCmdMgr, module, name, description, example, functor);
  164. }
  165. template <LIST(4, typename P), typename RT>
  166. bool CommandManagerHelper::RegisterCommand(ICommandManager* pCmdMgr, const char* module, const char* name,
  167. const char* description, const char* example,
  168. const AZStd::function<RT(LIST(4, P))>& functor)
  169. {
  170. return Private::RegisterCommand<AZStd::function<RT(LIST(4, P))>, CCommand4wRet<LIST(4, P), RT> >(pCmdMgr, module, name, description, example, functor);
  171. }
  172. template <LIST(5, typename P)>
  173. bool CommandManagerHelper::RegisterCommand(ICommandManager* pCmdMgr, const char* module, const char* name,
  174. const char* description, const char* example,
  175. const AZStd::function<void(LIST(5, P))>& functor)
  176. {
  177. return Private::RegisterCommand<AZStd::function<void(LIST(5, P))>, CCommand5<LIST(5, P)> >(pCmdMgr, module, name, description, example, functor);
  178. }
  179. template <LIST(6, typename P)>
  180. bool CommandManagerHelper::RegisterCommand(ICommandManager* pCmdMgr, const char* module, const char* name,
  181. const char* description, const char* example,
  182. const AZStd::function<void(LIST(6, P))>& functor)
  183. {
  184. return Private::RegisterCommand<AZStd::function<void(LIST(6, P))>, CCommand6<LIST(6, P)> >(pCmdMgr, module, name, description, example, functor);
  185. }
  186. #endif // CRYINCLUDE_EDITOR_INCLUDE_ICOMMANDMANAGER_H