UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
SharedString.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
7#include "CoreTypes.h"
8#include "HAL/MemoryBase.h"
9#include "HAL/UnrealMemory.h"
10#include "Misc/CString.h"
11
12#include <atomic>
13
14namespace UE
15{
16
27template <typename CharType>
29{
30public:
31 using ElementType = CharType;
32
33 [[nodiscard]] TSharedString() = default;
38
41
44
46 {
47 Release(Chars);
48 }
49
51 inline void Reset()
52 {
53 Release(Chars);
54 Chars = nullptr;
55 }
56
58 [[nodiscard]] inline bool IsEmpty() const
59 {
60 return !Chars;
61 }
62
64 [[nodiscard]] inline int32 Len() const
65 {
66 return Chars ? reinterpret_cast<const int32*>(Chars)[-1] : 0;
67 }
68
70 [[nodiscard]] inline const CharType* operator*() const
71 {
72 return Chars ? Chars : &NullChar;
73 }
74
76 static const TSharedString Empty;
77
78private:
79 [[nodiscard]] friend constexpr inline const CharType* GetData(const TSharedString String)
80 {
81 return *String;
82 }
83
84 [[nodiscard]] friend constexpr inline auto GetNum(const TSharedString String)
85 {
86 return String.Len();
87 }
88
89 [[nodiscard]] friend inline uint32 GetTypeHash(const TSharedString& String)
90 {
92 }
93
94 [[nodiscard]] friend inline bool operator==(const TSharedString& Lhs, const TSharedString& Rhs)
95 {
97 }
98
99 [[nodiscard]] friend inline bool operator<(const TSharedString& Lhs, const TSharedString& Rhs)
100 {
101 return MakeStringView(Lhs).Compare(MakeStringView(Rhs), ESearchCase::IgnoreCase) < 0;
102 }
103
104 static inline void AddRef(CharType* Chars);
105 static inline void Release(CharType* Chars);
106
107 CharType* Chars = nullptr;
108
109 static constexpr inline CharType NullChar{};
110};
111
112template <typename CharType>
114
115template <typename CharType>
117 : Chars(String.Chars)
118{
119 String.Chars = nullptr;
120}
121
122template <typename CharType>
124 : Chars(String.Chars)
125{
126 AddRef(Chars);
127}
128
129template <typename CharType>
131{
132 if (this != &String)
133 {
134 Release(Chars);
135 Chars = String.Chars;
136 String.Chars = nullptr;
137 }
138 return *this;
139}
140
141template <typename CharType>
143{
144 CharType* OldChars = Chars;
145 CharType* NewChars = String.Chars;
146 AddRef(NewChars);
148 Chars = NewChars;
149 return *this;
150}
151
152template <typename CharType>
154{
155 if (const int32 Length = String.Len())
156 {
157 static_assert(alignof(int32) <= MIN_ALIGNMENT);
158 static_assert(alignof(int32) >= alignof(CharType));
159 static_assert(sizeof(int32) == sizeof(std::atomic<int32>));
160 const SIZE_T Size = sizeof(int32) + sizeof(int32) + sizeof(CharType) + sizeof(CharType) * Length;
161 int32* const Header = static_cast<int32*>(FMemory::Malloc(Size));
162 new((void*)&Header[0]) std::atomic<int32>(1); // Ref Count
163 new((void*)&Header[1]) int32(Length);
164 Chars = reinterpret_cast<CharType*>(&Header[2]);
165 String.CopyString(Chars, Length);
166 Chars[Length] = CharType(0);
167 }
168}
169
170template <typename CharType>
175
176template <typename CharType>
178{
179 if (MaybeNullChars)
180 {
181 static_assert(sizeof(int32) == sizeof(std::atomic<int32>));
182 int32* const Header = reinterpret_cast<int32*>(MaybeNullChars) - 2;
183 std::atomic<int32>& RefCount = reinterpret_cast<std::atomic<int32>&>(Header[0]);
184 RefCount.fetch_add(1, std::memory_order_relaxed);
185 }
186}
187
188template <typename CharType>
189void TSharedString<CharType>::Release(CharType* const MaybeNullChars)
190{
191 if (MaybeNullChars)
192 {
193 static_assert(sizeof(int32) == sizeof(std::atomic<int32>));
194 int32* const Header = reinterpret_cast<int32*>(MaybeNullChars) - 2;
195 std::atomic<int32>& RefCount = reinterpret_cast<std::atomic<int32>&>(Header[0]);
196 if (RefCount.fetch_sub(1, std::memory_order_acq_rel) == 1)
197 {
199 }
200 }
201}
202
203} // UE
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
@ MIN_ALIGNMENT
Definition MemoryBase.h:27
constexpr auto MakeStringView(const CharPtrOrRangeType &CharPtrOrRange UE_LIFETIMEBOUND) -> decltype(TStringView(CharPtrOrRange))
Definition StringView.h:508
uint32 Size
Definition VulkanMemory.cpp:4034
uint32_t uint32
Definition binka_ue_file_header.h:6
Definition StringView.h:107
Definition SharedString.h:29
TSharedString & operator=(TStringView< CharType > String)
Definition SharedString.h:171
friend bool operator<(const TSharedString &Lhs, const TSharedString &Rhs)
Definition SharedString.h:99
TSharedString & operator=(const TSharedString &String)
Definition SharedString.h:142
static const TSharedString Empty
Definition SharedString.h:76
TSharedString & operator=(TSharedString &&String)
Definition SharedString.h:130
friend uint32 GetTypeHash(const TSharedString &String)
Definition SharedString.h:89
TSharedString(TSharedString &&String)
Definition SharedString.h:116
TSharedString(TStringView< CharType > String)
Definition SharedString.h:153
TSharedString()=default
int32 Len() const
Definition SharedString.h:64
friend constexpr const CharType * GetData(const TSharedString String)
Definition SharedString.h:79
friend bool operator==(const TSharedString &Lhs, const TSharedString &Rhs)
Definition SharedString.h:94
TSharedString(const TSharedString &String)
Definition SharedString.h:123
bool IsEmpty() const
Definition SharedString.h:58
~TSharedString()
Definition SharedString.h:45
void Reset()
Definition SharedString.h:51
friend constexpr auto GetNum(const TSharedString String)
Definition SharedString.h:84
CharType ElementType
Definition SharedString.h:31
const CharType * operator*() const
Definition SharedString.h:70
@ IgnoreCase
Definition CString.h:26
Definition AdvancedWidgetsModule.cpp:13
static FORCENOINLINE CORE_API void Free(void *Original)
Definition UnrealMemory.cpp:685