UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
Symbol.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
7#include "uLang/Common/Containers/SharedPointer.h" // For CSharedMix
11
12#define UE_API ULANGCORE_API
13
14namespace uLang
15{
16
19
21enum { SymbolId_Null = 0 };
22
23class CSymbol;
24
27{
28public:
32
34
36 CSymbol Get(SymbolId Id) const;
37
39 UE_API TOptional<CSymbol> Find(const CUTF8StringView& Text, bool bIsGenerated = false) const;
40
42 UE_API TOptional<CSymbol> Add(const CUTF8StringView& Text, bool bIsGenerated = false);
43
45 UE_API CSymbol AddChecked(const CUTF8StringView& Text, bool bIsGenerated = false);
46
49 UE_API void ReAdd(CSymbol& Symbol);
50
52 static constexpr uint32_t MaxSymbolLength = 1024; // A symbol can be accepted here, but fail later in the compiler due to name transformations.
53
54private:
55 friend class CSymbol;
56
59 struct SEntry
60 {
61 SEntry* _HashNext;
62 SymbolId _Id;
63 uint32_t _ByteLength : 31;
64 uint32_t _bIsGenerated : 1;
65 UTF8Char _Data[1];
66
67 ULANG_FORCEINLINE CUTF8StringView AsStringView() const
68 {
69 return { _Data, _Data + _ByteLength };
70 }
71
72 ULANG_FORCEINLINE const char* AsCString() const
73 {
74 return (const char *)_Data;
75 }
76 };
77
78 UE_API uint32_t GetBucketIndex(const CUTF8StringView& Text) const;
79 UE_API const SEntry * AddInternal(const CUTF8StringView& Text, uint32_t BucketIndex, bool bIsGenerated);
80 UE_API const SEntry * FindOrAddInternal(const CUTF8StringView& Text, bool bIsGenerated);
81
82 // No copying allowed
83 CSymbolTable(const CSymbolTable&) = delete;
84 void operator = (const CSymbolTable&) = delete;
85 void operator = (CSymbolTable&&) = delete;
86
87 TArray<SEntry*> _HashBuckets;
88 CArenaAllocator _Allocator;
89 TArray<SEntry**> _IdLookupTable;
90 uint32_t _IdChunkShift;
91 SymbolId _HighestUsedId;
92
93 static UE_API const SEntry _EntryNull;
94};
95
98{
99public:
100 ULANG_FORCEINLINE CSymbol() : _Entry(&CSymbolTable::_EntryNull) {}
101
102 ULANG_FORCEINLINE SymbolId GetId() const { return _Entry->_Id; }
103 ULANG_FORCEINLINE bool IsNull() const { return _Entry->_Id == SymbolId_Null; }
104 ULANG_FORCEINLINE bool IsGenerated() const { return _Entry->_bIsGenerated; }
105
106 ULANG_FORCEINLINE CUTF8String AsString() const { return _Entry->AsStringView(); }
107 ULANG_FORCEINLINE CUTF8StringView AsStringView() const { return _Entry->AsStringView(); }
108 ULANG_FORCEINLINE const char* AsCString() const { return _Entry->AsCString(); }
109 ULANG_FORCEINLINE UTF8Char FirstByte() const { return _Entry->_Data[0]; }
110
111 ULANG_FORCEINLINE EEquate Compare(const CSymbol& Other) const { return (_Entry == Other._Entry) ? EEquate::Equal : ((_Entry < Other._Entry) ? EEquate::Less : EEquate::Greater); }
112 ULANG_FORCEINLINE bool operator == (const CSymbol& Other) const { return _Entry == Other._Entry; }
113 ULANG_FORCEINLINE bool operator != (const CSymbol& Other) const { return _Entry != Other._Entry; }
114 ULANG_FORCEINLINE bool operator <= (const CSymbol& Other) const { return _Entry <= Other._Entry; }
115 ULANG_FORCEINLINE bool operator >= (const CSymbol& Other) const { return _Entry >= Other._Entry; }
116 ULANG_FORCEINLINE bool operator < (const CSymbol& Other) const { return _Entry < Other._Entry; }
117 ULANG_FORCEINLINE bool operator > (const CSymbol& Other) const { return _Entry > Other._Entry; }
118
121 {
122 return GetTypeHash(Symbol._Entry->_Id);
123 }
124
125private:
126 friend class CSymbolTable;
127
128 ULANG_FORCEINLINE CSymbol(const CSymbolTable::SEntry* Entry) : _Entry(Entry) {}
129
130 const CSymbolTable::SEntry * _Entry;
131};
132
133//=======================================================================================
134// CSymbolTable Inline Methods
135//=======================================================================================
136
138{
139 ULANG_ASSERTF(Id <= _HighestUsedId, "Id out of range!");
140
141 if (Id == SymbolId_Null) return CSymbol();
142
143 uint32_t Index = Id - 1;
144 uint32_t ChunkIndex = Index >> _IdChunkShift;
145 uint32_t WithinChunkIndex = Index & ((1 << _IdChunkShift) - 1);
146 return CSymbol(_IdLookupTable[ChunkIndex][WithinChunkIndex]);
147}
148
149}
150
151#undef UE_API
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
#define UE_API
Definition SColorGradingComponentViewer.h:12
#define ULANG_FORCEINLINE
Definition Common.h:188
#define ULANG_ASSERTF(expr, format,...)
Definition Common.h:290
Definition Array.h:670
Definition SharedPointer.h:28
Database keeping track of symbols and their text equivalent.
Definition Symbol.h:27
friend class CSymbol
Definition Symbol.h:55
CSymbol Get(SymbolId Id) const
Gets a symbol by id - id must exist.
Definition Symbol.h:137
UE_API void ReAdd(CSymbol &Symbol)
Definition Symbol.cpp:142
static constexpr uint32_t MaxSymbolLength
The max length of a symbol.
Definition Symbol.h:52
UE_API CSymbol AddChecked(const CUTF8StringView &Text, bool bIsGenerated=false)
Looks up a symbol by text, and if not present yet, adds it. Asserts if text is to long.
Definition Symbol.cpp:135
UE_API ~CSymbolTable()
Definition Symbol.cpp:29
Symbol representing a text string with an associated id.
Definition Symbol.h:98
ULANG_FORCEINLINE CSymbol()
Definition Symbol.h:100
ULANG_FORCEINLINE EEquate Compare(const CSymbol &Other) const
Definition Symbol.h:111
ULANG_FORCEINLINE SymbolId GetId() const
Definition Symbol.h:102
ULANG_FORCEINLINE UTF8Char FirstByte() const
Definition Symbol.h:109
ULANG_FORCEINLINE CUTF8StringView AsStringView() const
Definition Symbol.h:107
ULANG_FORCEINLINE bool operator==(const CSymbol &Other) const
Definition Symbol.h:112
ULANG_FORCEINLINE bool IsNull() const
Definition Symbol.h:103
ULANG_FORCEINLINE bool operator>=(const CSymbol &Other) const
Definition Symbol.h:115
ULANG_FORCEINLINE bool operator!=(const CSymbol &Other) const
Definition Symbol.h:113
ULANG_FORCEINLINE bool operator<(const CSymbol &Other) const
Definition Symbol.h:116
ULANG_FORCEINLINE CUTF8String AsString() const
Definition Symbol.h:106
ULANG_FORCEINLINE friend uint32_t GetTypeHash(CSymbol Symbol)
Hash function for maps, sets.
Definition Symbol.h:120
ULANG_FORCEINLINE bool IsGenerated() const
Definition Symbol.h:104
ULANG_FORCEINLINE const char * AsCString() const
Definition Symbol.h:108
ULANG_FORCEINLINE bool operator>(const CSymbol &Other) const
Definition Symbol.h:117
ULANG_FORCEINLINE bool operator<=(const CSymbol &Other) const
Definition Symbol.h:114
Definition UTF8StringView.h:15
Definition VVMEngineEnvironment.h:23
uint32_t SymbolId
A unique id representing a symbol.
Definition Symbol.h:18
uint8_t UTF8Char
UTF-8 octet.
Definition Unicode.h:20
EEquate
Definition Common.h:367
@ SymbolId_Null
Definition Symbol.h:21
U16 Index
Definition radfft.cpp:71
Definition Optional.h:23