diff options
author | Loek Le Blansch <loek@pipeframe.xyz> | 2024-10-12 20:54:07 +0200 |
---|---|---|
committer | Loek Le Blansch <loek@pipeframe.xyz> | 2024-10-12 20:54:07 +0200 |
commit | 5d041cce13da66aa3457d236579a9ac90ebf7618 (patch) | |
tree | 2107c4cad158bb3875faa838c12e064d6aba03f2 /src | |
parent | 579824011d5e8776e2079d6624a39535517760ff (diff) |
fix constructor/destructor member visibility for component manager
Diffstat (limited to 'src')
-rw-r--r-- | src/crepe/Component.cpp | 1 | ||||
-rw-r--r-- | src/crepe/Component.h | 6 | ||||
-rw-r--r-- | src/crepe/ComponentManager.cpp | 12 | ||||
-rw-r--r-- | src/crepe/ComponentManager.h | 3 | ||||
-rw-r--r-- | src/crepe/ComponentManager.hpp | 86 | ||||
-rw-r--r-- | src/crepe/api/BehaviorScript.h | 7 | ||||
-rw-r--r-- | src/crepe/util/log.h | 2 |
7 files changed, 61 insertions, 56 deletions
diff --git a/src/crepe/Component.cpp b/src/crepe/Component.cpp index d14159c..737f30a 100644 --- a/src/crepe/Component.cpp +++ b/src/crepe/Component.cpp @@ -2,4 +2,3 @@ using namespace crepe; -Component::Component() : active(true) {} diff --git a/src/crepe/Component.h b/src/crepe/Component.h index 16a4ce5..00b2164 100644 --- a/src/crepe/Component.h +++ b/src/crepe/Component.h @@ -3,12 +3,14 @@ namespace crepe { class Component { +protected: + Component() = default; public: - Component(); + virtual ~Component() = default; // TODO: shouldn't this constructor be deleted because this class will never // directly be instantiated? - bool active; + bool active = true; }; } // namespace crepe diff --git a/src/crepe/ComponentManager.cpp b/src/crepe/ComponentManager.cpp index 9a3fec7..8aa9d4e 100644 --- a/src/crepe/ComponentManager.cpp +++ b/src/crepe/ComponentManager.cpp @@ -1,3 +1,4 @@ +#include "util/log.h" #include "ComponentManager.h" using namespace crepe; @@ -20,5 +21,14 @@ void ComponentManager::delete_all_components_of_id(uint32_t id) { void ComponentManager::delete_all_components() { // Clear the whole unordered_map<> - components.clear(); + this->components.clear(); } + +ComponentManager::ComponentManager() { + dbg_trace(); +} + +ComponentManager::~ComponentManager() { + dbg_trace(); +} + diff --git a/src/crepe/ComponentManager.h b/src/crepe/ComponentManager.h index 2ab9dc8..eab9b45 100644 --- a/src/crepe/ComponentManager.h +++ b/src/crepe/ComponentManager.h @@ -44,7 +44,8 @@ public: std::vector<std::reference_wrapper<T>> get_components_by_type() const; private: - ComponentManager() = default; + ComponentManager(); + virtual ~ComponentManager(); /* * The std::unordered_map<std::type_index, std::vector<std::vector<std::unique_ptr<Component>>>> below might seem a bit strange, let me explain this structure: diff --git a/src/crepe/ComponentManager.hpp b/src/crepe/ComponentManager.hpp index 084cd33..8fc1cba 100644 --- a/src/crepe/ComponentManager.hpp +++ b/src/crepe/ComponentManager.hpp @@ -60,11 +60,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> @@ -78,30 +76,25 @@ 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; } @@ -119,30 +112,27 @@ 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<> diff --git a/src/crepe/api/BehaviorScript.h b/src/crepe/api/BehaviorScript.h index e9542c1..ba60a8c 100644 --- a/src/crepe/api/BehaviorScript.h +++ b/src/crepe/api/BehaviorScript.h @@ -6,11 +6,14 @@ namespace crepe::api { class BehaviorScript : public Script, public Component { +protected: // only allow ComponentManager to instantiate scripts friend class ComponentManager; - -protected: BehaviorScript(); +public: + // but allow uniqe_ptr to call the destructor (THIS IS VERY IMPORTANT) + virtual ~BehaviorScript() = default; + }; } diff --git a/src/crepe/util/log.h b/src/crepe/util/log.h index 2b0fbe1..fa5f633 100644 --- a/src/crepe/util/log.h +++ b/src/crepe/util/log.h @@ -15,7 +15,7 @@ // very illegal global function-style macros // NOLINTBEGIN #define dbg_logf(fmt, ...) _crepe_logf_here(": " fmt, __VA_ARGS__) -#define dbg_log(str) _crepe_logf_here(": %s", str) +#define dbg_log(str) _crepe_logf_here("%s: " str, "") #define dbg_trace() _crepe_logf_here("%s", "") // NOLINTEND |