Grindstone Game Engine v0.2.0
An open source game engine and toolkit.
Loading...
Searching...
No Matches
HashedString.hpp
1#pragma once
2
3#include <map>
4
5#include "String.hpp"
6#include "Hash.hpp"
7
8namespace Grindstone {
9 class HashedString {
10 public:
11 using HashMap = std::map<Grindstone::HashValue, Grindstone::String>;
12 static void CreateHashMap();
13 static HashMap* GetHashedStringMap();
14 static void SetHashMap(HashMap* hashMap);
15 HashedString();
16 explicit HashedString(const char* inStringRef);
17 explicit HashedString(const String& inString);
18 explicit HashedString(StringRef inStringRef);
19
20 void Create(StringRef inStringRef);
21 uint64_t GetHash() const;
22
23 operator bool() const noexcept;
24 bool operator==(const Grindstone::HashedString& other) const noexcept;
25
26 bool operator>(const HashedString& other) const {
27 return hash > other.hash;
28 }
29
30 bool operator<(const HashedString& other) const {
31 return hash < other.hash;
32 }
33
34 bool operator>=(const HashedString& other) const {
35 return hash >= other.hash;
36 }
37
38 bool operator<=(const HashedString& other) const {
39 return hash <= other.hash;
40 }
41
42 operator std::size_t() const {
43 return static_cast<std::size_t>(hash);
44 }
45
46
47 const String& ToString() const;
48 const char* c_str() const noexcept;
49
50 protected:
51 Grindstone::HashValue hash;
52 };
53
54 class ConstHashedString {
55 public:
56 constexpr ConstHashedString(const char* str)
57 : hash(Hash::MurmurOAAT64(str)), literal(str) {
58 }
59
60 bool operator==(const Grindstone::HashedString& other) const noexcept {
61 return hash == other.GetHash();
62 }
63
64 bool operator==(const Grindstone::ConstHashedString& other) const noexcept {
65 return hash == other.GetHash();
66 }
67
68 friend bool operator==(const Grindstone::HashedString& a, const Grindstone::ConstHashedString& b) noexcept {
69 return a.GetHash() == b.GetHash();
70 }
71
72 uint64_t GetHash() const noexcept { return hash; }
73 operator uint64_t() const noexcept { return hash; }
74 HashedString GetHashedString() const noexcept { return HashedString(literal); }
75 operator HashedString() const noexcept { return HashedString(literal); }
76
77 const char* ToString() const {
78 auto map = Grindstone::HashedString::GetHashedStringMap();
79 if (map != nullptr) {
80 auto it = map->find(hash);
81 if (it == map->end()) {
82 map->emplace(hash, Grindstone::String(literal));
83 }
84 }
85
86 return literal;
87 }
88
89 private:
90 uint64_t hash;
91 const char* literal;
92 };
93}
94
95namespace std {
96 template <>
97 struct hash<Grindstone::HashedString> {
98 std::size_t operator()(const Grindstone::HashedString& c) const {
99 uint64_t hash = c.GetHash();
100 return static_cast<std::size_t>(hash);
101 }
102 };
103}
Definition HashedString.hpp:54
Definition HashedString.hpp:9