UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
Attribute.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"
6#include "Misc/TVariant.h"
10#include "Delegates/Delegate.h"
11
15template< typename ObjectType >
17{
18
19public:
20
28 DECLARE_DELEGATE_RetVal( ObjectType, FGetter );
29
30
33 : Value() // NOTE: Potentially uninitialized for atomics!!
34 , bIsSet(false)
35 , Getter()
36 {
37 }
38
44 template< typename OtherType >
46 : Value( static_cast<ObjectType>(InInitialValue) )
47 , bIsSet(true)
48 , Getter()
49 {
50 }
51
58 : Value(MoveTemp(InInitialValue))
59 , bIsSet(true)
60 , Getter()
61 {
62 }
63
72 template< class SourceType >
74 : Value()
75 , bIsSet(true)
76 , Getter(FGetter::CreateSP(InUserObject, InMethodPtr))
77 {
78 }
79
88 template< class SourceType >
89 TAttribute( SourceType* InUserObject, typename FGetter::template TConstMethodPtr< SourceType > InMethodPtr )
90 : Value()
91 , bIsSet(true)
92 , Getter(FGetter::CreateSP(InUserObject, InMethodPtr))
93 {
94 }
95
101 [[nodiscard]] static TAttribute Create( const FGetter& InGetter )
102 {
103 const bool bExplicitConstructor = true;
105 }
106
112 [[nodiscard]] static TAttribute Create(FGetter&& InGetter)
113 {
114 const bool bExplicitConstructor = true;
116 }
117
125 template <typename FuncPtrType, typename... VarTypes >
127 {
128 const bool bExplicitConstructor = true;
129 return TAttribute(FGetter::CreateStatic(InFuncPtr, MoveTemp(Vars)...), bExplicitConstructor);
130 }
131
135 template<typename SourceType, typename SourceTypeOrBase, typename... PayloadTypes>
136 [[nodiscard]] UE_FORCEINLINE_HINT static TAttribute CreateRaw(const SourceType* InObject, ObjectType (SourceTypeOrBase::*InMethod)(PayloadTypes...) const, typename TDecay<PayloadTypes>::Type... InputPayload)
137 {
138 return Create(FGetter::CreateRaw(InObject, InMethod, MoveTemp(InputPayload)...));
139 }
140
144 template<typename SourceType, typename SourceTypeOrBase, typename... PayloadTypes>
145 [[nodiscard]] UE_FORCEINLINE_HINT static TAttribute CreateSP(const SourceType* InObject, ObjectType (SourceTypeOrBase::*InMethod)(PayloadTypes...) const, typename TDecay<PayloadTypes>::Type... InputPayload)
146 {
147 return Create(FGetter::CreateSP(InObject, InMethod, MoveTemp(InputPayload)...));
148 }
149
154 template<typename LambdaType, typename... PayloadTypes>
159
163 template<typename UserClass, ESPMode Mode, typename LambdaType, typename... PayloadTypes>
168 template <typename UserClass, typename LambdaType, typename... PayloadTypes>
173
177 template <typename UserClass, typename LambdaType, typename... PayloadTypes>
182
191 template< class SourceType >
198
203
209 template< typename OtherType >
210 void Set( const OtherType& InNewValue )
211 {
212 Getter.Unbind();
213 Value = InNewValue;
214 bIsSet = true;
215 }
216
222 void Set( ObjectType&& InNewValue )
223 {
224 Getter.Unbind();
225 Value = MoveTemp(InNewValue);
226 bIsSet = true;
227 }
228
230 bool IsSet() const
231 {
232 return bIsSet;
233 }
234
241 const ObjectType& Get() const
242 {
243 // If we have a getter delegate, then we'll call that to generate the value
244 if( Getter.IsBound() )
245 {
246 // Call the delegate to get the value. Note that this will assert if the delegate is not currently
247 // safe to call (e.g. object was deleted and we could detect that)
248
249 // NOTE: We purposely overwrite our value copy here so that we can return the value by address in
250 // the most common case, which is an attribute that doesn't have a delegate bound to it at all.
251 Value = Getter.Execute();
252 }
253
254 // Return the stored value
255 return Value;
256 }
257
262 const ObjectType& Get( const ObjectType& DefaultValue ) const
263 {
264 return bIsSet ? Get() : DefaultValue;
265 }
266
274 void Bind( const FGetter& InGetter )
275 {
276 bIsSet = true;
277 Getter = InGetter;
278 }
279
280
288 void Bind( FGetter&& InGetter )
289 {
290 bIsSet = true;
291 Getter = MoveTemp(InGetter);
292 }
293
301 template < typename... VarTypes >
302 void BindStatic( TIdentity_T< typename FGetter::template TFuncPtr< std::decay_t<VarTypes>... > > InFuncPtr, VarTypes&&... Vars )
303 {
304 bIsSet = true;
305 Getter.BindStatic( InFuncPtr, Forward<VarTypes>(Vars)... );
306 }
307
316 template< class SourceType, typename... VarTypes >
317 void BindRaw( SourceType* InUserObject, typename FGetter::template TConstMethodPtr< SourceType, std::decay_t<VarTypes>... > InMethodPtr, VarTypes&&... Vars )
318 {
319 bIsSet = true;
320 Getter.BindRaw( InUserObject, InMethodPtr, Forward<VarTypes>(Vars)... );
321 }
322
331 template< class SourceType, typename... VarTypes >
332 void Bind( TSharedRef< SourceType > InUserObject, typename FGetter::template TConstMethodPtr< SourceType, std::decay_t<VarTypes>... > InMethodPtr, VarTypes&&... Vars )
333 {
334 bIsSet = true;
335 Getter.BindSP( InUserObject, InMethodPtr, Forward<VarTypes>(Vars)... );
336 }
337
346 template< class SourceType, typename... VarTypes >
347 void Bind( SourceType* InUserObject, typename FGetter::template TConstMethodPtr< SourceType, std::decay_t<VarTypes>... > InMethodPtr, VarTypes&&... Vars )
348 {
349 bIsSet = true;
350 Getter.BindSP( InUserObject, InMethodPtr, Forward<VarTypes>(Vars)... );
351 }
352
361 template< class SourceType, typename... VarTypes >
362 void BindUObject( SourceType* InUserObject, typename FGetter::template TConstMethodPtr< SourceType, std::decay_t<VarTypes>... > InMethodPtr, VarTypes&&... Vars)
363 {
364 bIsSet = true;
365 Getter.BindUObject( InUserObject, InMethodPtr, Forward<VarTypes>(Vars)... );
366 }
367
376 template< class SourceType >
377 void BindUFunction( SourceType* InUserObject, const FName& InFunctionName )
378 {
379 bIsSet = true;
380 Getter.BindUFunction(InUserObject, InFunctionName);
381 }
382
387 template<typename LambdaType, typename... PayloadTypes>
389 {
390 bIsSet = true;
392 }
393
398 template<typename UserClass, ESPMode Mode, typename LambdaType, typename... PayloadTypes>
404 template <typename UserClass, typename LambdaType, typename... PayloadTypes>
406 {
407 bIsSet = true;
409 }
410
415 template<typename UserClass, typename LambdaType, typename... PayloadTypes>
417 {
418 bIsSet = true;
420 }
421
427 bool IsBound() const
428 {
429 return Getter.IsBound();
430 }
431
437 [[nodiscard]] const FGetter& GetBinding() const
438 {
439 return Getter;
440 }
441
448 {
449 checkf(IsSet(), TEXT("It is an error to call Steal() on an unset TAttribute. Check IsSet() before calling Steal()."));
450 bIsSet = false;
452 }
453
460 bool IdenticalTo(const TAttribute& InOther) const
461 {
462 const bool bIsBound = IsBound();
463
464 if ( bIsBound == InOther.IsBound() )
465 {
466 if ( bIsBound )
467 {
468 return Getter.GetHandle() == InOther.Getter.GetHandle();
469 }
470 else
471 {
472 return IsSet() == InOther.IsSet() && Value == InOther.Value;
473 }
474 }
475
476 return false;
477 }
478
479private:
480
482 TAttribute( const FGetter& InGetter, bool bExplicitConstructor )
483 : Value()
484 , bIsSet( true )
485 , Getter(InGetter)
486 { }
487
490 : Value()
491 , bIsSet(true)
492 , Getter(MoveTemp(InGetter))
493 { }
494
495 // We declare ourselves as a friend (templated using OtherType) so we can access members as needed
496 template< class OtherType > friend class TAttribute;
497
499 mutable ObjectType Value;
500
502 bool bIsSet;
503
507 FGetter Getter;
508};
509
510
514template<typename T, typename SourceType, typename SourceTypeOrBase, typename... PayloadTypes>
519
523template<typename T, typename SourceType, typename SourceTypeOrBase, typename... PayloadTypes>
528template<typename T, typename SourceType, typename SourceTypeOrBase, typename... PayloadTypes>
533
534template<typename T, typename SourceType, typename SourceTypeOrBase, typename... PayloadTypes>
539
544template<typename LambdaType, typename... PayloadTypes>
551
555template<typename UserClass, ESPMode Mode, typename LambdaType, typename... PayloadTypes>
562template <typename UserClass, typename LambdaType, typename... PayloadTypes>
569
573template <typename UserClass, typename LambdaType, typename... PayloadTypes>
#define checkf(expr, format,...)
Definition AssertionMacros.h:315
decltype(auto) MakeAttributeWeakLambda(UserClass *InUserObject, LambdaType &&InCallable, PayloadTypes &&... InputPayload)
Definition Attribute.h:574
decltype(auto) MakeAttributeSPLambda(const TSharedRef< UserClass, Mode > &InUserObjectRef, LambdaType &&InCallable, PayloadTypes &&... InputPayload)
Definition Attribute.h:556
UE_FORCEINLINE_HINT TAttribute< T > MakeAttributeRaw(const SourceType *InObject, T(SourceTypeOrBase::*InMethod)(PayloadTypes...) const, typename TDecay< PayloadTypes >::Type... InputPayload)
Definition Attribute.h:515
decltype(auto) MakeAttributeLambda(LambdaType &&InCallable, PayloadTypes &&... InputPayload)
Definition Attribute.h:545
UE_FORCEINLINE_HINT TAttribute< T > MakeAttributeSP(const SourceType *InObject, T(SourceTypeOrBase::*InMethod)(PayloadTypes...) const, typename TDecay< PayloadTypes >::Type... InputPayload)
Definition Attribute.h:524
UE_FORCEINLINE_HINT TAttribute< T > MakeAttributeUObject(const SourceType *InObject, T(SourceTypeOrBase::*InMethod)(PayloadTypes...) const, typename TDecay< PayloadTypes >::Type... InputPayload)
Definition Attribute.h:535
#define TEXT(x)
Definition Platform.h:1272
#define UE_FORCEINLINE_HINT
Definition Platform.h:723
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
return true
Definition ExternalRpcRegistry.cpp:601
typename TIdentity< T >::Type TIdentity_T
Definition Identity.h:24
ESPMode
Definition SharedPointerFwd.h:12
UE_INTRINSIC_CAST UE_REWRITE constexpr std::remove_reference_t< T > && MoveTemp(T &&Obj) noexcept
Definition UnrealTemplate.h:520
Definition NameTypes.h:617
Definition Attribute.h:17
const ObjectType & Get() const
Definition Attribute.h:241
static TAttribute Create(const FGetter &InGetter)
Definition Attribute.h:101
void BindRaw(SourceType *InUserObject, typename FGetter::template TConstMethodPtr< SourceType, std::decay_t< VarTypes >... > InMethodPtr, VarTypes &&... Vars)
Definition Attribute.h:317
TAttribute(ObjectType &&InInitialValue)
Definition Attribute.h:57
static UE_FORCEINLINE_HINT TAttribute CreateLambda(LambdaType &&InCallable, PayloadTypes &&... InputPayload)
Definition Attribute.h:155
bool IsSet() const
Definition Attribute.h:230
const FGetter & GetBinding() const
Definition Attribute.h:437
void Bind(const FGetter &InGetter)
Definition Attribute.h:274
void BindStatic(TIdentity_T< typename FGetter::template TFuncPtr< std::decay_t< VarTypes >... > > InFuncPtr, VarTypes &&... Vars)
Definition Attribute.h:302
static UE_FORCEINLINE_HINT TAttribute CreateSP(const SourceType *InObject, ObjectType(SourceTypeOrBase::*InMethod)(PayloadTypes...) const, typename TDecay< PayloadTypes >::Type... InputPayload)
Definition Attribute.h:145
void BindUObject(SourceType *InUserObject, typename FGetter::template TConstMethodPtr< SourceType, std::decay_t< VarTypes >... > InMethodPtr, VarTypes &&... Vars)
Definition Attribute.h:362
static TAttribute Create(FGetter &&InGetter)
Definition Attribute.h:112
void Bind(FGetter &&InGetter)
Definition Attribute.h:288
TAttribute(const OtherType &InInitialValue)
Definition Attribute.h:45
static UE_FORCEINLINE_HINT TAttribute CreateSPLambda(const TSharedRef< UserClass, Mode > &InUserObjectRef, LambdaType &&InCallable, PayloadTypes &&... InputPayload)
Definition Attribute.h:164
void Bind(SourceType *InUserObject, typename FGetter::template TConstMethodPtr< SourceType, std::decay_t< VarTypes >... > InMethodPtr, VarTypes &&... Vars)
Definition Attribute.h:347
static UE_FORCEINLINE_HINT TAttribute CreateRaw(const SourceType *InObject, ObjectType(SourceTypeOrBase::*InMethod)(PayloadTypes...) const, typename TDecay< PayloadTypes >::Type... InputPayload)
Definition Attribute.h:136
void BindUFunction(SourceType *InUserObject, const FName &InFunctionName)
Definition Attribute.h:377
const ObjectType & Get(const ObjectType &DefaultValue) const
Definition Attribute.h:262
void BindWeakLambda(UserClass *InUserObject, LambdaType &&InCallable, PayloadTypes &&... InputPayload)
Definition Attribute.h:416
TAttribute(SourceType *InUserObject, typename FGetter::template TConstMethodPtr< SourceType > InMethodPtr)
Definition Attribute.h:89
void Set(const OtherType &InNewValue)
Definition Attribute.h:210
void BindLambda(LambdaType &&InCallable, PayloadTypes &&... InputPayload)
Definition Attribute.h:388
void BindSPLambda(const UserClass *InUserObject, LambdaType &&InCallable, PayloadTypes &&... InputPayload)
Definition Attribute.h:405
void Bind(TSharedRef< SourceType > InUserObject, typename FGetter::template TConstMethodPtr< SourceType, std::decay_t< VarTypes >... > InMethodPtr, VarTypes &&... Vars)
Definition Attribute.h:332
static UE_FORCEINLINE_HINT TAttribute CreateSPLambda(UserClass *InUserObject, LambdaType &&InCallable, PayloadTypes &&... InputPayload)
Definition Attribute.h:169
static UE_FORCEINLINE_HINT TAttribute< ObjectType > Create(TFunction< ObjectType(void)> &&InLambda)
Definition Attribute.h:199
void BindSPLambda(const TSharedRef< UserClass, Mode > &InUserObjectRef, LambdaType &&InCallable, PayloadTypes &&... InputPayload)
Definition Attribute.h:399
TAttribute(TSharedRef< SourceType > InUserObject, typename FGetter::template TConstMethodPtr< SourceType > InMethodPtr)
Definition Attribute.h:73
friend class TAttribute
Definition Attribute.h:496
DECLARE_DELEGATE_RetVal(ObjectType, FGetter)
void Set(ObjectType &&InNewValue)
Definition Attribute.h:222
TVariant< ObjectType, FGetter > Steal()
Definition Attribute.h:447
TAttribute()
Definition Attribute.h:32
bool IsBound() const
Definition Attribute.h:427
static UE_FORCEINLINE_HINT TAttribute CreateWeakLambda(UserClass *InUserObject, LambdaType &&InCallable, PayloadTypes &&... InputPayload)
Definition Attribute.h:178
static TAttribute< ObjectType > Create(SourceType *InUserObject, const FName &InFunctionName)
Definition Attribute.h:192
bool IdenticalTo(const TAttribute &InOther) const
Definition Attribute.h:460
static TAttribute CreateStatic(FuncPtrType &&InFuncPtr, VarTypes... Vars)
Definition Attribute.h:126
Definition AndroidPlatformMisc.h:14
Definition SharedPointer.h:153
Definition TVariant.h:48
Mode
Definition AnimNode_TransitionPoseEvaluator.h:28
@ false
Definition radaudio_common.h:23
Definition Decay.h:44
Definition TVariant.h:13