Creating a templated view class

This commit is contained in:
Zoe Roux
2021-06-02 23:30:39 +02:00
parent a3804c170f
commit ae2e419832
3 changed files with 39 additions and 1 deletions
+1 -1
View File
@@ -17,6 +17,6 @@ add_library(wal
sources/Component/Component.cpp
sources/System/System.cpp
sources/Models/Callback.hpp
)
sources/View/View.hpp)
target_include_directories(wal PUBLIC sources)
+3
View File
@@ -7,6 +7,7 @@
#include <vector>
#include <functional>
#include "View/View.hpp"
#include "Entity/Entity.hpp"
namespace WAL
@@ -17,6 +18,8 @@ namespace WAL
private:
//! @brief The list of registered entities
std::vector<Entity> _entities = {};
//! @brief A list of cached views.
// std::vector<View> _views = {};
public:
//! @brief Get the list of entities.
std::vector<Entity> &getEntities();
+35
View File
@@ -0,0 +1,35 @@
//
// Created by Zoe Roux on 2021-06-02.
//
#pragma once
#include <vector>
#include <Entity/Entity.hpp>
namespace WAL
{
//! @brief A view caching entities containing requested components
template<typename ...Components>
class View
{
//! @brief A list of reference to entities that contains the
std::vector<std::reference_wrapper<Entity>> entities;
explicit View(std::vector<Entity> &entities)
: entities()
{
std::copy_if(entities.begin(), entities.end(), std::back_inserter(this->entities), [](Entity &entity) {
return (entity.hasComponent<Components>() && ...);
});
}
//! @brief A default copy constructor.
View(const View &) = default;
//! @brief A default destructor.
~View() = default;
//! @brief A View is assignable.
View &operator=(const View &) = default;
};
}