1//************************************ bs::framework - Copyright 2018 Marko Pintera **************************************//
2//*********** Licensed under the MIT license. See LICENSE.md for full terms. This notice is not to be removed. ***********//
3#include "BsCorePrerequisites.h"
4#include "Localization/BsHString.h"
5#include "Localization/BsStringTableManager.h"
6
7namespace bs
8{
9 HString::HString()
10 {
11 mStringData = StringTableManager::instance().getTable(0)->getStringData(u8"");
12
13 if (mStringData->numParameters > 0)
14 mParameters = bs_newN<String>(mStringData->numParameters);
15 }
16
17 HString::HString(UINT32 stringTableId)
18 {
19 mStringData = StringTableManager::instance().getTable(stringTableId)->getStringData(u8"");
20
21 if (mStringData->numParameters > 0)
22 mParameters = bs_newN<String>(mStringData->numParameters);
23 }
24
25 HString::HString(const String& identifierString, UINT32 stringTableId)
26 {
27 mStringData = StringTableManager::instance().getTable(stringTableId)->getStringData(identifierString);
28
29 if (mStringData->numParameters > 0)
30 mParameters = bs_newN<String>(mStringData->numParameters);
31 }
32
33 HString::HString(const String& identifierString, const String& defaultString, UINT32 stringTableId)
34 {
35 HStringTable table = StringTableManager::instance().getTable(stringTableId);
36 table->setString(identifierString, StringTable::DEFAULT_LANGUAGE, defaultString);
37
38 mStringData = table->getStringData(identifierString);
39
40 if (mStringData->numParameters > 0)
41 mParameters = bs_newN<String>(mStringData->numParameters);
42 }
43
44 HString::HString(const HString& copy)
45 {
46 mStringData = copy.mStringData;
47 mIsDirty = copy.mIsDirty;
48 mCachedString = copy.mCachedString;
49
50 if (copy.mStringData->numParameters > 0)
51 {
52 mParameters = bs_newN<String>(mStringData->numParameters);
53 if (copy.mParameters != nullptr)
54 {
55 for (UINT32 i = 0; i < mStringData->numParameters; i++)
56 mParameters[i] = copy.mParameters[i];
57 }
58
59 mStringPtr = &mCachedString;
60 }
61 else
62 {
63 mParameters = nullptr;
64 mStringPtr = &mStringData->string;
65 }
66 }
67
68 HString::~HString()
69 {
70 if (mParameters != nullptr)
71 bs_deleteN(mParameters, mStringData->numParameters);
72 }
73
74 HString::operator const String& () const
75 {
76 return getValue();
77 }
78
79 HString& HString::operator=(const HString& rhs)
80 {
81 if (mParameters != nullptr)
82 {
83 bs_deleteN(mParameters, mStringData->numParameters);
84 mParameters = nullptr;
85 }
86
87 mStringData = rhs.mStringData;
88 mIsDirty = rhs.mIsDirty;
89 mCachedString = rhs.mCachedString;
90
91 if (rhs.mStringData->numParameters > 0)
92 {
93 mParameters = bs_newN<String>(mStringData->numParameters);
94 if (rhs.mParameters != nullptr)
95 {
96 for (UINT32 i = 0; i < mStringData->numParameters; i++)
97 mParameters[i] = rhs.mParameters[i];
98 }
99
100 mStringPtr = &mCachedString;
101 }
102 else
103 {
104 mParameters = nullptr;
105 mStringPtr = &mStringData->string;
106 }
107
108 return *this;
109 }
110
111 const String& HString::getValue() const
112 {
113 if (mIsDirty)
114 {
115 if (mParameters != nullptr)
116 {
117 mStringData->concatenateString(mCachedString, mParameters, mStringData->numParameters);
118 mStringPtr = &mCachedString;
119 }
120 else
121 {
122 mStringPtr = &mStringData->string;
123 }
124
125 mIsDirty = false;
126 }
127
128 return *mStringPtr;
129 }
130
131 void HString::setParameter(UINT32 idx, const String& value)
132 {
133 if (idx >= mStringData->numParameters)
134 return;
135
136 mParameters[idx] = value;
137 mIsDirty = true;
138 }
139
140 const HString& HString::dummy()
141 {
142 static HString dummyVal;
143
144 return dummyVal;
145 }
146}