MCRO
C++23 utilities for Unreal Engine.
Loading...
Searching...
No Matches
AsNative.h
Go to the documentation of this file.
1/** @noop License Comment
2 * @file
3 * @copyright
4 * This Source Code is subject to the terms of the Mozilla Public License, v2.0.
5 * If a copy of the MPL was not distributed with this file You can obtain one at
6 * https://mozilla.org/MPL/2.0/
7 *
8 * @author David Mórász
9 * @date 2025
10 */
11
12#pragma once
13
14#include "CoreMinimal.h"
15#include "Mcro/FunctionTraits.h"
17
19{
20 using namespace Mcro::FunctionTraits;
21
22 /**
23 * @brief
24 * Creates a native delegate that is bound to the same UFunction as the specified dynamic delegate.
25 *
26 * @note This function can only convert non-multicast delegates.
27 *
28 * The signatures of the functions are checked, so that it is not possible to compile a conversion that would crash
29 * at runtime.
30 *
31 * Example Usage:
32 *
33 * @code
34 * using FMyNativeDelegate = TDelegate<void(int32 someParam)>;
35 * DECLARE_DYNAMIC_DELEGATE_OneParam(FMyBlueprintDelegate, int32, someParam);
36 *
37 * void MyNativeDelegateFunction(FMyNativeDelegate delegate)
38 * {
39 * ...
40 * }
41 *
42 * void MyBlueprintDelegateFunction(FMyBlueprintDelegate delegate)
43 * {
44 * MyNativeDelegateFunction(Delegates::AsNative(delegate));
45 * }
46 * @endcode
47 *
48 * @tparam Dynamic The origin type, i.e. the dynamic delegate. Will be auto-deduced.
49 * @tparam NativeDelegateType The target type, i.e. the delegate that you want to produce. Will be auto-deduced from Dynamic.
50 * @param dynamicDelegate The dynamic delegate that will be converted
51 */
52 template <
53 CDynamicDelegate Dynamic,
54 typename MethodPtrTypeDynamic = TDynamicMethodPtr<Dynamic>,
55 typename NativeDelegateType = TDelegate<TFunction_Signature<MethodPtrTypeDynamic>>,
56 typename MethodPtrTypeNative = typename TMemFunPtrType<
57 false,
58 FDeclareOnly,
59 typename NativeDelegateType::TFuncType
60 >::Type
61 >
62 requires CSameAsDecayed<MethodPtrTypeDynamic, MethodPtrTypeNative>
63 NativeDelegateType AsNative(Dynamic&& dynamicDelegate)
64 {
65 // The TBaseUFunctionDelegateInstance constructor asserts if the function name is NAME_None. We therefore must check
66 // to see if the dynamic delegate is bound at all. If it isn't, return an unbound native delegate.
67 if (!dynamicDelegate.IsBound())
68 {
69 return NativeDelegateType();
70 }
71
72 return NativeDelegateType::CreateUFunction(dynamicDelegate.GetUObject(), dynamicDelegate.GetFunctionName());
73 }
74}
Constraint given type to a dynamic delegate class.
Definition Traits.h:44
NativeDelegateType AsNative(Dynamic &&dynamicDelegate)
Creates a native delegate that is bound to the same UFunction as the specified dynamic delegate.
Definition AsNative.h:63
typename TDynamicMethodPtr_Struct< std::decay_t< Dynamic > >::Type TDynamicMethodPtr
Get the native function pointer type compatible with given dynamic (multicast) delegate.
Definition Traits.h:73