diff --git a/lib/wal/sources/Entity/Entity.hpp b/lib/wal/sources/Entity/Entity.hpp index c4a4e9cf..50770d18 100644 --- a/lib/wal/sources/Entity/Entity.hpp +++ b/lib/wal/sources/Entity/Entity.hpp @@ -54,25 +54,29 @@ namespace WAL void setDisable(bool disabled); //! @brief Get a component of a specific type + //! @tparam The type of the component //! @throw NotFoundError if the component could not be found + //! @return The component of the requested type. template T &getComponent() { - const std::type_index &type = typeid(T); - auto existing = this->_components.find(type); - if (existing == this->_components.end()) - throw NotFoundError("No component could be found with the type \"" + std::string(type.name()) + "\"."); - return *static_cast(existing->second.get()); + T *ret = this->tryGetComponent(); + if (ret == nullptr) + throw NotFoundError("No component could be found with the type \"" + std::string(typeid(T).name()) + "\"."); + return *ret; } + //! @brief Get a component of a specific type or null if not found. + //! @tparam The type of the component + //! @return The component or nullptr if not found. template - T *getComponentOrDefault() + T *tryGetComponent() { const std::type_index &type = typeid(T); auto existing = this->_components.find(type); if (existing == this->_components.end()) return nullptr; - return *static_cast(existing->second.get()); + return static_cast(existing->second.get()); } //! @brief Check if this entity has a component. diff --git a/lib/wal/sources/View/View.hpp b/lib/wal/sources/View/View.hpp index 22c02b60..1c3ba2d9 100644 --- a/lib/wal/sources/View/View.hpp +++ b/lib/wal/sources/View/View.hpp @@ -153,18 +153,19 @@ namespace WAL { this->_types = {typeid(Components)...}; for (auto &entity : scene) { - auto tuple = std::make_tuple(entity, entity.getComponentOrDefault()...); - std::apply(&this->_entities.emplace_back, tuple); + auto tuple = std::make_tuple(entity.tryGetComponent()...); + if (std::apply([](const auto *...component) {return ((component == nullptr) || ...);}, tuple)) + continue; + std::apply([&](auto *...component) { + this->_entities.emplace_back(entity, *component...); + }, tuple); } - // std::copy_if(scene.begin(), scene.end(), std::back_inserter(this->entities), [](Entity &entity) { -// return (entity.hasComponent() && ...); -// }); } //! @brief Copying a view is not possible since a view must be managed by a scene. View(const View &) = delete; //! @brief A default destructor - ~View() = default; + ~View() override = default; //! @brief A view is not assignable. View &operator=(const View &) = delete; };