Plasma Engine  2.0
Loading...
Searching...
No Matches
FormatStringImpl.h
1#pragma once
2
3#include <array>
4#include <tuple>
5#include <utility>
6
7template <typename... ARGS>
9{
10 // this is the size of the temp buffer that BuildString functions get for writing their result to.
11 // The buffer is always available and allocated on the stack, so this prevents the need for memory allocations.
12 // If a BuildString function requires no storage at all, it can return an plStringView to unrelated memory
13 // (e.g. if the memory already exists).
14 // If a BuildString function requires more storage, it may need to do some trickery.
15 // For an example look at BuildString for plArgErrorCode, which uses an increased thread_local temp buffer.
16 static constexpr plUInt32 TempStringLength = 64;
17 // Maximum number of parameters. Results in compilation error if exceeded.
18 static constexpr plUInt32 MaxNumParameters = 12;
19
20public:
21 plFormatStringImpl(plStringView sFormat, ARGS&&... args)
22 : m_Arguments(std::forward<ARGS>(args)...)
23 {
24 m_sString = sFormat;
25 }
26
27 plFormatStringImpl(const char* szFormat, ARGS&&... args)
28 : m_Arguments(std::forward<ARGS>(args)...)
29 {
30 m_sString = szFormat;
31 }
32
37 virtual plStringView GetText(plStringBuilder& ref_sStorage) const override
38 {
39 if (m_sString.IsEmpty())
40 {
41 return {};
42 }
43
44 plStringView param[MaxNumParameters];
45
46 char tmp[MaxNumParameters][TempStringLength];
47 ReplaceString<0>(tmp, param);
48
49 return BuildFormattedText(ref_sStorage, param, MaxNumParameters);
50 }
51
52 virtual const char* GetTextCStr(plStringBuilder& out_sString) const override
53 {
54 plStringView param[MaxNumParameters];
55
56 char tmp[MaxNumParameters][TempStringLength];
57 ReplaceString<0>(tmp, param);
58
59 return BuildFormattedText(out_sString, param, MaxNumParameters).GetStartPointer();
60 }
61
62private:
63 template <plInt32 N>
64 typename std::enable_if<sizeof...(ARGS) != N>::type ReplaceString(char tmp[MaxNumParameters][TempStringLength], plStringView* pViews) const
65 {
66 static_assert(N < MaxNumParameters, "Maximum number of format arguments reached");
67
68 // using a free function allows to overload with various different argument types
69 pViews[N] = BuildString(tmp[N], TempStringLength - 1, std::get<N>(m_Arguments));
70
71 // Recurse, chip off one argument
72 ReplaceString<N + 1>(tmp, pViews);
73 }
74
75 // Recursion end if we reached the number of arguments.
76 template <plInt32 N>
77 typename std::enable_if<sizeof...(ARGS) == N>::type ReplaceString(char tmp[MaxNumParameters][TempStringLength], plStringView* pViews) const
78 {
79 }
80
81
82 // stores the arguments
83 std::tuple<ARGS...> m_Arguments;
84};
Implements formating of strings with placeholders and formatting options.
Definition FormatString.h:59
plStringView BuildFormattedText(plStringBuilder &ref_sStorage, plStringView *pArgs, plUInt32 uiNumArgs) const
Helper function to build the formatted text with the given arguments.
Definition FormatString.cpp:21
Definition FormatStringImpl.h:9
virtual plStringView GetText(plStringBuilder &ref_sStorage) const override
Generates the formatted text. Make sure to only call this function once and only when the formatted s...
Definition FormatStringImpl.h:37
virtual const char * GetTextCStr(plStringBuilder &out_sString) const override
Similar to GetText() but guaranteed to copy the string into the given string builder,...
Definition FormatStringImpl.h:52
plStringBuilder is a class that is meant for creating and modifying strings.
Definition StringBuilder.h:35
plStringView represent a read-only sub-string of a larger string, as it can store a dedicated string ...
Definition StringView.h:34
const char * GetStartPointer() const
Returns the start of the view range.
Definition StringView.h:102
bool IsEmpty() const
Returns whether the string is an empty string.
Definition StringView_inl.h:85