CallProcessorBinder.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. #pragma once
  9. #include <AzCore/std/containers/vector.h>
  10. #include <AzCore/std/smart_ptr/unique_ptr.h>
  11. #include <SceneAPI/SceneCore/SceneCoreConfiguration.h>
  12. #include <SceneAPI/SceneCore/Events/ProcessingResult.h>
  13. #include <SceneAPI/SceneCore/Events/CallProcessorBus.h>
  14. namespace AZ
  15. {
  16. class ReflectContext;
  17. namespace SceneAPI
  18. {
  19. namespace Events
  20. {
  21. // CallProcessorBinder automatically registers to the CallProcessorBus to
  22. // handle process calls on behave to the parent class by filtering
  23. // and forwarding calls to the appropriate functions.
  24. // To use, derive from CallProcessorBinder and call "BindToCall"
  25. // one or more times to register functions with that accept a
  26. // processor context with the signature "ProcessingResult(X& context) const" or
  27. // "ProcessingResult(X& context)", where X is any class derived from ICallContext.
  28. //
  29. // Example:
  30. // Example inherits from CallProcessorBinder and has the following
  31. // function: ProcessingResult ProcessContext(ExampleContext& context);
  32. // In Example's constructor call:
  33. // BindToCall(&Example::ProcessContext);
  34. // If an processor call with the ExampleContext is send,
  35. // Example::ProcessContext will automatically be called.
  36. class SCENE_CORE_CLASS CallProcessorBinder :
  37. public CallProcessorBus::Handler
  38. {
  39. public:
  40. enum class TypeMatch
  41. {
  42. Exact,
  43. Derived
  44. };
  45. AZ_RTTI(CallProcessorBinder, "{887A50B4-3FC4-4695-A88E-CA7BE931A73E}");
  46. SCENE_CORE_API ProcessingResult Process(ICallContext* context) override final;
  47. CallProcessorBinder() = default;
  48. SCENE_CORE_API virtual ~CallProcessorBinder();
  49. static void Reflect(AZ::ReflectContext* context);
  50. protected:
  51. CallProcessorBinder(const CallProcessorBinder&) = delete;
  52. template<typename Class, typename ContextType>
  53. inline void BindToCall(ProcessingResult(Class::*Func)(ContextType& context) const, TypeMatch typeMatch = TypeMatch::Exact);
  54. template<typename Class, typename ContextType>
  55. inline void BindToCall(ProcessingResult(Class::*Func)(ContextType& context), TypeMatch typeMatch = TypeMatch::Exact);
  56. SCENE_CORE_API void ActivateBindings();
  57. SCENE_CORE_API void DeactivateBindings();
  58. SCENE_CORE_API void ClearBindings();
  59. private:
  60. class FunctionBinding
  61. {
  62. public:
  63. virtual ~FunctionBinding() = default;
  64. virtual ProcessingResult Process(CallProcessorBinder* thisPtr, ICallContext* context) = 0;
  65. protected:
  66. template<typename Class, typename ContextType, typename Function>
  67. ProcessingResult Call(CallProcessorBinder* thisPtr, ICallContext* context, Function function);
  68. };
  69. template<typename Class, typename ContextType>
  70. class ConstFunctionBindingTemplate : public FunctionBinding
  71. {
  72. public:
  73. using Function = ProcessingResult(Class::*)(ContextType&) const;
  74. explicit ConstFunctionBindingTemplate(Function function);
  75. ~ConstFunctionBindingTemplate() override = default;
  76. ProcessingResult Process(CallProcessorBinder* thisPtr, ICallContext* context) override;
  77. private:
  78. Function m_function;
  79. };
  80. template<typename Class, typename ContextType>
  81. class FunctionBindingTemplate : public FunctionBinding
  82. {
  83. public:
  84. using Function = ProcessingResult(Class::*)(ContextType&);
  85. explicit FunctionBindingTemplate(Function function);
  86. ~FunctionBindingTemplate() override = default;
  87. ProcessingResult Process(CallProcessorBinder* thisPtr, ICallContext* context) override;
  88. private:
  89. Function m_function;
  90. };
  91. template<typename Class, typename ContextType>
  92. class ConstDerivedFunctionBindingTemplate : public FunctionBinding
  93. {
  94. public:
  95. using Function = ProcessingResult(Class::*)(ContextType&) const;
  96. explicit ConstDerivedFunctionBindingTemplate(Function function);
  97. ~ConstDerivedFunctionBindingTemplate() override = default;
  98. ProcessingResult Process(CallProcessorBinder* thisPtr, ICallContext* context) override;
  99. private:
  100. Function m_function;
  101. };
  102. template<typename Class, typename ContextType>
  103. class DerivedFunctionBindingTemplate : public FunctionBinding
  104. {
  105. public:
  106. using Function = ProcessingResult(Class::*)(ContextType&);
  107. explicit DerivedFunctionBindingTemplate(Function function);
  108. ~DerivedFunctionBindingTemplate() override = default;
  109. ProcessingResult Process(CallProcessorBinder* thisPtr, ICallContext* context) override;
  110. private:
  111. Function m_function;
  112. };
  113. AZStd::vector<AZStd::unique_ptr<FunctionBinding>> m_bindings;
  114. };
  115. } // namespace Events
  116. } // namespace SceneAPI
  117. } // namespace AZ
  118. #include <SceneAPI/SceneCore/Events/CallProcessorBinder.inl>