aboutsummaryrefslogtreecommitdiff
path: root/src/openvic/types/IdentifierRegistry.hpp
blob: 989db0177caeb24625073bfe09c89d4158c5a6e5 (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
#pragma once

#include <map>
#include <unordered_map>
#include <vector>

#include "openvic/types/Colour.hpp"
#include "openvic/types/Return.hpp"
#include "openvic/utility/Logger.hpp"

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(const std::string_view new_identifier);

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

      std::string const& get_identifier() const;
   };

   /*
    * 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);

   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;
      static std::string colour_to_hex_string(colour_t const colour);
   };

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

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

   using distribution_t = std::map<HasIdentifierAndColour const*, float>;

   distribution_t::value_type get_largest_item(distribution_t const& dist);

   /*
    * Template for a list of objects with unique string identifiers that can
    * be locked to prevent any further additions. The template argument T is
    * the type of object that the registry will store, and the second part ensures
    * that HasIdentifier is a base class of T.
    */
   template<class T, typename std::enable_if<std::is_base_of<HasIdentifier, T>::value>::type* = nullptr>
   class IdentifierRegistry {
      using identifier_index_map_t = std::map<std::string, size_t, std::less<void>>;

      const std::string name;
      std::vector<T> items;
      bool locked = false;
      identifier_index_map_t identifier_index_map;

   public:
      IdentifierRegistry(const std::string_view new_name) : name { new_name } {}
      return_t add_item(T&& item) {
         if (locked) {
            Logger::error("Cannot add item to the ", name, " registry - locked!");
            return FAILURE;
         }
         T const* old_item = get_item_by_identifier(item.get_identifier());
         if (old_item != nullptr) {
            Logger::error("Cannot add item to the ", name, " registry - an item with the identifier \"", item.get_identifier(), "\" already exists!");
            return FAILURE;
         }
         identifier_index_map[item.get_identifier()] = items.size();
         items.push_back(std::move(item));
         return SUCCESS;
      }
      void lock(bool log = true) {
         if (locked) {
            Logger::error("Failed to lock ", name, " registry - already locked!");
         } else {
            locked = true;
            if (log) 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);
         }
      }
      T* get_item_by_identifier(const std::string_view identifier) {
         const identifier_index_map_t::const_iterator it = identifier_index_map.find(identifier);
         if (it != identifier_index_map.end()) return &items[it->second];
         return nullptr;
      }
      T const* get_item_by_identifier(const std::string_view identifier) const {
         const identifier_index_map_t::const_iterator it = identifier_index_map.find(identifier);
         if (it != identifier_index_map.end()) return &items[it->second];
         return nullptr;
      }
      T* get_item_by_index(size_t index) {
         return index < items.size() ? &items[index] : nullptr;
      }
      T const* get_item_by_index(size_t index) const {
         return index < items.size() ? &items[index] : nullptr;
      }
      std::vector<T>& get_items() {
         return items;
      }
      std::vector<T> const& get_items() const {
         return items;
      }
   };
}