1
2
3
4
5
6 #ifndef CMAJOR_BINDER_STRING_REPOSITORY_INCLUDED
7 #define CMAJOR_BINDER_STRING_REPOSITORY_INCLUDED
8 #include <soulng/util/Error.hpp>
9 #include <unordered_map>
10 #include <vector>
11
12 namespace cmajor { namespace binder {
13
14 template<class StringT class CharPtrT>
15 class StringRepository
16 {
17 public:
18 int Install(const StringT& str);
19 const StringT& GetString(int id) const;
20 CharPtrT CharPtr(int id) const;
21 private:
22 std::unordered_map<StringT, int> stringMap;
23 std::vector<StringT> strings;
24 };
25
26 template<class StringT class CharPtrT>
27 int StringRepository< StringT, CharPtrT>::Install(constStringT&str)
28 {
29 auto it = stringMap.find(str);
30 if (it != stringMap.cend())
31 {
32 return it->second;
33 }
34 else
35 {
36 int id = strings.size();
37 stringMap[str] = id;
38 strings.push_back(str);
39 return id;
40 }
41 }
42
43 template<class StringT class CharPtrT>
44 const StringT& StringRepository< StringT, CharPtrT>::GetString(intid) const
45 {
46 Assert(id >= 0 && id < strings.size(), "invalid string id");
47 return strings[id];
48 }
49
50 template<class StringT class CharPtrT>
51 CharPtrT StringRepository< StringT, CharPtrT>::CharPtr(intid) const
52 {
53 Assert(id >= 0 && id < strings.size(), "invalid string id");
54 return reinterpret_cast<CharPtrT>(strings[id].c_str());
55 }
56
57 } }
58
59 #endif // CMAJOR_BINDER_STRING_REPOSITORY_INCLUDED