-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLookup.hpp
181 lines (152 loc) · 5.16 KB
/
Lookup.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#pragma once
#include <filesystem>
#include <functional>
#include <cstdint>
#include <unordered_map>
#include <unordered_set>
#include "nejlika.hpp"
namespace nejlika {
/**
* @brief A one-way mapping between symbols (names) and their corresponding numerical values.
*
* This is an active lookup, meaning that it is possible to add new symbols and their values to the lookup.
*
* A method is provided to wait for a symbol to be added to the lookup.
*/
class Lookup
{
public:
inline static const name core_prefix = "lego-universe";
/**
* @brief Constructs a Lookup object with the specified lookup file path.
*
* @param lookup The path to the lookup file.
* @throw If the lookup file could not be parsed.
*/
Lookup(const std::filesystem::path& lookup);
/**
* @brief Constructs a Lookup object from another Lookup object.
*
* @param other The other Lookup object.
*/
Lookup(const Lookup& other);
/**
* @brief Gets the value of the specified symbol.
*
* @param symbol The symbol to get the value of.
* @return The value of the specified symbol.
* @throw If the specified symbol does not exist.
*/
id GetValue(const name& symbol) const;
/**
* @brief Checks whether the specified symbol exists.
*
* @param symbol The symbol to check.
* @return Whether the specified symbol exists.
*/
bool Exists(const name& symbol) const;
/**
* @brief Checks whether any symbol has the specified value.
*
* @param value The value to check.
* @return Whether any symbol has the specified value.
*/
bool Exists(id value) const;
/**
* @brief Registers a new symbol with the specified value.
*
* @param symbol The symbol to register.
* @param value The value of the symbol.
* @param overwrite Whether to overwrite the value of an existing symbol.
* @throw If the specified symbol already exists with a different value and overwrite is false.
*/
void Register(const name& symbol, id value, bool overwrite = false);
/**
* @brief Set up a callback to be called when the specified symbol is registered.
*
* @param symbol The symbol to wait for.
* @param callback The callback to call when the symbol is registered.
*/
void AwaitValue(const name& symbol, std::function<void(id)> callback);
/**
* @brief Wait for a set of symbols to be registered.
*
* @param symbols The symbols to wait for.
* @param callback The callback to call when all symbols are registered.
*/
void AwaitValues(const std::unordered_set<name>& symbols, const std::function<void()>& callback);
/**
* @brief Set metadata of a specified symbol.
*
* @param symbol The symbol to set metadata of.
* @param metadata The metadata to set.
*/
void SetMetadata(const name& symbol, const std::string& metadata);
/**
* @brief Gets the metadata of a specified symbol.
*
* @param symbol The symbol to get metadata of.
* @return The metadata of the specified symbol or an empty string if the symbol does not exist.
*/
const std::string& GetMetadata(const name& symbol) const;
/**
* @brief Gets the path to the lookup file.
*
* @return The path to the lookup file.
*/
const std::filesystem::path& GetLookup() const;
/**
* @brief Gets the map of all symbols and their values.
*
* @return The map of all symbols and their values.
*/
const std::unordered_map<name, id>& GetMap() const;
/**
* @brief Sets the map of all symbols and their values.
*
* @param map The map of all symbols and their values.
*/
void SetMap(const std::unordered_map<name, id>& map);
/**
* @brief Get a set of all unresolved symbols.
*
* @return A set of all unresolved symbols.
*/
std::unordered_set<name> GetUnresolved() const;
/**
* Saves the lookup to file.
*/
void Save();
/**
* @brief Register a mapping of core symbols.
*
* @param symbols The symbols to register.
*/
void RegisterCoreSymbols(const std::unordered_map<name, id>& symbols);
/**
* @brief Checks whether the specified symbol is a core symbol.
*
* @param symbol The symbol to check.
* @param value The value of the core symbol.
* @return Whether the specified symbol is a core symbol.
*/
bool IsCoreSymbol(const name& symbol, id& value) const;
/**
* @brief Checks whether the specified symbol is a core symbol.
*
* A symbol is considered a core symbol if it is either:
* a number;
* or a string starting with the core_prefix.
*
* @param symbol The symbol to check.
* @return Whether the specified symbol is a core symbol.
*/
static bool IsCoreSymbol(const name& symbol);
private:
std::filesystem::path m_Lookup;
std::unordered_map<name, id> m_LookupMap;
std::unordered_map<name, std::string> m_Metadata;
std::unordered_map<name, std::vector<std::function<void(int)>>> m_Callbacks;
std::unordered_map<name, id> m_CoreSymbols;
};
} // namespace nejlika