From fc94ede4398b9d8b6f96ecdc63ad0f158cf7f2c4 Mon Sep 17 00:00:00 2001 From: AMZN-ScottR <24445312+AMZN-ScottR@users.noreply.github.com> Date: Thu, 28 Oct 2021 11:55:49 -0700 Subject: [PATCH] [profiler_capture_api] fixed runtime issues with BehaviorInterfaceProxy reflection Signed-off-by: AMZN-ScottR <24445312+AMZN-ScottR@users.noreply.github.com> --- .../AzCore/Debug/ProfilerReflection.cpp | 10 +++- .../AzCore/RTTI/BehaviorInterfaceProxy.h | 58 ++++++++++--------- 2 files changed, 40 insertions(+), 28 deletions(-) diff --git a/Code/Framework/AzCore/AzCore/Debug/ProfilerReflection.cpp b/Code/Framework/AzCore/AzCore/Debug/ProfilerReflection.cpp index 430e320c54..3455400cfe 100644 --- a/Code/Framework/AzCore/AzCore/Debug/ProfilerReflection.cpp +++ b/Code/Framework/AzCore/AzCore/Debug/ProfilerReflection.cpp @@ -51,13 +51,15 @@ namespace AZ::Debug { public: AZ_RTTI(ProfilerSystemScriptProxy, "{D671FB70-8B09-4C3A-96CD-06A339F3138E}", BehaviorInterfaceProxy); + + AZ_BEHAVIOR_INTERFACE(ProfilerSystemScriptProxy, ProfilerRequests); }; void ProfilerReflect(AZ::ReflectContext* context) { if (AZ::BehaviorContext* behaviorContext = azrtti_cast(context)) { - behaviorContext->ConstantProperty("g_ProfilerSystem", ProfilerSystemScriptProxy::GetInstance) + behaviorContext->ConstantProperty("g_ProfilerSystem", ProfilerSystemScriptProxy::GetProxy) ->Attribute(AZ::Script::Attributes::Category, ProfilerScriptCategory) ->Attribute(AZ::Script::Attributes::Module, ProfilerScriptModule) ->Attribute(AZ::Script::Attributes::Scope, ProfilerScriptScope); @@ -69,6 +71,12 @@ namespace AZ::Debug ->Method("IsValid", &ProfilerSystemScriptProxy::IsValid) + ->Method("GetCaptureLocation", + [](ProfilerSystemScriptProxy*) -> AZStd::string + { + return AZStd::string(GetProfilerCaptureLocation().c_str()); + }) + ->Method("IsActive", ProfilerSystemScriptProxy::WrapMethod<&ProfilerRequests::IsActive>()) ->Method("SetActive", ProfilerSystemScriptProxy::WrapMethod<&ProfilerRequests::SetActive>()) diff --git a/Code/Framework/AzCore/AzCore/RTTI/BehaviorInterfaceProxy.h b/Code/Framework/AzCore/AzCore/RTTI/BehaviorInterfaceProxy.h index 430524d3a2..0e7a4ac356 100644 --- a/Code/Framework/AzCore/AzCore/RTTI/BehaviorInterfaceProxy.h +++ b/Code/Framework/AzCore/AzCore/RTTI/BehaviorInterfaceProxy.h @@ -34,13 +34,14 @@ namespace AZ * { * public: * AZ_RTTI(MySystemProxy, "{CDCDCDCD-BAAD-BADD-F00D-CDCDCDCDCDCD}", BehaviorInterfaceProxy); + * AZ_BEHAVIOR_INTERFACE(MySystemProxy, MyInterface); * }; * * void Reflect(AZ::ReflectContext* context) * { * if (AZ::BehaviorContext* behaviorContext = azrtti_cast(context)) * { - * behaviorContext->ConstantProperty("g_MySystem", MySystemProxy::GetInstance) + * behaviorContext->ConstantProperty("g_MySystem", MySystemProxy::GetProxy) * ->Attribute(AZ::Script::Attributes::Scope, AZ::Script::Attributes::ScopeFlags::Common) * ->Attribute(AZ::Script::Attributes::Module, "MyModule"); * @@ -60,26 +61,6 @@ namespace AZ AZ_CLASS_ALLOCATOR(BehaviorInterfaceProxy, AZ::SystemAllocator, 0); AZ_RTTI(BehaviorInterfaceProxy, "{E7CC8D27-4499-454E-A7DF-3F72FBECD30D}"); - //! Accessor for use with ConstantProperty - static T* GetInstance() - { - T* interfacePtr = AZ::Interface::Get(); - AZ_Warning("BehaviorInterfaceProxy", interfacePtr, - "There is currently no global %s registered with an AZ Interface", - AzTypeInfo::Name() - ); - // Don't delete the global instance, it is not owned by the behavior context - return interfacePtr; - } - - //! Helper for attaching interface function via ClassBuilder::Method - template - static auto WrapMethod() - { - using FuncTraits = AZStd::function_traits>; - return FuncTraits::template expand_args::template WrapMethod(); - } - BehaviorInterfaceProxy() = default; virtual ~BehaviorInterfaceProxy() = default; @@ -98,25 +79,48 @@ namespace AZ //! Returns if the m_instance shared pointer is non-nullptr bool IsValid() const { return m_instance; } - private: - template + protected: + //! Internal access for use in the derived GetProxy function + static T* GetInstance() + { + T* interfacePtr = AZ::Interface::Get(); + AZ_Warning("BehaviorInterfaceProxy", interfacePtr, + "There is currently no global %s registered with an AZ Interface", + AzTypeInfo::Name() + ); + // Don't delete the global instance, it is not owned by the behavior context + return interfacePtr; + } + + template struct MethodWrapper { - template + template static auto WrapMethod() { - using return_type = AZStd::function_traits_get_result_t>; - return [](BehaviorInterfaceProxy* proxy, Args... params) -> return_type + using ReturnType = AZStd::function_traits_get_result_t>; + return [](Proxy* proxy, Args... params) -> ReturnType { if (proxy && proxy->IsValid()) { return AZStd::invoke(Method, proxy->m_instance, AZStd::forward(params)...); } - return return_type(); + return ReturnType(); }; } }; AZStd::shared_ptr m_instance; }; + + #define AZ_BEHAVIOR_INTERFACE(ProxyType, InterfaceType) \ + static ProxyType GetProxy() { return GetInstance(); } \ + template \ + static auto WrapMethod() { \ + using FuncTraits = AZStd::function_traits>; \ + return FuncTraits::template expand_args::template WrapMethod(); \ + } \ + ProxyType() = default; \ + ProxyType(AZStd::shared_ptr sharedInstance) : BehaviorInterfaceProxy(sharedInstance) {} \ + ProxyType(InterfaceType* rawIntance) : BehaviorInterfaceProxy(rawIntance) {} } // namespace AZ