UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
NetBitArrayPrinter.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
7
8namespace UE::Net
9{
10
16{
17public:
18
20 template <class T>
21 static FString PrintSetSummary(const T& BitArray);
22
24 template <class T>
25 static FString PrintZeroSummary(const T& BitArray);
26
28 template <class T>
29 static FString PrintSetBits(const T& BitArray);
30
32 template <class T>
33 static FString PrintZeroBits(const T& BitArray);
34
36 template <class T, class U>
37 static FString PrintDeltaSummary(const T& BitArrayA, const U& BitArrayB);
38
40 template <class T, class U>
41 static FString PrintDeltaBits(const T& BitArrayA, const U& BitArrayB);
42
43private:
44
45 FNetBitArrayPrinter() = delete;
46 ~FNetBitArrayPrinter() = delete;
47};
48
49
50//*************************************************************************************************
51// FNetBitArrayPrinter
52// Implementation
53//*************************************************************************************************
54
55template <class T>
56FString FNetBitArrayPrinter::PrintSetSummary(const T& BitArray)
57{
58 return FString::Printf(TEXT("%u bits set"), BitArray.CountSetBits());
59}
60
61template <class T>
62FString FNetBitArrayPrinter::PrintZeroSummary(const T& BitArray)
63{
64 return FString::Printf(TEXT("%u bits zero"), BitArray.GetNumBits() - BitArray.CountSetBits());
65}
66
67template <class T>
68FString FNetBitArrayPrinter::PrintSetBits(const T& BitArray)
69{
70 using WordType = typename T::StorageWordType;
71
72 FString Buffer;
73 Buffer.Reserve(1024);
74
75 const uint32 BitEndIndex = BitArray.GetNumBits() - 1U;
76 const uint32 WordEndIndex = BitEndIndex / T::WordBitCount;
77 const WordType EndWordMask = (~WordType(0) >> (~BitEndIndex & (T::WordBitCount - 1U)));
78
79 const WordType* BitWordData = BitArray.GetData();
80
82 for (uint32 WordIndex = 0; WordIndex < BitArray.GetNumWords(); ++WordIndex)
83 {
84 const bool bIsEndWord = (WordIndex == WordEndIndex);
85 const WordType WordMask = bIsEndWord ? EndWordMask : ~WordType(0);
86
87 const WordType BitWord = BitWordData[WordIndex];
88
89 const int32 NumSetBits = FPlatformMath::CountBits(BitWord & WordMask);
90
91 if (NumSetBits > 0)
92 {
94
95 Buffer += FString::Printf(TEXT(" (0x%.2x)[%#.8x]"), WordIndex, BitWord);
96 }
97 }
98
99 if (Buffer.IsEmpty())
100 {
101 Buffer = TEXT(" none");
102 }
103
104 return FString::Printf(TEXT("%d bits set:%s"), TotalSetBits, *Buffer);
105}
106
107template <class T>
108FString FNetBitArrayPrinter::PrintZeroBits(const T& BitArray)
109{
110 using WordType = typename T::StorageWordType;
111
112 FString Buffer;
113 Buffer.Reserve(1024);
114
115 const uint32 BitEndIndex = BitArray.GetNumBits() - 1U;
116 const uint32 WordEndIndex = BitEndIndex / T::WordBitCount;
117 const WordType EndWordMask = (~WordType(0) >> (~BitEndIndex & (T::WordBitCount - 1U)));
118
119 const WordType* BitWordData = BitArray.GetData();
120
122 for (uint32 WordIndex = 0; WordIndex < BitArray.GetNumWords(); ++WordIndex)
123 {
124 const bool bIsEndWord = (WordIndex == WordEndIndex);
125 const WordType WordMask = bIsEndWord ? EndWordMask : ~WordType(0);
126
127 const WordType BitWord = BitWordData[WordIndex];
128 const WordType FlipBitWord = ~(BitWord & WordMask);
129
130 const int32 NumZeroBits = FPlatformMath::CountBits(FlipBitWord);
131
132 if (NumZeroBits > 0)
133 {
135
136 Buffer += FString::Printf(TEXT(" (0x%.2x)[%#.8x]"), WordIndex, BitWord);
137 }
138 }
139
140 if (Buffer.IsEmpty())
141 {
142 Buffer = TEXT(" none");
143 }
144
145 return FString::Printf(TEXT("%d bits zero:%s"), TotalZeroBits, *Buffer);
146}
147
148template <class T, class U>
150{
151 using WordTypeT = typename T::StorageWordType;
152 using WordTypeU = typename U::StorageWordType;
154
155 const WordTypeT* BitWordDataA = BitArrayA.GetData();
156 const WordTypeU* BitWordDataB = BitArrayB.GetData();
157
158 int32 DiffNumBits = 0;
159 for (uint32 WordIndex = 0; WordIndex < BitArrayA.GetNumWords(); ++WordIndex)
160 {
161 const WordTypeT BitWordA = BitWordDataA[WordIndex];
162 const WordTypeU BitWordB = BitWordDataB[WordIndex];
163
164 // XOR both words
166
167 DiffNumBits += FPlatformMath::CountBits(DeltaBits);
168 }
169
170 return FString::Printf(TEXT("%d bits differ"), DiffNumBits);
171}
172
173template <class T, class U>
175{
176 using WordTypeT = typename T::StorageWordType;
177 using WordTypeU = typename U::StorageWordType;
179
180 FString Buffer;
181 Buffer.Reserve(1024);
182
183 const WordTypeT* BitWordDataA = BitArrayA.GetData();
184 const WordTypeU* BitWordDataB = BitArrayB.GetData();
185
186 int32 DiffNumBits = 0;
187 for (uint32 WordIndex = 0; WordIndex < BitArrayA.GetNumWords(); ++WordIndex)
188 {
189 const WordTypeT BitWordA = BitWordDataA[WordIndex];
190 const WordTypeU BitWordB = BitWordDataB[WordIndex];
191
193
194 const int32 DiffBits = FPlatformMath::CountBits(DeltaBits);
195
196 if (DiffBits > 0)
197 {
199
200 Buffer += FString::Printf(TEXT(" (0x%.2x)[%#.8x]vs[%#.8x]"), WordIndex, BitWordA, BitWordB);
201 }
202 }
203
204 if (Buffer.IsEmpty())
205 {
206 Buffer = TEXT(" none");
207 }
208
209 return FString::Printf(TEXT("%d bits differ:%s"), DiffNumBits, *Buffer);
210}
211
212} // end namespace UE::Net
#define TEXT(x)
Definition Platform.h:1272
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
#define UE_NETBITARRAY_VALIDATE_BOTH_COMPATIBLE(lhs, rhs)
Definition NetBitArray.h:39
uint32_t uint32
Definition binka_ue_file_header.h:6
Definition NetBitArrayPrinter.h:16
static FString PrintZeroSummary(const T &BitArray)
Definition NetBitArrayPrinter.h:62
static FString PrintSetBits(const T &BitArray)
Definition NetBitArrayPrinter.h:68
static FString PrintSetSummary(const T &BitArray)
Definition NetBitArrayPrinter.h:56
static FString PrintZeroBits(const T &BitArray)
Definition NetBitArrayPrinter.h:108
static FString PrintDeltaBits(const T &BitArrayA, const U &BitArrayB)
Definition NetBitArrayPrinter.h:174
static FString PrintDeltaSummary(const T &BitArrayA, const U &BitArrayB)
Definition NetBitArrayPrinter.h:149
Definition NetworkVersion.cpp:28