1
2
3
4
5
6 #include <cmajor/rt/String.hpp>
7 #include <mutex>
8
9 namespace cmajor { namespace rt {
10
11 std::unique_ptr<StringTable> StringTable::instance;
12
13 void StringTable::Init()
14 {
15 instance.reset(new StringTable());
16 }
17
18 void StringTable::Done()
19 {
20 instance.reset();
21 }
22
23 StringTable::StringTable() : nextStringHandle(1)
24 {
25 }
26
27 std::mutex mtx;
28
29 int32_t StringTable::InstallString(const std::string& str)
30 {
31 std::lock_guard<std::mutex> lock(mtx);
32 int32_t stringHandle = nextStringHandle++;
33 stringMap[stringHandle] = str;
34 return stringHandle;
35 }
36
37 const char* StringTable::GetString(int32_t stringHandle)
38 {
39 std::lock_guard<std::mutex> lock(mtx);
40 auto it = stringMap.find(stringHandle);
41 if (it != stringMap.cend())
42 {
43 return it->second.c_str();
44 }
45 return "";
46 }
47
48 void StringTable::DisposeString(int32_t stringHandle)
49 {
50 std::lock_guard<std::mutex> lock(mtx);
51 stringMap.erase(stringHandle);
52 }
53
54 int32_t InstallString(const std::string& str)
55 {
56 return StringTable::Instance().InstallString(str);
57 }
58
59 const char* GetString(int32_t stringHandle)
60 {
61 return StringTable::Instance().GetString(stringHandle);
62 }
63
64 void DisposeString(int32_t stringHandle)
65 {
66 StringTable::Instance().DisposeString(stringHandle);
67 }
68
69 void InitString()
70 {
71 StringTable::Init();
72 }
73
74 void DoneString()
75 {
76 StringTable::Done();
77 }
78
79 } }