aboutsummaryrefslogtreecommitdiff
path: root/src/crepe/ComponentManager.hpp
diff options
context:
space:
mode:
authorLoek Le Blansch <loek@pipeframe.xyz>2024-10-21 10:42:10 +0200
committerLoek Le Blansch <loek@pipeframe.xyz>2024-10-21 10:42:10 +0200
commit157cdabe941be14ca72022e7a7c8c55a582a7c1c (patch)
treeca09caaa0b4a8ec8ba7c6a109445be4339f08cb1 /src/crepe/ComponentManager.hpp
parent58dfdba241b501d4b1b9688b44ee775507ec325b (diff)
parent0fdebc7d7a143ad2b9b5ed6a596bf4bb1f0f7d7f (diff)
merge loek/scripts into loek/config
Diffstat (limited to 'src/crepe/ComponentManager.hpp')
-rw-r--r--src/crepe/ComponentManager.hpp92
1 files changed, 43 insertions, 49 deletions
diff --git a/src/crepe/ComponentManager.hpp b/src/crepe/ComponentManager.hpp
index 2ea0c70..2377a94 100644
--- a/src/crepe/ComponentManager.hpp
+++ b/src/crepe/ComponentManager.hpp
@@ -7,7 +7,7 @@
namespace crepe {
template <class T, typename... Args>
-void ComponentManager::add_component(uint32_t id, Args &&... args) {
+T & ComponentManager::add_component(uint32_t id, Args &&... args) {
using namespace std;
static_assert(is_base_of<Component, T>::value,
@@ -33,6 +33,8 @@ void ComponentManager::add_component(uint32_t id, Args &&... args) {
T * instance = new T(forward<Args>(args)...);
// store its unique_ptr in the vector<>
components[type][id].push_back(unique_ptr<T>(instance));
+
+ return *instance;
}
template <typename T>
@@ -61,11 +63,9 @@ 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);
- // Find the type (in the unordered_map<>)
- if (components.find(type) != components.end()) {
- // Clear the whole vector<> of this specific type
- components[type].clear();
- }
+ if (components.find(type) == components.end()) return;
+
+ components[type].clear();
}
template <typename T>
@@ -79,30 +79,26 @@ ComponentManager::get_components_by_id(uint32_t id) const {
// Create an empty vector<>
vector<reference_wrapper<T>> component_vector;
- // Find the type (in the unordered_map<>)
- if (components.find(type) != components.end()) {
- // Get the correct vector<>
- const vector<vector<unique_ptr<Component>>> & component_array
- = components.at(type);
+ if (components.find(type) == components.end()) return component_vector;
- // Make sure that the id (that we are looking for) is within the boundaries of the vector<>
- if (id < component_array.size()) {
- // Loop trough the whole vector<>
- for (const unique_ptr<Component> & component_ptr :
- component_array[id]) {
- // Cast the unique_ptr to a raw pointer
- T * casted_component = static_cast<T *>(component_ptr.get());
-
- // Ensure that the cast was successful
- if (casted_component) {
- // Add the dereferenced raw pointer to the vector<>
- component_vector.push_back(*casted_component);
- }
- }
- }
+ // Get the correct vector<>
+ const vector<vector<unique_ptr<Component>>> & component_array
+ = 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;
+
+ // Loop trough the whole vector<>
+ for (const unique_ptr<Component> & component_ptr : component_array[id]) {
+ // Cast the unique_ptr to a raw pointer
+ T * casted_component = static_cast<T *>(component_ptr.get());
+
+ if (casted_component == nullptr) continue;
+
+ // Add the dereferenced raw pointer to the vector<>
+ component_vector.push_back(*casted_component);
}
- // Return the vector<>
return component_vector;
}
@@ -120,30 +116,28 @@ ComponentManager::get_components_by_type() const {
// uint32_t id = 0;
// Find the type (in the unordered_map<>)
- if (components.find(type) != components.end()) {
+ if (components.find(type) == components.end()) return component_vector;
- // Get the correct vector<>
- const vector<vector<unique_ptr<Component>>> & component_array
- = components.at(type);
-
- // Loop through the whole vector<>
- for (const vector<unique_ptr<Component>> & component :
- component_array) {
- // Loop trough the whole vector<>
- for (const unique_ptr<Component> & component_ptr : component) {
- // Cast the unique_ptr to a raw pointer
- T * casted_component = static_cast<T *>(component_ptr.get());
-
- // Ensure that the cast was successful
- if (casted_component) {
- // Pair the dereferenced raw pointer and the id and add it to the vector<>
- component_vector.emplace_back(ref(*casted_component));
- }
- }
-
- // Increase the id (the id will also be stored in the returned vector<>)
- //++id;
+ // Get the correct vector<>
+ const vector<vector<unique_ptr<Component>>> & component_array
+ = components.at(type);
+
+ // Loop through the whole vector<>
+ for (const vector<unique_ptr<Component>> & component : component_array) {
+ // Loop trough the whole vector<>
+ for (const unique_ptr<Component> & component_ptr : component) {
+ // Cast the unique_ptr to a raw pointer
+ T * casted_component = static_cast<T *>(component_ptr.get());
+
+ // 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<>
+ component_vector.emplace_back(ref(*casted_component));
}
+
+ // Increase the id (the id will also be stored in the returned vector<>)
+ //++id;
}
// Return the vector<>