UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
FunctionWithContext.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
5#include "HAL/Platform.h"
6#include "Templates/Invoke.h"
7#include <type_traits>
8
9namespace UE
10{
11
36template <typename FunctionType>
38
39template <typename ReturnType, typename... ArgTypes>
40class TFunctionWithContext<ReturnType (ArgTypes...)>
41{
42public:
43 using FunctionType = ReturnType (void*, ArgTypes...);
44
46 template <typename LambdaType>
48 requires (!std::is_same_v<std::decay_t<LambdaType>, TFunctionWithContext>) &&
49 std::is_invocable_r_v<ReturnType, std::decay_t<LambdaType>, ArgTypes...>
50 : Function(&Call<std::decay_t<LambdaType>>)
51 , Context(&Lambda)
52 {
53 }
54
56 template <typename LambdaType>
58 requires (!std::is_same_v<std::decay_t<LambdaType>, TFunctionWithContext>) &&
59 std::is_invocable_r_v<ReturnType, std::decay_t<LambdaType>, ArgTypes...>
60 {
61 Function = &Call<std::decay_t<LambdaType>>;
62 Context = &Lambda;
63 return *this;
64 }
65
72
74 inline constexpr TFunctionWithContext(decltype(nullptr))
75 {
76 }
77
79 inline explicit operator bool() const
80 {
81 return !!Function;
82 }
83
85 inline ReturnType operator()(ArgTypes... Args) const
86 {
87 return Function(Context, (ArgTypes&&)Args...);
88 }
89
90 inline FunctionType* GetFunction() const
91 {
92 return Function;
93 }
94
95 inline void* GetContext() const
96 {
97 return Context;
98 }
99
100private:
101 template <typename LambdaType>
102 static ReturnType Call(void* Lambda, ArgTypes... Args)
103 {
104 return Invoke(*(LambdaType*)Lambda, (ArgTypes&&)Args...);
105 }
106
107 FunctionType* Function = nullptr;
108 void* Context = nullptr;
109};
110
111} // UE
#define UE_LIFETIMEBOUND
Definition Platform.h:812
AUTORTFM_INFER UE_FORCEINLINE_HINT constexpr auto Invoke(FuncType &&Func, ArgTypes &&... Args) -> decltype(((FuncType &&) Func)((ArgTypes &&) Args...))
Definition Invoke.h:44
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
const bool
Definition NetworkReplayStreaming.h:178
ReturnType(void *, ArgTypes...) FunctionType
Definition FunctionWithContext.h:43
FunctionType * GetFunction() const
Definition FunctionWithContext.h:90
ReturnType operator()(ArgTypes... Args) const
Definition FunctionWithContext.h:85
TFunctionWithContext & operator=(LambdaType &&Lambda UE_LIFETIMEBOUND)
Definition FunctionWithContext.h:57
void * GetContext() const
Definition FunctionWithContext.h:95
constexpr TFunctionWithContext(decltype(nullptr))
Definition FunctionWithContext.h:74
TFunctionWithContext(FunctionType *InFunction, void *InContext)
Definition FunctionWithContext.h:67
TFunctionWithContext(LambdaType &&Lambda UE_LIFETIMEBOUND)
Definition FunctionWithContext.h:47
Definition FunctionWithContext.h:37
Definition AdvancedWidgetsModule.cpp:13