Plasma Engine  2.0
Loading...
Searching...
No Matches
ConversionUtils.h
1#pragma once
2
3#include <Foundation/Basics.h>
4#include <Foundation/Math/Color.h>
5#include <Foundation/Math/Mat3.h>
6#include <Foundation/Math/Mat4.h>
7#include <Foundation/Math/Quat.h>
8#include <Foundation/Math/Transform.h>
9#include <Foundation/Strings/String.h>
10#include <Foundation/Time/Time.h>
11#include <Foundation/Types/Uuid.h>
12
18{
38 PL_FOUNDATION_DLL plResult StringToInt(plStringView sText, plInt32& out_iRes, const char** out_pLastParsePosition = nullptr); // [tested]
39
43 PL_FOUNDATION_DLL plResult StringToUInt(plStringView sText, plUInt32& out_uiRes, const char** out_pLastParsePosition = nullptr); // [tested]
44
46 PL_FOUNDATION_DLL plResult StringToInt64(plStringView sText, plInt64& out_iRes, const char** out_pLastParsePosition = nullptr); // [tested]
47
81 PL_FOUNDATION_DLL plResult StringToFloat(plStringView sText, double& out_fRes, const char** out_pLastParsePosition = nullptr); // [tested]
82
102 PL_FOUNDATION_DLL plResult StringToBool(plStringView sText, bool& out_bRes, const char** out_pLastParsePosition = nullptr); // [tested]
103
104
124 PL_FOUNDATION_DLL plUInt32 ExtractFloatsFromString(plStringView sText, plUInt32 uiNumFloats, float* out_pFloats, const char** out_pLastParsePosition = nullptr); // [tested]
125
129 PL_FOUNDATION_DLL plInt8 HexCharacterToIntValue(plUInt32 uiCharacter); // [tested]
130
132 PL_FOUNDATION_DLL plResult ConvertHexStringToUInt32(plStringView sHex, plUInt32& out_uiResult); // [tested]
133
135 PL_FOUNDATION_DLL plResult ConvertHexStringToUInt64(plStringView sHex, plUInt64& out_uiResult); // [tested]
136
144 PL_FOUNDATION_DLL plResult ConvertHexStringToUInt(plStringView sHex, plUInt64& out_uiResult, plUInt32 uiMaxHexCharacters, plUInt32* pTotalCharactersParsed); // [tested]
145
157 PL_FOUNDATION_DLL void ConvertHexToBinary(plStringView sText, plUInt8* pBinary, plUInt32 uiBinaryBuffer); // [tested]
158
165 template <typename APPEND_CONTAINER_LAMBDA>
166 inline void ConvertBinaryToHex(const void* pBinaryData, plUInt32 uiBytes, APPEND_CONTAINER_LAMBDA append); // [tested]
167
169 PL_FOUNDATION_DLL plUuid ConvertStringToUuid(plStringView sText); // [tested]
170
173 PL_FOUNDATION_DLL bool IsStringUuid(plStringView sText); // [tested]
174
176 PL_ALWAYS_INLINE const plStringBuilder& ToString(bool value, plStringBuilder& out_sResult) // [tested]
177 {
178 out_sResult = value ? "true" : "false";
179 return out_sResult;
180 }
181
183 PL_FOUNDATION_DLL const plStringBuilder& ToString(plInt8 value, plStringBuilder& out_sResult); // [tested]
184
186 PL_FOUNDATION_DLL const plStringBuilder& ToString(plUInt8 value, plStringBuilder& out_sResult); // [tested]
187
189 PL_FOUNDATION_DLL const plStringBuilder& ToString(plInt16 value, plStringBuilder& out_sResult); // [tested]
190
192 PL_FOUNDATION_DLL const plStringBuilder& ToString(plUInt16 value, plStringBuilder& out_sResult); // [tested]
193
195 PL_FOUNDATION_DLL const plStringBuilder& ToString(plInt32 value, plStringBuilder& out_sResult); // [tested]
196
198 PL_FOUNDATION_DLL const plStringBuilder& ToString(plUInt32 value, plStringBuilder& out_sResult); // [tested]
199
201 PL_FOUNDATION_DLL const plStringBuilder& ToString(plInt64 value, plStringBuilder& out_sResult); // [tested]
202
204 PL_FOUNDATION_DLL const plStringBuilder& ToString(plUInt64 value, plStringBuilder& out_sResult); // [tested]
205
207 PL_FOUNDATION_DLL const plStringBuilder& ToString(float value, plStringBuilder& out_sResult); // [tested]
208
210 PL_FOUNDATION_DLL const plStringBuilder& ToString(double value, plStringBuilder& out_sResult); // [tested]
211
213 PL_FOUNDATION_DLL const plStringBuilder& ToString(const plColor& value, plStringBuilder& out_sResult); // [tested]
214
216 PL_FOUNDATION_DLL const plStringBuilder& ToString(const plColorGammaUB& value, plStringBuilder& out_sResult); // [tested]
217
219 PL_FOUNDATION_DLL const plStringBuilder& ToString(const plVec2& value, plStringBuilder& out_sResult); // [tested]
220
222 PL_FOUNDATION_DLL const plStringBuilder& ToString(const plVec3& value, plStringBuilder& out_sResult); // [tested]
223
225 PL_FOUNDATION_DLL const plStringBuilder& ToString(const plVec4& value, plStringBuilder& out_sResult); // [tested]
226
228 PL_FOUNDATION_DLL const plStringBuilder& ToString(const plVec2I32& value, plStringBuilder& out_sResult); // [tested]
229
231 PL_FOUNDATION_DLL const plStringBuilder& ToString(const plVec3I32& value, plStringBuilder& out_sResult); // [tested]
232
234 PL_FOUNDATION_DLL const plStringBuilder& ToString(const plVec4I32& value, plStringBuilder& out_sResult); // [tested]
235
237 PL_FOUNDATION_DLL const plStringBuilder& ToString(const plQuat& value, plStringBuilder& out_sResult); // [tested]
238
240 PL_FOUNDATION_DLL const plStringBuilder& ToString(const plMat3& value, plStringBuilder& out_sResult); // [tested]
241
243 PL_FOUNDATION_DLL const plStringBuilder& ToString(const plMat4& value, plStringBuilder& out_sResult); // [tested]
244
246 PL_FOUNDATION_DLL const plStringBuilder& ToString(const plTransform& value, plStringBuilder& out_sResult); // [tested]
247
249 PL_FOUNDATION_DLL const plStringBuilder& ToString(const plUuid& value, plStringBuilder& out_sResult); // [tested]
250
252 PL_FOUNDATION_DLL const plStringBuilder& ToString(const plAngle& value, plStringBuilder& out_sResult); // [tested]
253
255 PL_FOUNDATION_DLL const plStringBuilder& ToString(const plTime& value, plStringBuilder& out_sResult);
256
258 PL_FOUNDATION_DLL const plStringBuilder& ToString(const plStringView& value, plStringBuilder& out_sResult);
259
261 PL_FOUNDATION_DLL const plStringBuilder& ToString(const plHashedString& value, plStringBuilder& out_sResult);
262
264 PL_FOUNDATION_DLL const plStringBuilder& ToString(const plTempHashedString& value, plStringBuilder& out_sResult);
265
267 PL_FOUNDATION_DLL const plStringBuilder& ToString(const plDynamicArray<plVariant>& value, plStringBuilder& out_sResult);
268
270 PL_FOUNDATION_DLL const plStringBuilder& ToString(const plHashTable<plString, plVariant>& value, plStringBuilder& out_sResult);
271
273 template <typename T>
274 PL_ALWAYS_INLINE const plStringBuilder& ToString(const T& value, plStringBuilder& out_sResult)
275 {
276 out_sResult = "N/A";
277 return out_sResult;
278 }
279
284 PL_FOUNDATION_DLL plColor GetColorByName(plStringView sText, bool* out_pValidColorName = nullptr); // [tested]
285
287 PL_FOUNDATION_DLL plString GetColorName(const plColor& col); // [tested]
288}; // namespace plConversionUtils
289
290template <typename APPEND_CONTAINER_LAMBDA>
291inline void plConversionUtils::ConvertBinaryToHex(const void* pBinaryData, plUInt32 uiBytes, APPEND_CONTAINER_LAMBDA append) // [tested]
292{
293 char tmp[4];
294
295 plUInt8* pBytes = (plUInt8*)pBinaryData;
296
297 for (plUInt32 i = 0; i < uiBytes; ++i)
298 {
299 plStringUtils::snprintf(tmp, 4, "%02X", (plUInt32)*pBytes);
300 ++pBytes;
301
302 append(tmp);
303 }
304}
Float wrapper struct for a safe usage and conversions of angles.
Definition Angle.h:10
A 8bit per channel unsigned normalized (values interpreted as 0-1) color storage format that represen...
Definition Color8UNorm.h:99
plColor represents an RGBA color in linear color space. Values are stored as float,...
Definition Color.h:44
Definition DynamicArray.h:81
Definition HashTable.h:333
This class is optimized to take nearly no memory (sizeof(void*)) and to allow very fast checks whethe...
Definition HashedString.h:25
plStringBuilder is a class that is meant for creating and modifying strings.
Definition StringBuilder.h:35
static plInt32 snprintf(char *szDst, plUInt32 uiDstSize, const char *szFormat,...)
Creates a formated string in szDst. uiDstSize defines how many bytes szDst can hold.
Definition snprintf.cpp:1062
plStringView represent a read-only sub-string of a larger string, as it can store a dedicated string ...
Definition StringView.h:34
A class to use together with plHashedString for quick comparisons with temporary strings that need no...
Definition HashedString.h:151
This data type is the abstraction for 128-bit Uuid (also known as GUID) instances.
Definition Uuid.h:11
This namespace contains functions to convert between different types.
Definition ConversionUtils.h:18
PL_FOUNDATION_DLL plResult StringToBool(plStringView sText, bool &out_bRes, const char **out_pLastParsePosition=nullptr)
Parses szString and checks that the first word it finds starts with a phrase that can be interpreted ...
Definition ConversionUtils.cpp:310
PL_FOUNDATION_DLL plColor GetColorByName(plStringView sText, bool *out_pValidColorName=nullptr)
Returns the color with the given name.
Definition ConversionUtils.cpp:871
PL_FOUNDATION_DLL plResult ConvertHexStringToUInt64(plStringView sHex, plUInt64 &out_uiResult)
Same as ConvertHexStringToUInt() with uiMaxHexCharacters set to 16.
Definition ConversionUtils.cpp:477
PL_FOUNDATION_DLL plUuid ConvertStringToUuid(plStringView sText)
Converts a string that was written with plConversionUtils::ToString(plUuid) back to an plUuid object.
Definition ConversionUtils.cpp:794
PL_FOUNDATION_DLL plUInt32 ExtractFloatsFromString(plStringView sText, plUInt32 uiNumFloats, float *out_pFloats, const char **out_pLastParsePosition=nullptr)
Parses szText and tries to find up to uiNumFloats float values to extract. Skips all characters that ...
Definition ConversionUtils.cpp:422
void ConvertBinaryToHex(const void *pBinaryData, plUInt32 uiBytes, APPEND_CONTAINER_LAMBDA append)
Converts a binary stream to a HEX string.
Definition ConversionUtils.h:291
PL_FOUNDATION_DLL plString GetColorName(const plColor &col)
The inverse of GetColorByName.
Definition ConversionUtils.cpp:1089
PL_FOUNDATION_DLL plResult ConvertHexStringToUInt32(plStringView sHex, plUInt32 &out_uiResult)
Same as ConvertHexStringToUInt() with uiMaxHexCharacters set to 8.
Definition ConversionUtils.cpp:468
PL_FOUNDATION_DLL bool IsStringUuid(plStringView sText)
Returns true when the given string is in the exact format "{ 05af8d07-0b38-44a6-8d50-49731ae2625d }...
Definition ConversionUtils.cpp:776
PL_FOUNDATION_DLL void ConvertHexToBinary(plStringView sText, plUInt8 *pBinary, plUInt32 uiBinaryBuffer)
Converts a HEX string to a binary value.
Definition ConversionUtils.cpp:530
PL_FOUNDATION_DLL plResult StringToInt(plStringView sText, plInt32 &out_iRes, const char **out_pLastParsePosition=nullptr)
Parses szString and converts it to an integer value. Returns PL_FAILURE if the string contains no par...
Definition ConversionUtils.cpp:87
PL_FOUNDATION_DLL plResult StringToInt64(plStringView sText, plInt64 &out_iRes, const char **out_pLastParsePosition=nullptr)
Same as StringToInt but converts to a 64bit integer value instead.
Definition ConversionUtils.cpp:111
PL_ALWAYS_INLINE const plStringBuilder & ToString(bool value, plStringBuilder &out_sResult)
Converts a bool to a string.
Definition ConversionUtils.h:176
PL_FOUNDATION_DLL plResult StringToUInt(plStringView sText, plUInt32 &out_uiRes, const char **out_pLastParsePosition=nullptr)
Same as StringToInt() but expects the string to be a uint32.
Definition ConversionUtils.cpp:99
PL_FOUNDATION_DLL plResult StringToFloat(plStringView sText, double &out_fRes, const char **out_pLastParsePosition=nullptr)
Parses szString and converts it to a double value. Returns PL_FAILURE if the string contains no parse...
Definition ConversionUtils.cpp:162
PL_FOUNDATION_DLL plResult ConvertHexStringToUInt(plStringView sHex, plUInt64 &out_uiResult, plUInt32 uiMaxHexCharacters, plUInt32 *pTotalCharactersParsed)
Converts a hex string (i.e. 0xAABBCCDD) into its uint64 value.
Definition ConversionUtils.cpp:482
PL_FOUNDATION_DLL plInt8 HexCharacterToIntValue(plUInt32 uiCharacter)
Converts a hex character ('0', '1', ... '9', 'A'/'a', ... 'F'/'f') to the corresponding int value 0 -...
Definition ConversionUtils.cpp:454
Default enum for returning failure or success, instead of using a bool.
Definition Types.h:54
The time class encapsulates a double value storing the time in seconds.
Definition Time.h:12