UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
Delegate.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
5#include "CoreTypes.h"
7#include "UObject/NameTypes.h"
10#include "Delegates/MulticastDelegateBase.h" // IWYU pragma: export
11#include "Delegates/IntegerSequence.h" // IWYU pragma: export
12#include "AutoRTFM.h"
13
197#define HEADER_GENERATED_DELEGATE_SIGNATURE_SUFFIX TEXT("__DelegateSignature")
198
200#define FUNC_CONCAT( ... ) __VA_ARGS__
201
208#define FUNC_DECLARE_DELEGATE( DelegateName, ReturnType, ... ) \
209 typedef TDelegate<ReturnType(__VA_ARGS__)> DelegateName;
210
212#define FUNC_DECLARE_MULTICAST_DELEGATE( MulticastDelegateName, ReturnType, ... ) \
213 typedef TMulticastDelegate<ReturnType(__VA_ARGS__)> MulticastDelegateName;
214
216#define FUNC_DECLARE_TS_MULTICAST_DELEGATE( MulticastDelegateName, ReturnType, ... ) \
217 typedef TMulticastDelegate<ReturnType(__VA_ARGS__), FDefaultTSDelegateUserPolicy> MulticastDelegateName;
218
224#define FUNC_DECLARE_EVENT( OwningType, EventName, ReturnType, ... ) \
225 class EventName : public TMulticastDelegate<ReturnType(__VA_ARGS__)> \
226 { \
227 friend class OwningType; \
228 };
229
231#define DECLARE_DERIVED_EVENT( OwningType, BaseTypeEvent, EventName ) \
232 class EventName : public BaseTypeEvent { friend class OwningType; };
233
235#define FUNC_DECLARE_DYNAMIC_DELEGATE( DynamicDelegateClassName, ExecFunction, FuncParamList, FuncParamPassThru, ... ) \
236 class DynamicDelegateClassName : public TBaseDynamicDelegate<FNotThreadSafeDelegateMode, __VA_ARGS__> \
237 { \
238 public: \
239 \
240 DynamicDelegateClassName() \
241 { \
242 } \
243 \
244 \
245 explicit DynamicDelegateClassName( const TScriptDelegate<>& InScriptDelegate ) \
246 : TBaseDynamicDelegate<FNotThreadSafeDelegateMode, __VA_ARGS__>( InScriptDelegate ) \
247 { \
248 } \
249 \
250 \
251 inline void Execute( FuncParamList ) const \
252 { \
253 /* Verify that the user object is still valid. We only have a weak reference to it. */ \
254 checkSlow( IsBound() ); \
255 ExecFunction( FuncParamPassThru ); \
256 } \
257 \
258 inline bool ExecuteIfBound( FuncParamList ) const \
259 { \
260 if( IsBound() ) \
261 { \
262 ExecFunction( FuncParamPassThru ); \
263 return true; \
264 } \
265 return false; \
266 } \
267 };
268
270#define FUNC_DECLARE_DYNAMIC_DELEGATE_RETVAL(DynamicDelegateRetValClassName, ExecFunction, RetValType, FuncParamList, FuncParamPassThru, ...) \
271 class DynamicDelegateRetValClassName : public TBaseDynamicDelegate<FNotThreadSafeDelegateMode, __VA_ARGS__> \
272 { \
273 public: \
274 \
275 DynamicDelegateRetValClassName() \
276 { \
277 } \
278 \
279 \
280 explicit DynamicDelegateRetValClassName( const TScriptDelegate<>& InScriptDelegate ) \
281 : TBaseDynamicDelegate<FNotThreadSafeDelegateMode, __VA_ARGS__>( InScriptDelegate ) \
282 { \
283 } \
284 \
285 \
286 inline RetValType Execute( FuncParamList ) const \
287 { \
288 /* Verify that the user object is still valid. We only have a weak reference to it. */ \
289 checkSlow( IsBound() ); \
290 return ExecFunction( FuncParamPassThru ); \
291 } \
292 };
293
294
296#define FUNC_DECLARE_DYNAMIC_MULTICAST_DELEGATE(DynamicMulticastDelegateClassName, ExecFunction, FuncParamList, FuncParamPassThru, ...) \
297class DynamicMulticastDelegateClassName : public TBaseDynamicMulticastDelegate<FNotThreadSafeDelegateMode, __VA_ARGS__> \
298 { \
299 public: \
300 \
301 DynamicMulticastDelegateClassName() \
302 { \
303 } \
304 \
305 \
306 explicit DynamicMulticastDelegateClassName( const TMulticastScriptDelegate<>& InMulticastScriptDelegate ) \
307 : TBaseDynamicMulticastDelegate<FNotThreadSafeDelegateMode, __VA_ARGS__>( InMulticastScriptDelegate ) \
308 { \
309 } \
310 \
311 \
312 void Broadcast( FuncParamList ) const \
313 { \
314 ExecFunction( FuncParamPassThru ); \
315 } \
316 };
317
318
319#define ENABLE_STATIC_FUNCTION_FNAMES (PLATFORM_COMPILER_CLANG && (__clang_major__ > 3 || (__clang_major__ == 3 && __clang_minor__ >= 5)))
320
321#if ENABLE_STATIC_FUNCTION_FNAMES
322
324 {
325 template <typename T>
326 struct TTypedImpl
327 {
328 // Dummy bool parameter needed so that we can ensure the last specialization is partial
329 template <bool Dummy, T... Chars>
330 struct Inner;
331
332 template <typename SecondMatch, T... Chars>
333 struct Inner2
334 {
335 typedef typename TTypedImpl<T>::template Inner<true, Chars...>::Type Type;
336 };
337
338 template <T... Chars>
339 struct Inner2<TIntegerSequence<T>, Chars...>
340 {
341 typedef TIntegerSequence<T, Chars...> Type;
342 };
343
344 template <bool Dummy, T Char, T... Chars>
345 struct Inner<Dummy, Char, Chars...>
346 {
347 typedef typename Inner<Dummy, Chars...>::Type Type;
348 };
349
350 template <bool Dummy, T... Chars>
351 struct Inner<Dummy, ':', ':', Chars...>
352 {
353 typedef typename Inner2<typename Inner<Dummy, Chars...>::Type, Chars...>::Type Type;
354 };
355
356 // Without the dummy bool parameter, this would not compile, as full specializations
357 // of nested class templates is not allowed.
358 template <bool Dummy>
359 struct Inner<Dummy>
360 {
361 typedef TIntegerSequence<T> Type;
362 };
363 };
364
365
366 template <typename IntSeq>
368
369 template <typename T, T... Chars>
371 {
372 static FName Get()
373 {
374 static const T Str[sizeof...(Chars) + 1] = { Chars..., 0 };
375 FName* Result = nullptr;
377 {
378 static FName StaticResult{Str};
379 Result = &StaticResult;
380 };
381 return *Result;
382 }
383 };
384
391 template <typename T>
393
394 template <typename T, T... Chars>
395 struct TStrAfterLastDoubleColon<TIntegerSequence<T, Chars...>>
396 {
397 typedef typename NStrAfterLastDoubleColon_Private::TTypedImpl<T>::template Inner<true, Chars...>::Type Type;
398 };
399 }
400
404 template <typename T, T... Chars>
405 UE_FORCEINLINE_HINT constexpr TIntegerSequence<T, Chars...> operator""_intseq()
406 {
407 return {};
408 }
409
410 #define STATIC_FUNCTION_FNAME(str) NStrAfterLastDoubleColon_Private::TStaticFNameFromCharSequence<typename NStrAfterLastDoubleColon_Private::TStrAfterLastDoubleColon<decltype(PREPROCESSOR_JOIN(str, _intseq))>::Type>::Get()
411
412#else
413
414 #define STATIC_FUNCTION_FNAME(str) UE::Delegates::Private::GetTrimmedMemberFunctionName(str)
415
416#endif
417
418
425#define BindDynamic( UserObject, FuncName ) __Internal_BindDynamic( UserObject, FuncName, STATIC_FUNCTION_FNAME( #FuncName ) )
426
433#define AddDynamic( UserObject, FuncName ) __Internal_AddDynamic( UserObject, FuncName, STATIC_FUNCTION_FNAME( #FuncName ) )
434
441#define AddUniqueDynamic( UserObject, FuncName ) __Internal_AddUniqueDynamic( UserObject, FuncName, STATIC_FUNCTION_FNAME( #FuncName ) )
442
449#define RemoveDynamic( UserObject, FuncName ) __Internal_RemoveDynamic( UserObject, FuncName, STATIC_FUNCTION_FNAME( #FuncName ) )
450
457#define IsAlreadyBound( UserObject, FuncName ) __Internal_IsAlreadyBound( UserObject, FuncName, STATIC_FUNCTION_FNAME( #FuncName ) )
458
459
461{
470 {
471 // We strip off the class prefix and just return the function name by itself.
473 const ANSICHAR* Result = FCStringAnsi::Strrstr( InMacroFunctionName, "::" );
474 checkf(Result && Result[2] != '0', TEXT("'%hs' does not look like a member function"), InMacroFunctionName);
475 return FName(Result + 2);
476 }
477}
478
479
480/*********************************************************************************************************************/
481
482// We define this as a guard to prevent DelegateSignatureImpl.inl being included outside of this file
483#define __Delegate_h__
484#define FUNC_INCLUDING_INLINE_IMPL
485
486#if !UE_BUILD_DOCS
489 #include "Delegates/DelegateSignatureImpl.inl" // IWYU pragma: export
490 #include "Delegates/DelegateCombinations.h" // IWYU pragma: export
491#endif
492
493// No longer allowed to include DelegateSignatureImpl.inl
494#undef FUNC_INCLUDING_INLINE_IMPL
495#undef __Delegate_h__
496
497/*********************************************************************************************************************/
498
499// Simple delegate used by various utilities such as timers
#define check(expr)
Definition AssertionMacros.h:314
#define checkf(expr, format,...)
Definition AssertionMacros.h:315
#define TEXT(x)
Definition Platform.h:1272
#define UE_FORCEINLINE_HINT
Definition Platform.h:723
FPlatformTypes::ANSICHAR ANSICHAR
An ANSI character. Normally a signed type.
Definition Platform.h:1131
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
#define DECLARE_DELEGATE(DelegateName)
Definition DelegateCombinations.h:20
#define DECLARE_MULTICAST_DELEGATE(DelegateName)
Definition DelegateCombinations.h:23
#define DECLARE_TS_MULTICAST_DELEGATE(DelegateName)
Definition DelegateCombinations.h:26
@ Char
Character type.
Definition NameTypes.h:617
Definition DelegateHandle.cpp:9
FName GetTrimmedMemberFunctionName(const ANSICHAR *InMacroFunctionName)
Definition Delegate.h:469
static UE_FORCEINLINE_HINT const CharType * Strrstr(const CharType *String, const CharType *Find)
Definition CString.h:1102
Definition IntegerSequence.h:9