UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
ManagedArrayCollection.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
5#include "CoreMinimal.h"
8#include "UObject/Object.h"
11
12#include "ManagedArrayCollection.generated.h"
13
14class FSimulationProperties;
15
16namespace Chaos
17{
18 class FChaosArchive;
19}
20
22{
25
27 {
28 return ((AttributeName == Other.AttributeName) && (GroupName == Other.GroupName));
29 }
30};
31
54USTRUCT()
56{
58 friend FSimulationProperties;
59
60public:
61
62
65
70
71 CHAOS_API bool operator==(const FManagedArrayCollection& Other) const; // Slow, required for comparing to a default collection during UPROPERTY serialization
72
91
93 {
94 FProcessingParameters() : bDoValidation(true), bReindexDependentAttibutes(true) {}
95
98 };
99
108
109 template<class T>
111 {
114 };
115
119 static FName StaticType() { return FName("FManagedArrayCollection"); }
120
121 virtual bool IsAType(FName InTypeName) const { return InTypeName.IsEqual(FManagedArrayCollection::StaticType()); }
122
123 template<typename T>
124 bool IsA() { return IsAType(T::StaticType()); }
125
126 template<class T>
127 T* Cast() { return IsAType(T::StaticType())?static_cast<T*>(this):nullptr; }
128
129 template<class T>
130 const T* Cast() const { return IsAType(T::StaticType())? static_cast<const T*>(this) : nullptr; }
131
138 template<typename T>
140 {
141 if (!HasAttribute(Name, Group))
142 {
143 AddNewAttributeImpl<T>(Name, Group, Parameters);
144 }
146 }
147
154 template<typename T>
163
164
169 template<class T = FManagedArrayCollection>
170 T* NewCopy() const
171 {
172 T* Collection = new T();
173 for (const FName& GroupName : Collection->GroupNames())
174 {
175 Collection->EmptyGroup(GroupName); //Geometry collection has 1 element in ConvexProperties group when initialized
176 }
177 CopyTo(Collection);
178 return Collection;
179 }
180
182 {
183 if (!Map.IsEmpty())
184 {
185 for (const TTuple<FKeyType, FValueType>& Entry : Map)
186 {
187 const FName& AttributeName = Entry.Key.Get<0>();
188 const FName& GroupName = Entry.Key.Get<1>();
189 if (AttributesToSkip.Num() > 0)
190 {
191 const TTuple<FName, FName> GroupAndAttribute{ AttributeName, GroupName };
193 {
194 continue;
195 }
196 }
197 if (!GroupsToSkip.Contains(GroupName))
198 {
199 if (!Collection->HasGroup(GroupName))
200 {
201 Collection->AddGroup(GroupName);
202 }
203
204 if (NumElements(GroupName) != Collection->NumElements(GroupName))
205 {
206 ensure(!Collection->NumElements(GroupName));
207 Collection->AddElements(NumElements(GroupName), GroupName);
208 }
209
210 Collection->CopyAttribute(*this, AttributeName, GroupName);
211 }
212 }
213 }
214 }
215
222 template<typename T>
224 {
225 check(!HasAttribute(Name, Group));
226
227 if (!HasGroup(Group))
228 {
229 AddGroup(Group);
230 }
231
232 const int32 InitialSize = NumElements(Group);
233 FName GroupIndexDependency;
234 if (ensure(Parameters.bAllowCircularDependency || !IsConnected(Parameters.GroupIndexDependency, Group)))
235 {
236 GroupIndexDependency = Parameters.GroupIndexDependency;
237 }
238 else
239 {
240 GroupIndexDependency = NAME_None;
241 }
242
243 FValueType NewAttribute = FValueType::MakeExternal<T>(&ValueIn, NumElements(Group), GroupIndexDependency, Parameters.Saved);
244 Map.Add({ Name, Group }, MoveTemp(NewAttribute));
245 }
246
251 CHAOS_API void AddGroup(FName Group);
252
256 CHAOS_API int32 NumAttributes(FName Group) const;
257
262 CHAOS_API TArray<FName> AttributeNames(FName Group) const;
263
271
280
286
293 template<typename T>
295 {
296 const FKeyType Key = FManagedArrayCollection::MakeMapKey(Name, Group);
297 if (FValueType* FoundValue = Map.Find(Key))
298 {
299 return FoundValue->ModifyTypedPtr<T>();
300 }
301 return nullptr;
302 };
303
304 template<typename T>
306 {
307 if (const FValueType* FoundValue = Map.Find({ Name, Group }))
308 {
309 return FoundValue->GetTypedPtr<T>();
310 }
311 return nullptr;
312 };
313
320 template<typename T>
322 {
323 if (FValueType* FoundValue = Map.Find({ Name, Group }))
324 {
325 if(FoundValue->IsSameType<T>())
326 {
327 return FoundValue->ModifyTypedPtr<T>();
328 }
329 }
330 return nullptr;
331 };
332
333 template<typename T>
335 {
336 if (const FValueType* FoundValue = Map.Find({ Name, Group }))
337 {
338 if(FoundValue->IsSameType<T>())
339 {
340 return FoundValue->GetTypedPtr<T>();
341 }
342 }
343 return nullptr;
344 };
345
353 template<typename T>
355 {
356 check(HasAttribute(Name, Group))
357 return Map[{Name, Group}].ModifyTyped<T>();
358 }
359
367 template<typename T>
369 {
370 if (FValueType* FoundValue = Map.Find({ Name, Group }))
371 {
372 if (FoundValue->IsSameType<T>())
373 {
374 return FoundValue->ModifyTypedPtr<T>();
375 }
376 }
377 return nullptr;
378 }
379
386 template<typename T>
388 {
389 check(HasAttribute(Name, Group));
390 return Map[{Name, Group}].GetTyped<T>();
391 };
392
396 virtual void Reset() { Map.Reset(); GroupInfo.Reset(); MakeDirty(); }
397
398
402 CHAOS_API virtual void RemoveElements(const FName & Group, const TArray<int32> & SortedDeletionList, FProcessingParameters Params = FProcessingParameters());
403
408 CHAOS_API virtual void MergeElements(const FName& Group, const TArray<int32>& SortedMergeList, const TArray<int32>& MergeRemapIndex, FProcessingParameters Params = FProcessingParameters());
409
416 CHAOS_API virtual void RemoveElements(const FName& Group, int32 NumberElements, int32 Position);
417
418
424 CHAOS_API void RemoveAttribute(FName Name, FName Group);
425
426
431 CHAOS_API void RemoveGroup(FName Group);
432
436 CHAOS_API TArray<FName> GroupNames() const;
437
441 bool IsEmpty() const
442 {
443 return GroupInfo.IsEmpty() && Map.IsEmpty();
444 }
445
451 CHAOS_API bool HasAttribute(FName Name, FName Group) const;
452
457 CHAOS_API bool HasAttributes(const TArray<FManagedType>& Types) const;
458
463 FORCEINLINE bool HasGroup(FName Group) const { return GroupInfo.Contains(Group); }
464
470 CHAOS_API EArrayType GetAttributeType(FName Name, FName Group) const;
471
477 CHAOS_API bool IsAttributeDirty(FName Name, FName Group) const;
478
485 CHAOS_API bool IsAttributePersistent(FName Name, FName Group) const;
486
490 CHAOS_API void SetDependency(FName Name, FName Group, FName DependencyGroup, bool bAllowCircularDependency = false);
491
497 CHAOS_API FName GetDependency(FName Name, FName Group) const;
498
502 CHAOS_API void RemoveDependencyFor(FName Group);
503
510
517 CHAOS_API void CopyAttribute(const FManagedArrayCollection& InCollection, FName SrcName, FName DestName, FName Group);
518
527
533 CHAOS_API void CopyMatchingAttributesFrom(const FManagedArrayCollection& InCollection, const TMap<FName, TSet<FName>>* SkipList=nullptr);
534
545
550 CHAOS_API int32 NumElements(FName Group) const;
551
557 CHAOS_API void Resize(int32 Size, FName Group);
558
564 CHAOS_API void Reserve(int32 Size, FName Group);
565
570 CHAOS_API void EmptyGroup(FName Group);
571
575 CHAOS_API virtual void ReorderElements(FName Group, const TArray<int32>& NewOrder);
576#if 0 //not needed until we support per instance serialization
583 CHAOS_API void SwapElements(int32 Index1, int32 Index2, FName Group);
584#endif
585
589 void MakeDirty() { bDirty = true; }
590 void MakeClean() { bDirty = false; }
591 bool IsDirty() const { return bDirty; }
592
596 CHAOS_API virtual void Serialize(Chaos::FChaosArchive& Ar);
597
598 CHAOS_API bool Serialize(FArchive& Ar);
599
604 CHAOS_API bool IsConnected(FName StartingNode, FName TargetNode);
605
606
610 CHAOS_API FString ToString() const;
611
615 CHAOS_API SIZE_T GetAllocatedSize() const;
616
620 CHAOS_API void GetElementSizeInfoForGroups(TArray<TPair<FName, SIZE_T>>& OutSizeInfo) const;
621
622private:
623
624 template<typename T>
625 void AddNewAttributeImpl(FName Name, FName Group, const FConstructionParameters& Parameters)
626 {
627 checkSlow(!HasAttribute(Name, Group));
628 if (!HasGroup(Group))
629 {
630 AddGroup(Group);
631 }
632
633 static const FName EmptyName = NAME_None;
634
635 const int32 InitialSize = NumElements(Group);
636 FName GroupIndexDependency;
637 if (ensure(Parameters.bAllowCircularDependency || !IsConnected(Parameters.GroupIndexDependency, Group)))
638 {
639 GroupIndexDependency = Parameters.GroupIndexDependency;
640 }
641 else
642 {
643 GroupIndexDependency = EmptyName;
644 }
645
647 FValueType NewAttribute = FValueType::MakeManaged(MoveTemp(ArrayPtr), InitialSize, GroupIndexDependency, Parameters.Saved);
648 Map.Add({ Name, Group }, MoveTemp(NewAttribute));
649 }
650
658 TArray<int32> InsertElementsNoReorder(int32 NumberElements, int32 Position, FName Group);
659
660 /****
661 * Mapping Key/Value
662 *
663 * The Key and Value pairs for the attribute arrays allow for grouping
664 * of array attributes based on the Name,Group pairs. Array attributes
665 * that share Group names will have the same lengths.
666 *
667 * The FKeyType is a tuple of <FName,FName> where the Get<0>() parameter
668 * is the AttributeName, and the Get<1>() parameter is the GroupName.
669 * Construction of the FKeyType will follow the following pattern:
670 * FKeyType("AttributeName","GroupName");
671 *
672 */
673 typedef TTuple<FName, FName> FKeyType;
674 struct FGroupInfo
675 {
676 int32 Size;
677 };
678
679 static inline FKeyType MakeMapKey(FName Name, FName Group) // inline for linking issues
680 {
681 return FKeyType(Name, Group);
682 }
683
685 struct FValueType
686 {
687 private:
688 EArrayType ArrayType;
689 FName GroupIndexDependency;
690 bool bPersistent;
691 bool bExternalValue; //External arrays have external memory management.
692
693 TSharedPtr<FManagedArrayBase, ESPMode::ThreadSafe> SharedManagedArray; // external arrays have this set this to null
694 FManagedArrayBase* ManagedArray = nullptr;;
695
696 private:
701 template<typename T>
702 FValueType(TUniquePtr<TManagedArray<T>>&& ArrayPtr, int32 InitialSize, FName InGroupIndexDependency, bool bInPersistent)
703 : ArrayType(ManagedArrayType<T>())
704 , GroupIndexDependency(InGroupIndexDependency)
705 , bPersistent(bInPersistent)
706 , bExternalValue(false)
707 , SharedManagedArray(ArrayPtr.Release())
708 , ManagedArray(SharedManagedArray.Get())
709 {
710 MakeUniqueForWrite();
711 ManagedArray->Reserve(InitialSize); // make sure we have the exact size to avoid over-allocation
712 ManagedArray->Resize(InitialSize);
713 };
714
719 template<typename T>
721 : ArrayType(ManagedArrayType<T>())
722 , GroupIndexDependency(InGroupIndexDependency)
723 , bPersistent(bInPersistent)
724 , bExternalValue(true)
725 , SharedManagedArray(nullptr)
726 , ManagedArray(ExternalArrayPtr)
727 {
728 ManagedArray->Reserve(InitialSize); // make sure we have the exact size to avoid over-allocation
729 ManagedArray->Resize(InitialSize);
730 };
731
732 CHAOS_API void MakeUniqueForWrite();
733
734 public:
739 template <typename T>
740 static FValueType MakeManaged(TUniquePtr<TManagedArray<T>>&& ArrayPtr, int32 InitialSize, FName InGroupIndexDependency, bool bInPersistent)
741 {
742 return FValueType(MoveTemp(ArrayPtr), InitialSize, InGroupIndexDependency, bInPersistent);
743 }
744
745 template <typename T>
746 static FValueType MakeExternal(TManagedArray<T>* ExternalArrayPtr, int32 InitialSize, FName InGroupIndexDependency, bool bInPersistent)
747 {
748 return FValueType(ExternalArrayPtr, InitialSize, InGroupIndexDependency, bInPersistent);
749 }
750
751 CHAOS_API FValueType();
752 CHAOS_API FValueType(const FValueType& Other);
753 CHAOS_API FValueType(FValueType&& Other);
754
755 CHAOS_API ~FValueType();
756
757 EArrayType GetArrayType() const { return ArrayType; }
758 FName GetGroupIndexDependency() const { return GroupIndexDependency; };
759 bool IsPersistent() const { return bPersistent; }
760 bool IsExternal() const { return bExternalValue; }
761 bool IsDirty() const { return ManagedArray->IsDirty(); }
762
763 void SetGroupIndexDependency(FName NewGroupDependency) { GroupIndexDependency = NewGroupDependency; }
764
765 template <typename T>
766 bool IsSameType() const { return (ArrayType == ManagedArrayType<T>()); }
767
768 template <typename T>
769 const TManagedArray<T>* GetTypedPtr() const
770 {
772
773 const TManagedArray<T>* TypedManagedArray = static_cast<const TManagedArray<T>*>(ManagedArray);
774 check(TypedManagedArray != nullptr);
775 return TypedManagedArray;
776 }
777
778 template <typename T>
779 const TManagedArray<T>& GetTyped() const
780 {
781 return *GetTypedPtr<T>();
782 }
783
784 const FManagedArrayBase& Get() const { return *ManagedArray; }
785
786 template <typename T>
787 TManagedArray<T>* ModifyTypedPtr()
788 {
790 MakeUniqueForWrite();
791
792 TManagedArray<T>* TypedManagedArray = static_cast<TManagedArray<T>*>(ManagedArray);
793 check(TypedManagedArray != nullptr);
794
795 TypedManagedArray->MarkDirty();
796 return TypedManagedArray;
797 }
798
799 template <typename T>
800 TManagedArray<T>& ModifyTyped()
801 {
802 return *ModifyTypedPtr<T>();
803 }
804
805 FManagedArrayBase& Modify();
806
807 void Reserve(int32 ReservedSize);
808 void Resize(int32 NewSize);
809 void InitFrom(const FValueType& Other);
810 void Exchange(FValueType& Other);
811 void Convert(FValueType& Other);
812 void CopyFrom(const FValueType& Other);
813 void Empty();
814 void RemoveGroupIndexDependency(FName Group);
815
816 FValueType& operator=(const FValueType& Other) = delete;
817 FValueType& operator=(FValueType&& Other) = delete;
818
819 void Serialize(FArchive& Ar);
820 };
821
822
823 TMap< FKeyType, FValueType> Map; //data is owned by the map explicitly
824 TMap< FName, FGroupInfo> GroupInfo;
825 bool bDirty;
826
827protected:
828
829 CHAOS_API virtual void SetDefaults(FName Group, uint32 StartSize, uint32 NumElements);
830
837
843 CHAOS_API void SyncGroupSizeFrom(const FManagedArrayCollection& InCollection, FName Group);
844
852
857
858
862 friend FArchive& operator<<(FArchive& Ar, FGroupInfo& GroupInfo);
863
867 friend FArchive& operator<<(FArchive& Ar, FValueType& ValueIn);
868
869};
870
871template<> struct TStructOpsTypeTraits<FManagedArrayCollection> : public TStructOpsTypeTraitsBase2<FManagedArrayCollection>
872{
873 enum
874 {
876 WithIdenticalViaEquality = true // Required for usage as a UPROPERTY, otherwise the reflection system won't see a difference with a default collection when serializing
877 };
878};
879
880/*
881* FManagedArrayInterface
882*/
898
899
900#define MANAGED_ARRAY_COLLECTION_INTERNAL(TYPE_NAME) \
901 static FName StaticType() { return FName(#TYPE_NAME); } \
902 virtual bool IsAType(FName InTypeName) const override { \
903 return InTypeName.IsEqual(TYPE_NAME::StaticType()) \
904 || Super::IsAType(InTypeName); \
905 }
#define FORCEINLINE
Definition AndroidPlatform.h:140
#define checkSlow(expr)
Definition AssertionMacros.h:332
#define check(expr)
Definition AssertionMacros.h:314
#define ensure( InExpression)
Definition AssertionMacros.h:464
FPlatformTypes::int8 int8
An 8-bit signed integer.
Definition Platform.h:1121
FPlatformTypes::SIZE_T SIZE_T
An unsigned integer the same size as a pointer, the same as UPTRINT.
Definition Platform.h:1150
FPlatformTypes::int32 int32
A 32-bit signed integer.
Definition Platform.h:1125
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
FArchive & operator<<(FArchive &Ar, FEnvQueryDebugProfileData::FStep &Data)
Definition EnvQueryTypes.cpp:489
return true
Definition ExternalRpcRegistry.cpp:601
EManagedArrayType
Definition ManagedArrayTypes.h:67
EManagedArrayType ManagedArrayType()
#define USTRUCT(...)
Definition ObjectMacros.h:746
#define GENERATED_USTRUCT_BODY(...)
Definition ObjectMacros.h:767
UE_REWRITE constexpr void Exchange(T &A, T &B)
Definition UnrealTemplate.h:627
UE_INTRINSIC_CAST UE_REWRITE constexpr std::remove_reference_t< T > && MoveTemp(T &&Obj) noexcept
Definition UnrealTemplate.h:520
uint32 Size
Definition VulkanMemory.cpp:4034
uint32_t uint32
Definition binka_ue_file_header.h:6
Definition ChaosArchive.h:167
Definition Archive.h:1208
Definition ManagedArray.h:154
FORCEINLINE bool IsDirty() const
Definition ManagedArray.h:225
virtual void Resize(const int32 Num)
Definition ManagedArray.h:161
virtual void Reserve(const int32 Num)
Definition ManagedArray.h:167
Definition ManagedArrayCollection.h:884
FManagedArrayCollection * ManagedCollection
Definition ManagedArrayCollection.h:895
virtual void InitializeInterface()=0
FManagedArrayInterface(FManagedArrayCollection *InManagedArray)
Definition ManagedArrayCollection.h:887
FManagedArrayInterface()
Definition ManagedArrayCollection.h:886
virtual void RemoveInterfaceAttributes()=0
virtual void CleanInterfaceForCook()=0
Definition NameTypes.h:617
Definition ArrayView.h:139
Definition Array.h:670
Definition ManagedArray.h:1099
Definition UnrealString.h.inl:34
Definition SharedPointer.h:692
Definition UniquePtr.h:107
Definition SkeletalMeshComponent.h:307
FORCEINLINE T * Get(const FObjectPtr &ObjectPtr)
Definition ObjectPtr.h:426
UTF8CHAR * Convert(UTF8CHAR *Dest, int32 DestLen, const WIDECHAR *Src)
Definition GenericPlatformString.cpp:595
@ false
Definition radaudio_common.h:23
Definition ManagedArrayCollection.h:22
bool operator==(const FAttributeAndGroupId &Other) const
Definition ManagedArrayCollection.h:26
FName AttributeName
Definition ManagedArrayCollection.h:23
FName GroupName
Definition ManagedArrayCollection.h:24
Definition ManagedArrayCollection.cpp:745
Definition ManagedArrayCollection.h:78
bool Saved
Definition ManagedArrayCollection.h:88
bool bAllowCircularDependency
Definition ManagedArrayCollection.h:89
FConstructionParameters(FName GroupIndexDependencyIn=NAME_None, bool SavedIn=true, bool bInAllowCircularDependency=false)
Definition ManagedArrayCollection.h:80
FName GroupIndexDependency
Definition ManagedArrayCollection.h:87
Definition ManagedArrayCollection.h:101
FManagedType(EManagedArrayType InType, FName InName, FName InGroup)
Definition ManagedArrayCollection.h:102
Definition ManagedArrayCollection.h:93
bool bDoValidation
Definition ManagedArrayCollection.h:96
bool bReindexDependentAttibutes
Definition ManagedArrayCollection.h:97
FProcessingParameters()
Definition ManagedArrayCollection.h:94
Definition ManagedArrayCollection.h:111
TManagedType(FName InName, FName InGroup)
Definition ManagedArrayCollection.h:112
Definition ManagedArrayCollection.h:56
void AddExternalAttribute(FName Name, FName Group, TManagedArray< T > &ValueIn, FConstructionParameters Parameters=FConstructionParameters())
Definition ManagedArrayCollection.h:223
virtual void Reset()
Definition ManagedArrayCollection.h:396
void MakeClean()
Definition ManagedArrayCollection.h:590
void MakeDirty()
Definition ManagedArrayCollection.h:589
T * NewCopy() const
Definition ManagedArrayCollection.h:170
const T * Cast() const
Definition ManagedArrayCollection.h:130
bool IsDirty() const
Definition ManagedArrayCollection.h:591
TManagedArray< T > * ModifyAttributeTyped(FName Name, FName Group)
Definition ManagedArrayCollection.h:368
TManagedArray< T > & ModifyAttribute(FName Name, FName Group)
Definition ManagedArrayCollection.h:354
int32 Version
Definition ManagedArrayCollection.h:856
TManagedArray< T > * FindAttributeTyped(FName Name, FName Group)
Definition ManagedArrayCollection.h:321
T * Cast()
Definition ManagedArrayCollection.h:127
void CopyTo(FManagedArrayCollection *Collection, const TArray< FName > &GroupsToSkip=TArray< FName >(), TArray< TTuple< FName, FName > > AttributesToSkip=TArray< TTuple< FName, FName > >()) const
Definition ManagedArrayCollection.h:181
virtual void MatchOptionalDefaultAttributes(const FManagedArrayCollection &InCollection)
Definition ManagedArrayCollection.h:835
const TManagedArray< T > & GetAttribute(FName Name, FName Group) const
Definition ManagedArrayCollection.h:387
virtual bool IsAType(FName InTypeName) const
Definition ManagedArrayCollection.h:121
static FName StaticType()
Definition ManagedArrayCollection.h:119
bool IsA()
Definition ManagedArrayCollection.h:124
bool IsEmpty() const
Definition ManagedArrayCollection.h:441
const TManagedArray< T > * FindAttributeTyped(FName Name, FName Group) const
Definition ManagedArrayCollection.h:334
TManagedArray< T > * FindOrAddAttributeTyped(FName Name, FName Group, FConstructionParameters Parameters=FConstructionParameters())
Definition ManagedArrayCollection.h:155
const TManagedArray< T > * FindAttribute(FName Name, FName Group) const
Definition ManagedArrayCollection.h:305
TManagedArray< T > & AddAttribute(FName Name, FName Group, FConstructionParameters Parameters=FConstructionParameters())
Definition ManagedArrayCollection.h:139
TManagedArray< T > * FindAttribute(FName Name, FName Group)
Definition ManagedArrayCollection.h:294
FORCEINLINE bool HasGroup(FName Group) const
Definition ManagedArrayCollection.h:463
Definition StructOpsTypeTraits.h:11
@ WithIdenticalViaEquality
Definition StructOpsTypeTraits.h:18
@ WithSerializer
Definition StructOpsTypeTraits.h:23
Definition StructOpsTypeTraits.h:46
Definition Tuple.h:652