aboutsummaryrefslogtreecommitdiff
path: root/src/openvic-simulation/types/IdentifierRegistry.hpp
blob: b2f52b26326c4dc5a5e793ef99c4d10caa895f25 (plain) (blame)
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
#pragma once

#include <map>
#include <vector>

#include "openvic-simulation/dataloader/NodeTools.hpp"
#include "openvic-simulation/utility/Logger.hpp"

#define REF_GETTERS(var) \
   constexpr decltype(var)& get_##var() { return var; } \
   constexpr decltype(var) const& get_##var() const { return var; }

namespace OpenVic {
   /*
    * Base class for objects with a non-empty string identifier,
    * uniquely named instances of which can be entered into an
    * IdentifierRegistry instance.
    */
   class HasIdentifier {
      const std::string identifier;

   protected:
      HasIdentifier(std::string_view new_identifier);

   public:
      HasIdentifier(HasIdentifier const&) = delete;
      HasIdentifier(HasIdentifier&&) = default;
      HasIdentifier& operator=(HasIdentifier const&) = delete;
      HasIdentifier& operator=(HasIdentifier&&) = delete;

      std::string_view get_identifier() const;
   };

   std::ostream& operator<<(std::ostream& stream, HasIdentifier const& obj);
   std::ostream& operator<<(std::ostream& stream, HasIdentifier const* obj);

   /*
    * Base class for objects with associated colour information.
    */
   class HasColour {
      const colour_t colour;

   protected:
      HasColour(const colour_t new_colour, bool can_be_null, bool can_have_alpha);

   public:
      HasColour(HasColour const&) = delete;
      HasColour(HasColour&&) = default;
      HasColour& operator=(HasColour const&) = delete;
      HasColour& operator=(HasColour&&) = delete;

      colour_t get_colour() const;
      std::string colour_to_hex_string() const;
   };

   /*
    * Base class for objects with a unique string identifier
    * and associated colour information.
    */
   class HasIdentifierAndColour : public HasIdentifier, public HasColour {
   protected:
      HasIdentifierAndColour(std::string_view new_identifier, const colour_t new_colour, bool can_be_null, bool can_have_alpha);

   public:
      HasIdentifierAndColour(HasIdentifierAndColour const&) = delete;
      HasIdentifierAndColour(HasIdentifierAndColour&&) = default;
      HasIdentifierAndColour& operator=(HasIdentifierAndColour const&) = delete;
      HasIdentifierAndColour& operator=(HasIdentifierAndColour&&) = delete;
   };

   template<typename T>
   using decimal_map_t = std::map<T, fixed_point_t>;

   template<typename T>
   constexpr typename decimal_map_t<T>::value_type get_largest_item(decimal_map_t<T> const& map) {
      constexpr auto pred = [](typename decimal_map_t<T>::value_type a, typename decimal_map_t<T>::value_type b) -> bool {
         return a.second < b.second;
      };
      const typename decimal_map_t<T>::const_iterator result = std::max_element(
         map.begin(), map.end(), pred
      );
      if (result != map.end()) {
         return *result;
      } else {
         return { nullptr, -1 };
      }
   }

   using distribution_t = decimal_map_t<HasIdentifierAndColour const*>;

   /* Callbacks for trying to add duplicate keys via UniqueKeyRegistry::add_item */
   static bool duplicate_fail_callback(std::string_view registry_name, std::string_view duplicate_identifier) {
      Logger::error("Failure adding item to the ", registry_name, " registry - an item with the identifier \"",
         duplicate_identifier, "\" already exists!");
      return false;
   }
   static bool duplicate_warning_callback(std::string_view registry_name, std::string_view duplicate_identifier) {
      Logger::warning("Warning adding item to the ", registry_name, " registry - an item with the identifier \"",
         duplicate_identifier, "\" already exists!");
      return true;
   }
   static bool duplicate_ignore_callback(std::string_view registry_name, std::string_view duplicate_identifier) {
      return true;
   }

   template<typename T>
   using get_identifier_func_t = std::string_view(T::*)(void) const;

   template<typename _Base, std::derived_from<_Base> _Type, get_identifier_func_t<_Base> get_identifier,
      typename _Storage, _Type* (*get_ptr)(_Storage&), _Type const* (*get_cptr)(_Storage const&)>
   class UniqueKeyRegistry {

      const std::string name;
      const bool log_lock;
      std::vector<_Storage> items;
      bool locked = false;
      string_map_t<size_t> identifier_index_map;

   public:
      using value_type = _Type;
      using storage_type = _Storage;

      UniqueKeyRegistry(std::string_view new_name, bool new_log_lock = true)
         : name { new_name }, log_lock { new_log_lock } {}

      std::string_view get_name() const {
         return name;
      }

      bool add_item(storage_type&& item, NodeTools::callback_t<std::string_view, std::string_view> duplicate_callback = duplicate_fail_callback) {
         if (locked) {
            Logger::error("Cannot add item to the ", name, " registry - locked!");
            return false;
         }
         value_type const* new_item = (*get_cptr)(item);
         const std::string_view new_identifier = (new_item->*get_identifier)();
         value_type const* old_item = get_item_by_identifier(new_identifier);
         if (old_item != nullptr) {
            return duplicate_callback(name, new_identifier);
         }
         identifier_index_map.emplace(new_identifier, items.size());
         items.push_back(std::move(item));
         return true;
      }

      void lock() {
         if (locked) {
            Logger::error("Failed to lock ", name, " registry - already locked!");
         } else {
            locked = true;
            if (log_lock) Logger::info("Locked ", name, " registry after registering ", size(), " items");
         }
      }

      bool is_locked() const {
         return locked;
      }

      void reset() {
         identifier_index_map.clear();
         items.clear();
         locked = false;
      }

      size_t size() const {
         return items.size();
      }

      bool empty() const {
         return items.empty();
      }

      void reserve(size_t size) {
         if (locked) {
            Logger::error("Failed to reserve space for ", size, " items in ", name, " registry - already locked!");
         } else {
            items.reserve(size);
         }
      }

      value_type* get_item_by_identifier(std::string_view identifier) {
         const typename decltype(identifier_index_map)::const_iterator it = identifier_index_map.find(identifier);
         if (it != identifier_index_map.end()) return (*get_ptr)(items[it->second]);
         return nullptr;
      }

      value_type const* get_item_by_identifier(std::string_view identifier) const {
         const typename decltype(identifier_index_map)::const_iterator it = identifier_index_map.find(identifier);
         if (it != identifier_index_map.end()) return (*get_cptr)(items[it->second]);
         return nullptr;
      }

      bool has_identifier(std::string_view identifier) const {
         return get_item_by_identifier(identifier) != nullptr;
      }

      value_type* get_item_by_index(size_t index) {
         return index < items.size() ? &items[index] : nullptr;
      }

      value_type const* get_item_by_index(size_t index) const {
         return index < items.size() ? &items[index] : nullptr;
      }

      bool has_index(size_t index) const {
         return get_item_by_index(index) != nullptr;
      }

      REF_GETTERS(items)

      std::vector<std::string_view> get_item_identifiers() const {
         std::vector<std::string_view> identifiers;
         identifiers.reserve(items.size());
         for (typename decltype(identifier_index_map)::value_type const& entry : identifier_index_map) {
            identifiers.push_back(entry.first);
         }
         return identifiers;
      }

      NodeTools::callback_t<std::string_view> expect_item_identifier(NodeTools::callback_t<value_type&> callback) {
         return [this, callback](std::string_view identifier) -> bool {
            value_type* item = get_item_by_identifier(identifier);
            if (item != nullptr) return callback(*item);
            Logger::error("Invalid ", name, ": ", identifier);
            return false;
         };
      }

      NodeTools::callback_t<std::string_view> expect_item_identifier(NodeTools::callback_t<value_type const&> callback) const {
         return [this, callback](std::string_view identifier) -> bool {
            value_type const* item = get_item_by_identifier(identifier);
            if (item != nullptr) return callback(*item);
            Logger::error("Invalid ", name, ": ", identifier);
            return false;
         };
      }

      NodeTools::node_callback_t expect_item_dictionary(NodeTools::callback_t<value_type&, ast::NodeCPtr> callback) {
         return NodeTools::expect_dictionary([this, callback](std::string_view key, ast::NodeCPtr value) -> bool {
            value_type* item = get_item_by_identifier(key);
            if (item != nullptr) {
               return callback(*item, value);
            }
            Logger::error("Invalid ", name, " identifier: ", key);
            return false;
         });
      }

      NodeTools::node_callback_t expect_item_dictionary(NodeTools::callback_t<value_type const&, ast::NodeCPtr> callback) const {
         return NodeTools::expect_dictionary([this, callback](std::string_view key, ast::NodeCPtr value) -> bool {
            value_type const* item = get_item_by_identifier(key);
            if (item != nullptr) {
               return callback(*item, value);
            }
            Logger::error("Invalid ", name, " identifier: ", key);
            return false;
         });
      }

      NodeTools::node_callback_t expect_item_decimal_map(NodeTools::callback_t<decimal_map_t<value_type const*>&&> callback) const {
         return [this, callback](ast::NodeCPtr node) -> bool {
            decimal_map_t<value_type const*> map;
            bool ret = expect_item_dictionary([&map](value_type const& key, ast::NodeCPtr value) -> bool {
               fixed_point_t val;
               const bool ret = NodeTools::expect_fixed_point(NodeTools::assign_variable_callback(val))(value);
               map.emplace(&key, val);
               return ret;
            })(node);
            ret &= callback(std::move(map));
            return ret;
         };
      }
   };

   template<typename T>
   [[nodiscard]] inline constexpr T* _addressof(T& v) noexcept {
      return std::addressof<T>(v);
   }

   template<typename T>
   const T* _addressof(const T&&) = delete;

   template<typename _Base, std::derived_from<_Base> _Type, get_identifier_func_t<_Base> get_identifier>
   using ValueRegistry = UniqueKeyRegistry<_Base, _Type, get_identifier, _Type, _addressof<_Type>, _addressof<const _Type>>;

   template<typename _Type>
   constexpr _Type* get_ptr(std::unique_ptr<_Type>& storage) {
      return storage.get();
   }
   template<typename _Type>
   constexpr _Type const* get_cptr(std::unique_ptr<_Type> const& storage) {
      return storage.get();
   }

   template<typename _Base, std::derived_from<_Base> _Type, get_identifier_func_t<_Base> get_identifier>
   using InstanceRegistry = UniqueKeyRegistry<_Base, _Type, get_identifier, std::unique_ptr<_Type>,
      get_ptr<_Type>, get_cptr<_Type>>;

   template<std::derived_from<HasIdentifier> _Type>
   using IdentifierRegistry = ValueRegistry<HasIdentifier, _Type, &HasIdentifier::get_identifier>;

   template<std::derived_from<HasIdentifier> _Type>
   using IdentifierInstanceRegistry = InstanceRegistry<HasIdentifier, _Type, &HasIdentifier::get_identifier>;

#define IDENTIFIER_REGISTRY_ACCESSORS_CUSTOM_PLURAL(singular, plural) \
   void lock_##plural() { plural.lock(); } \
   bool plural##_are_locked() const { return plural.is_locked(); } \
   decltype(plural)::value_type const* get_##singular##_by_identifier(std::string_view identifier) const { \
      return plural.get_item_by_identifier(identifier); } \
   bool has_##singular##_identifier(std::string_view identifier) const { \
      return plural.has_identifier(identifier); } \
   size_t get_##singular##_count() const { \
      return plural.size(); } \
   std::vector<decltype(plural)::storage_type> const& get_##plural() const { \
      return plural.get_items(); } \
   std::vector<std::string_view> get_##singular##_identifiers() const { \
      return plural.get_item_identifiers(); } \
   NodeTools::callback_t<std::string_view> expect_##singular##_identifier(NodeTools::callback_t<decltype(plural)::value_type const&> callback) const { \
      return plural.expect_item_identifier(callback); } \
   NodeTools::node_callback_t expect_##singular##_dictionary(NodeTools::callback_t<decltype(plural)::value_type const&, ast::NodeCPtr> callback) const { \
      return plural.expect_item_dictionary(callback); } \
   NodeTools::node_callback_t expect_##singular##_decimal_map(NodeTools::callback_t<decimal_map_t<decltype(plural)::value_type const*>&&> callback) const { \
      return plural.expect_item_decimal_map(callback); }

#define IDENTIFIER_REGISTRY_NON_CONST_ACCESSORS_CUSTOM_PLURAL(singular, plural) \
   decltype(plural)::value_type* get_##singular##_by_identifier(std::string_view identifier) { \
      return plural.get_item_by_identifier(identifier); } \
   NodeTools::callback_t<std::string_view> expect_##singular##_identifier(NodeTools::callback_t<decltype(plural)::value_type&> callback) { \
      return plural.expect_item_identifier(callback); } \
   NodeTools::node_callback_t expect_##singular##_dictionary(NodeTools::callback_t<decltype(plural)::value_type&, ast::NodeCPtr> callback) { \
      return plural.expect_item_dictionary(callback); }

#define IDENTIFIER_REGISTRY_ACCESSORS(name) IDENTIFIER_REGISTRY_ACCESSORS_CUSTOM_PLURAL(name, name##s)
#define IDENTIFIER_REGISTRY_NON_CONST_ACCESSORS(name) IDENTIFIER_REGISTRY_NON_CONST_ACCESSORS_CUSTOM_PLURAL(name, name##s)
}