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 * Creates a native delegate that is bound to the same UFunction as the specified dynamic delegate.
24 *
25 * @note This function can only convert non-multicast delegates.
26 *
27 * The signatures of the functions are checked, so that it is not possible to compile a conversion that would crash
28 * at runtime.
29 *
30 * Example Usage:
31 *
32 * using FMyNativeDelegate = TDelegate<void(int32 someParam)>;
33 * DECLARE_DYNAMIC_DELEGATE_OneParam(FMyBlueprintDelegate, const int32, someParam);
34 *
35 * void MyNativeDelegateFunction(FMyNativeDelegate delegate)
36 * {
37 * ...
38 * }
39 *
40 * void MyBlueprintDelegateFunction(FMyBlueprintDelegate delegate)
41 * {
42 * MyNativeDelegateFunction(Delegates::AsNative(delegate));
43 * }
44 *
45 * @tparam Dynamic The origin type, i.e. the dynamic delegate. Will be auto-deduced.
46 * @tparam NativeDelegateType The target type, i.e. the delegate that you want to produce. Will be auto-deduced from Dynamic.
47 * @param dynamicDelegate The dynamic delegate that will be converted
48 */
49 template <
50 CDynamicDelegate Dynamic,
51 typename MethodPtrTypeDynamic = TDynamicMethodPtr<Dynamic>,
52 typename NativeDelegateType = TDelegate<TFunction_Signature<MethodPtrTypeDynamic>>,
53 typename MethodPtrTypeNative = typename TMemFunPtrType<
54 false,
55 FDeclareOnly,
56 typename NativeDelegateType::TFuncType
57 >::Type
58 >
59 requires CSameAs<MethodPtrTypeDynamic, MethodPtrTypeNative>
60 NativeDelegateType AsNative(Dynamic&& dynamicDelegate)
61 {
62 // The TBaseUFunctionDelegateInstance constructor asserts if the function name is NAME_None. We therefore must check
63 // to see if the dynamic delegate is bound at all. If it isn't, return an unbound native delegate.
64 if (!dynamicDelegate.IsBound())
65 {
66 return NativeDelegateType();
67 }
68
69 return NativeDelegateType::CreateUFunction(dynamicDelegate.GetUObject(), dynamicDelegate.GetFunctionName());
70 }
71}
NativeDelegateType AsNative(Dynamic &&dynamicDelegate)
Definition AsNative.h:60
typename TDynamicMethodPtr_Struct< std::decay_t< Dynamic > >::Type TDynamicMethodPtr
Definition Traits.h:55