KTech 1.1.0
C++ 2D terminal game engine library
Loading...
Searching...
No Matches
cachingregistry.hpp
1/*
2 KTech, Kaup's C++ 2D terminal game engine library.
3 Copyright (C) 2023-2025 Ethan Kaufman (AKA Kaup)
4
5 This file is part of KTech.
6
7 KTech is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 any later version.
11
12 KTech is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with KTech. If not, see <https://www.gnu.org/licenses/>.
19*/
20
21#pragma once
22
23#define KTECH_DEFINITION
24#include "../ktech.hpp"
25#undef KTECH_DEFINITION
26
27#include <cstddef>
28#include <vector>
29
47template<typename T>
49{
50public:
59 auto operator[](const ID<T>& id) -> T*
60 {
61 if (!m_vec.empty())
62 {
63 for (size_t i = (id.m_i < m_vec.size() ? id.m_i : m_vec.size() - 1);; i--)
64 {
65 if (m_vec[i]->m_id == id)
66 {
67 id.m_i = i;
68 return m_vec[i];
69 }
70 if (i == 0)
71 {
72 break;
73 }
74 }
75 }
76 id.m_i = 0;
77 return nullptr;
78 }
79
88 auto Exists(const ID<T>& id) -> bool
89 {
90 return IDToIndex(id) != m_vec.size();
91 }
92
93private:
94 std::vector<T*> m_vec;
95
96 // Adds the pointer to the container.
97 // Automatically called by objects, layers, cameras and maps for themselves.
98 // You shouldn't call this manually on a structure.
99 auto Add(T* structure) -> ID<T>
100 {
101 structure->m_id.m_i = m_vec.size();
102 m_vec.push_back(structure);
103 return m_vec[m_vec.size() - 1]->m_id;
104 }
105
106 // Remove a structure from storage (doesn't delete it's memory).
107 // Returns true if the structure was found and removed.
108 // Returns false if the structure is missing.
109 auto Remove(const ID<T>& id) -> bool
110 {
111 size_t toRemove = IDToIndex(id);
112 if (toRemove == m_vec.size())
113 {
114 return false;
115 }
116 m_vec.erase(m_vec.begin() + toRemove);
117 return true;
118 }
119
120 // Returns the valid index of the ID.
121 // If the UUID is missing, return the size of the array making the index invalid.
122 auto IDToIndex(const ID<T>& id) -> size_t
123 {
124 if (!m_vec.empty())
125 {
126 for (size_t i = (id.m_i < m_vec.size() ? id.m_i : m_vec.size() - 1);; i--)
127 {
128 if (m_vec[i]->m_id == id)
129 {
130 id.m_i = i;
131 return i;
132 }
133 if (i == 0)
134 {
135 break;
136 }
137 }
138 }
139 id.m_i = 0;
140 return m_vec.size();
141 }
142
143 friend T;
144 friend class Memory;
145};
Registry that retrieves world structure pointers by serializable ID.
Definition cachingregistry.hpp:49
auto Exists(const ID< T > &id) -> bool
Check if an ID matches a registered structure.
Definition cachingregistry.hpp:88
auto operator[](const ID< T > &id) -> T *
Retrieve structure using its ID.
Definition cachingregistry.hpp:59
Serializable world structure identifier.
Definition id.hpp:38