aboutsummaryrefslogtreecommitdiff
path: root/src/crepe/ComponentManager.hpp
diff options
context:
space:
mode:
authorLoek Le Blansch <loek@pipeframe.xyz>2024-11-07 20:08:09 +0100
committerLoek Le Blansch <loek@pipeframe.xyz>2024-11-07 20:08:09 +0100
commit1c4156ee127b14760ed3b1a0cd16ad12180c7ac6 (patch)
tree211505d5328f24c9f9beabf8f874d9e13ef92130 /src/crepe/ComponentManager.hpp
parent9df087ede0b539ecbd2778236c7d1143362b384d (diff)
parent3d2428af8e8d9d49b4ade52d4806a7dae4cf1ab8 (diff)
merge `master` into `loek/savemgr`
Diffstat (limited to 'src/crepe/ComponentManager.hpp')
-rw-r--r--src/crepe/ComponentManager.hpp59
1 files changed, 35 insertions, 24 deletions
diff --git a/src/crepe/ComponentManager.hpp b/src/crepe/ComponentManager.hpp
index 9b07f13..98efb49 100644
--- a/src/crepe/ComponentManager.hpp
+++ b/src/crepe/ComponentManager.hpp
@@ -3,11 +3,12 @@
#include <type_traits>
#include "ComponentManager.h"
+#include "types.h"
namespace crepe {
template <class T, typename... Args>
-T & ComponentManager::add_component(uint32_t id, Args &&... args) {
+T & ComponentManager::add_component(game_object_id_t id, Args &&... args) {
using namespace std;
static_assert(is_base_of<Component, T>::value,
@@ -17,38 +18,51 @@ T & ComponentManager::add_component(uint32_t id, Args &&... args) {
type_index type = typeid(T);
// Check if this component type is already in the unordered_map<>
- if (components.find(type) == components.end()) {
+ if (this->components.find(type) == this->components.end()) {
//If not, create a new (empty) vector<> of vector<unique_ptr<Component>>
- components[type] = vector<vector<unique_ptr<Component>>>();
+ this->components[type] = vector<vector<unique_ptr<Component>>>();
}
// Resize the vector<> if the id is greater than the current size
- if (id >= components[type].size()) {
+ if (id >= this->components[type].size()) {
// Initialize new slots to nullptr (resize does automatically init to nullptr)
- components[type].resize(id + 1);
+ this->components[type].resize(id + 1);
}
// Create a new component of type T (arguments directly forwarded). The
// constructor must be called by ComponentManager.
- T * instance = new T(id, forward<Args>(args)...);
+ T * instance_ptr = new T(id, forward<Args>(args)...);
+ if (instance_ptr == nullptr) throw std::bad_alloc();
+
+ T & instance_ref = *instance_ptr;
+ unique_ptr<T> instance = unique_ptr<T>(instance_ptr);
+
+ // Check if the vector size is not greater than get_instances_max
+ int max_instances = instance->get_instances_max();
+ if (max_instances != -1 && components[type][id].size() >= max_instances) {
+ // TODO: Exception
+ throw std::runtime_error(
+ "Exceeded maximum number of instances for this component type");
+ }
+
// store its unique_ptr in the vector<>
- components[type][id].push_back(unique_ptr<T>(instance));
+ this->components[type][id].push_back(std::move(instance));
- return *instance;
+ return instance_ref;
}
template <typename T>
-void ComponentManager::delete_components_by_id(uint32_t id) {
+void ComponentManager::delete_components_by_id(game_object_id_t id) {
using namespace std;
// Determine the type of T (this is used as the key of the unordered_map<>)
type_index type = typeid(T);
// Find the type (in the unordered_map<>)
- if (components.find(type) != components.end()) {
+ if (this->components.find(type) != this->components.end()) {
// Get the correct vector<>
vector<vector<unique_ptr<Component>>> & component_array
- = components[type];
+ = this->components[type];
// Make sure that the id (that we are looking for) is within the boundaries of the vector<>
if (id < component_array.size()) {
@@ -63,14 +77,14 @@ void ComponentManager::delete_components() {
// Determine the type of T (this is used as the key of the unordered_map<>)
std::type_index type = typeid(T);
- if (components.find(type) == components.end()) return;
+ if (this->components.find(type) == this->components.end()) return;
- components[type].clear();
+ this->components[type].clear();
}
template <typename T>
std::vector<std::reference_wrapper<T>>
-ComponentManager::get_components_by_id(uint32_t id) const {
+ComponentManager::get_components_by_id(game_object_id_t id) const {
using namespace std;
// Determine the type of T (this is used as the key of the unordered_map<>)
@@ -79,11 +93,12 @@ ComponentManager::get_components_by_id(uint32_t id) const {
// Create an empty vector<>
vector<reference_wrapper<T>> component_vector;
- if (components.find(type) == components.end()) return component_vector;
+ if (this->components.find(type) == this->components.end())
+ return component_vector;
// Get the correct vector<>
const vector<vector<unique_ptr<Component>>> & component_array
- = components.at(type);
+ = this->components.at(type);
// Make sure that the id (that we are looking for) is within the boundaries of the vector<>
if (id >= component_array.size()) return component_vector;
@@ -112,15 +127,14 @@ ComponentManager::get_components_by_type() const {
// Create an empty vector<>
vector<reference_wrapper<T>> component_vector;
- // Set the id to 0 (the id will also be stored in the returned vector<>)
- // uint32_t id = 0;
// Find the type (in the unordered_map<>)
- if (components.find(type) == components.end()) return component_vector;
+ if (this->components.find(type) == this->components.end())
+ return component_vector;
// Get the correct vector<>
const vector<vector<unique_ptr<Component>>> & component_array
- = components.at(type);
+ = this->components.at(type);
// Loop through the whole vector<>
for (const vector<unique_ptr<Component>> & component : component_array) {
@@ -132,12 +146,9 @@ ComponentManager::get_components_by_type() const {
// Ensure that the cast was successful
if (casted_component == nullptr) continue;
- // Pair the dereferenced raw pointer and the id and add it to the vector<>
+ // Add the dereferenced raw pointer to the vector<>
component_vector.emplace_back(ref(*casted_component));
}
-
- // Increase the id (the id will also be stored in the returned vector<>)
- //++id;
}
// Return the vector<>