UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
SlateAttributeMember.inl
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
7{
11 template<typename InObjectType, typename InInvalidationReasonPredicate, typename InComparePredicate>
12 struct TSlateMemberAttribute : public TSlateAttributeBase<SWidget, InObjectType, InInvalidationReasonPredicate, InComparePredicate, ESlateAttributeType::Member>
13 {
14 private:
16
18 static void VerifyAttributeAddress(const WidgetType& Widget, const TSlateMemberAttribute& Self)
19 {
20 checkf((UPTRINT)&Self >= (UPTRINT)&Widget && (UPTRINT)&Self < (UPTRINT)&Widget + sizeof(WidgetType),
21 TEXT("Use TAttribute or TSlateManagedAttribute instead. See SlateAttribute.h for more info."));
23 TEXT("The TSlateAttribute could not be found in the SlateAttributeDescriptor.\n")
24 TEXT("Use the SLATE_DECLARE_WIDGET and add the attribute in PrivateRegisterAttributes,\n")
25 TEXT("Or use TSlateAttribute with a valid Invalidation Reason instead."));
26 }
27
28 public:
29 using FGetter = typename Super::FGetter;
30 using ObjectType = typename Super::ObjectType;
31
32 public:
33 //~ You can only register Attribute that are defined in a SWidget (member of the class).
34 //~ Use the constructor with Widget pointer.
35 //~ See documentation in SlateAttribute.h for more info.
41 void* operator new(size_t) = delete;
42 void* operator new[](size_t) = delete;
43
44#if UE_SLATE_WITH_MEMBER_ATTRIBUTE_DEBUGGING
46 {
53 checkf(Super::ProtectedIsWidgetInDestructionPath(Super::Debug_OwningWidget), TEXT("The Owning widget should be invalid."));
54 }
55#endif
56
58 explicit TSlateMemberAttribute(WidgetType& Widget)
59 : Super(Widget)
60 {
61 VerifyAttributeAddress(Widget, *this);
62 }
63
65 explicit TSlateMemberAttribute(WidgetType& Widget, const ObjectType& InValue)
67 {
68 VerifyAttributeAddress(Widget, *this);
69 }
70
74 {
75 VerifyAttributeAddress(Widget, *this);
76 }
77 };
78
79
80
82
83 /*
84 * A reference to a SlateAttribute that can be returned and saved for later.
85 */
86 template<typename AttributeMemberType>
88 {
89 public:
91 using ObjectType = typename AttributeMemberType::ObjectType;
93
94 private:
95 template<typename WidgetType>
96 static void VerifyAttributeAddress(const WidgetType& InWidget, const AttributeMemberType& InAttribute)
97 {
98 checkf((UPTRINT)&InAttribute >= (UPTRINT)&InWidget && (UPTRINT)&InAttribute < (UPTRINT)&InWidget + sizeof(WidgetType),
99 TEXT("The attribute is not a member of the widget."));
100 InAttribute.VerifyOwningWidget(InWidget);
101 }
102
103 public:
107 : Owner(InOwner)
108 , Attribute(InAttribute)
109 {
110 VerifyAttributeAddress(InOwner.Get(), InAttribute);
111 }
112
119
120 public:
122 [[nodiscard]] bool IsValid() const
123 {
124 return Owner.IsValid();
125 }
126
128 [[nodiscard]] const ObjectType& Get() const
129 {
130 if (TSharedPtr<const SWidget> Pin = Owner.Pin())
131 {
132 return Attribute.Get();
133 }
134 checkf(false, TEXT("It is an error to call GetValue() on an unset TSlateMemberAttributeRef. Please either check IsValid() or use Get(DefaultValue) instead."));
135 static ObjectType Tmp;
136 return Tmp;
137 }
138
140 [[nodiscard]] const ObjectType& Get(const ObjectType& DefaultValue) const
141 {
142 if (TSharedPtr<const SWidget> Pin = Owner.Pin())
143 {
144 return Attribute.Get();
145 }
146 return DefaultValue;
147 }
148
151 {
152 if (TSharedPtr<const SWidget> Pin = Owner.Pin())
153 {
154 const_cast<AttributeMemberType&>(Attribute).UpdateNow(const_cast<SWidget&>(*Pin.Get()));
155 }
156 }
157
163 {
164 if (TSharedPtr<const SWidget> Pin = Owner.Pin())
165 {
166 const_cast<AttributeMemberType&>(Attribute)->UpdateNow(const_cast<SWidget&>(*Pin.Get()));
167 return Attribute.Get();
168 }
169 checkf(false, TEXT("It is an error to call GetValue() on an unset TSlateMemberAttributeRef. Please either check IsValid() or use Get(DefaultValue) instead."));
170 }
171
176 [[nodiscard]] const ObjectType& UpdateAndGet(const ObjectType& DefaultValue)
177 {
178 if (TSharedPtr<const SWidget> Pin = Owner.Pin())
179 {
180 const_cast<AttributeMemberType&>(Attribute)->UpdateNow(const_cast<SWidget&>(*Pin.Get()));
181 return Attribute.Get();
182 }
183 return DefaultValue;
184 }
185
188 {
189 if (TSharedPtr<const SWidget> Pin = Owner.Pin())
190 {
191 return Attribute.ToAttribute(*Pin.Get());
192 }
193 return TAttribute<ObjectType>();
194 }
195
197 [[nodiscard]] bool IsBound() const
198 {
199 if (TSharedPtr<const SWidget> Pin = Owner.Pin())
200 {
201 return Attribute.IsBound(*Pin.Get());
202 }
203 return false;
204 }
205
208 {
211 if (SelfPin == OtherPin && SelfPin)
212 {
213 return Attribute.IdenticalTo(*SelfPin.Get(), Other.Attribute);
214 }
215 return SelfPin == OtherPin;
216 }
217
220 {
221 if (TSharedPtr<const SWidget> Pin = Owner.Pin())
222 {
223 return Attribute.IdenticalTo(*Pin.Get(), Other);
224 }
225 return !Other.IsSet(); // if the other is not set, then both are invalid.
226 }
227
228 private:
230 const AttributeMemberType& Attribute;
231 };
232
233} // SlateAttributePrivate
#define ensureAlwaysMsgf(InExpression, InFormat,...)
Definition AssertionMacros.h:467
#define checkf(expr, format,...)
Definition AssertionMacros.h:315
return Self
Definition CocoaThread.cpp:337
#define TEXT(x)
Definition Platform.h:1272
FPlatformTypes::UPTRINT UPTRINT
An unsigned integer the same size as a pointer.
Definition Platform.h:1146
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
UE_INTRINSIC_CAST UE_REWRITE constexpr std::remove_reference_t< T > && MoveTemp(T &&Obj) noexcept
Definition UnrealTemplate.h:520
Definition SWidget.h:165
Definition Attribute.h:17
Definition SharedPointer.h:692
Definition SharedPointer.h:153
Definition SharedPointer.h:1295
UE_FORCEINLINE_HINT TSharedPtr< ObjectType, Mode > Pin() const &
Definition SharedPointer.h:1512
UE_FORCEINLINE_HINT bool IsValid() const
Definition SharedPointer.h:1535
Definition SlateAttribute.cpp:11
ESlateMemberAttributeRefNoCheckParam
Definition SlateAttributeMember.inl:81
@ SlateMemberAttributeRefNoCheckParam
Definition SlateAttributeMember.inl:81
SLATECORE_API bool ProtectedIsWidgetInDestructionPath(SWidget *Widget) const
Definition SlateAttribute.cpp:50
Definition SlateAttributeBase.inl:20
Definition SlateAttributeMember.inl:88
bool IdenticalTo(const TSlateMemberAttributeRef &Other) const
Definition SlateAttributeMember.inl:207
TSlateMemberAttributeRef(const TSharedRef< WidgetType > &InOwner, const AttributeMemberType &InAttribute)
Definition SlateAttributeMember.inl:106
AttributeMemberType SlateAttributeType
Definition SlateAttributeMember.inl:90
const ObjectType & Get(const ObjectType &DefaultValue) const
Definition SlateAttributeMember.inl:140
TSlateMemberAttributeRef(ESlateMemberAttributeRefNoCheckParam, const TSharedRef< SWidget > &InOwner, const AttributeMemberType &InAttribute)
Definition SlateAttributeMember.inl:114
void UpdateValue()
Definition SlateAttributeMember.inl:150
const ObjectType & Get() const
Definition SlateAttributeMember.inl:128
const ObjectType & UpdateAndGet(const ObjectType &DefaultValue)
Definition SlateAttributeMember.inl:176
TAttribute< ObjectType > ToAttribute() const
Definition SlateAttributeMember.inl:187
bool IsBound() const
Definition SlateAttributeMember.inl:197
typename AttributeMemberType::ObjectType ObjectType
Definition SlateAttributeMember.inl:91
bool IdenticalTo(const TAttribute< ObjectType > &Other) const
Definition SlateAttributeMember.inl:219
const ObjectType & UpdateAndGet()
Definition SlateAttributeMember.inl:162
bool IsValid() const
Definition SlateAttributeMember.inl:122
Definition SlateAttributeMember.inl:13
TSlateMemberAttribute & operator=(const TSlateMemberAttribute &)=delete
TSlateMemberAttribute & operator=(TSlateMemberAttribute &&)=delete
TSlateMemberAttribute(WidgetType &Widget, ObjectType &&InValue)
Definition SlateAttributeMember.inl:72
typename Super::FGetter FGetter
Definition SlateAttributeMember.inl:29
TSlateMemberAttribute(const TSlateMemberAttribute &)=delete
TSlateMemberAttribute(TSlateMemberAttribute &&)=delete
typename Super::ObjectType ObjectType
Definition SlateAttributeMember.inl:30
TSlateMemberAttribute(WidgetType &Widget)
Definition SlateAttributeMember.inl:58
TSlateMemberAttribute(WidgetType &Widget, const ObjectType &InValue)
Definition SlateAttributeMember.inl:65