MCRO
C++23 utilities for Unreal Engine.
Loading...
Searching...
No Matches
TextMacros.h
Go to the documentation of this file.
1/** @noop License Comment
2 * @file
3 * @copyright
4 * This Source Code is subject to the terms of the Mozilla Public License, v2.0.
5 * If a copy of the MPL was not distributed with this file You can obtain one at
6 * https://mozilla.org/MPL/2.0/
7 *
8 * @author David Mórász
9 * @date 2025
10 */
11
12/**
13 * @file
14 * @brief
15 * Use leading `TEXT_` without parenthesis for Unreal compatible text literals.
16 */
17
18#pragma once
19
20#include <string>
21
22#include "CoreMinimal.h"
23#include "Misc/EngineVersionComparison.h"
24#include "Internationalization/Internationalization.h"
25#include "Mcro/FunctionTraits.h"
26
27#define UTF8TEXT_PASTE_ u8""
28#define UTF16TEXT_PASTE_ u""
29
30#if PLATFORM_WIDECHAR_IS_CHAR16
31 #define WIDETEXT_PASTE_ UTF16TEXT_PASTE_
32#else
33 #define WIDETEXT_PASTE_ L""
34#endif
35
36#define UTF8TEXT_ UTF8TEXT_PASTE_ // TODO: UE::Core::Private::ToUTF8Literal with operator?
37#define UTF16TEXT_ UTF16TEXT_PASTE_
38#define WIDETEXT_ WIDETEXT_PASTE_
39
40#if PLATFORM_TCHAR_IS_UTF8CHAR
41 #define TEXT_PASTE_ UTF8TEXT_
42#else
43 #define TEXT_PASTE_ WIDETEXT_
44#endif
45
46/**
47 * @brief
48 * A convenience alternative to Unreal's own `TEXT` macro but this one doesn't require parenthesis around the text
49 * literal, relying on string literal concatenation rules of C++.
50 *
51 * This operation is resolved entirely in compile time
52 */
53#define TEXT_ TEXT_PASTE_
54
55/** @brief This namespace is used by MCRO text literal macros, don't use it directly! */
57{
58 using namespace Mcro::FunctionTraits;
59
60#if UE_VERSION_OLDER_THAN(5, 5, 0)
61 FORCEINLINE FText AsLocalizable_Advanced(const TCHAR* Namespace, const TCHAR* Key, const TCHAR* String)
62 {
63 return FInternationalization::ForUseOnlyByLocMacroAndGraphNodeTextLiterals_CreateText(String, Namespace, Key);
64 }
65#else
66 FORCEINLINE FText AsLocalizable_Advanced(const FTextKey& Namespace, const FTextKey& Key, const TCHAR* String)
67 {
68 return FText::AsLocalizable_Advanced(Namespace, Key, String);
69 }
70#endif
71
73 {
74 FORCEINLINE FText operator % (const TCHAR* literal)
75 {
76 return (*this)(literal);
77 }
78 };
79
81 {
82 FORCEINLINE FText operator % (const TCHAR* str) const
83 {
84 return FText::AsCultureInvariant(str);
85 }
86 };
87
89 {
90 template <size_t N>
91 consteval FStringView operator % (const TCHAR(& str)[N]) const
92 {
93 return FStringView(str, N-1);
94 }
95 };
96
98 {
99 template <size_t N>
100 FString operator % (const TCHAR(& str)[N]) const
101 {
102#if UE_VERSION_OLDER_THAN(5, 5, 0)
103 return FString(N-1, str);
104#else
105 return FString::ConstructFromPtrSize(str, N-1);
106#endif
107 }
108 };
109
111 {
112 template <size_t N>
113 FName operator % (const TCHAR(& str)[N]) const
114 {
115 return FName(N-1, str);
116 }
117 };
118
120 {
121 template <size_t N>
122 consteval std::basic_string_view<TCHAR> operator % (const TCHAR(& str)[N]) const
123 {
124 return std::basic_string_view<TCHAR>(str, N-1);
125 }
126 };
127}
128
129/**
130 * @brief
131 * A convenience alternative to Unreal's own `LOCTEXT` macro but this one doesn't require parenthesis around the text
132 * literal
133 *
134 * This operation allocates an argument deferring struct and FText in runtime
135 */
136#define LOCTEXT_(key) \
137 Mcro::Text::Macros::FDefer_AsLocalizable_Advanced(TEXT(LOCTEXT_NAMESPACE), TEXT(key)) % TEXT_
138
139/**
140 * @brief
141 * A convenience alternative to Unreal's own `NSLOCTEXT` macro but this one doesn't require parenthesis around the text
142 * literal
143 *
144 * This operation allocates an argument deferring struct and FText in runtime
145 */
146#define NSLOCTEXT_(ns, key) \
147 Mcro::Text::Macros::FDefer_AsLocalizable_Advanced(TEXT(ns), TEXT(key)) % TEXT_
148
149/**
150 * @brief
151 * A convenience alternative to Unreal's own `INVTEXT` macro but this one doesn't require parenthesis around the text
152 * literal
153 *
154 * This operation allocates FText in runtime and an empty tag struct
155 */
156#define INVTEXT_ \
157 Mcro::Text::Macros::FInvTextFakeLiteralTag() % TEXT_
158
159/**
160 * @brief
161 * A convenience alternative to Unreal's own `TEXTVIEW` macro but this one doesn't require parenthesis around the text
162 * literal.
163 *
164 * This operation creates an FStringView in consteval time. This is not a custom string literal because they're not
165 * available for concatenated groups of string literals of mixed encodings.
166 */
167#define TEXTVIEW_ Mcro::Text::Macros::FStringViewFakeLiteralTag() % TEXT_
168
169/**
170 * @brief
171 * A convenience alternative to Unreal's own `TEXTVIEW` macro but this one doesn't require parenthesis around the text
172 * literal and it returns an STL string view.
173 *
174 * This operation creates a `std::[w]string_view` in consteval time. This is not a custom string literal because
175 * they're not available for concatenated groups of string literals of mixed encodings.
176 */
177#define STDVIEW_ Mcro::Text::Macros::FStdStringLiteralTag() % TEXT_
178
179/**
180 * @brief
181 * A convenience macro to allocate an FString directly.
182 *
183 * This operation allocates FString in runtime and an empty tag struct. This is not a custom string literal because
184 * they're not available for concatenated groups of string literals of mixed encodings.
185 */
186#define STRING_ Mcro::Text::Macros::FStringFakeLiteralTag() % TEXT_
187
188/**
189 * @brief
190 * A convenience macro to allocate an FName directly.
191 *
192 * This operation allocates FName in runtime and an empty tag struct. This is not a custom string literal because
193 * they're not available for concatenated groups of string literals of mixed encodings.
194 */
195#define NAME_ Mcro::Text::Macros::FNameFakeLiteralTag() % TEXT_
This namespace is used by MCRO text literal macros, don't use it directly!
Definition TextMacros.h:57
FORCEINLINE FText AsLocalizable_Advanced(const FTextKey &Namespace, const FTextKey &Key, const TCHAR *String)
Definition TextMacros.h:66
Defers a set of arguments for a function call later with its first argument. This is useful for devel...
FORCEINLINE FText operator%(const TCHAR *literal)
Definition TextMacros.h:74
FORCEINLINE FText operator%(const TCHAR *str) const
Definition TextMacros.h:82
FName operator%(const TCHAR(&str)[N]) const
Definition TextMacros.h:113
consteval std::basic_string_view< TCHAR > operator%(const TCHAR(&str)[N]) const
Definition TextMacros.h:122
FString operator%(const TCHAR(&str)[N]) const
Definition TextMacros.h:100
consteval FStringView operator%(const TCHAR(&str)[N]) const
Definition TextMacros.h:91