Iterating over selected vectors from std :: tuple of vectors

I am trying to implement the entity-component-system template statically in C ++ 11/14. I have managed to create a container for my objects and I am trying to add functionality to work with the data inside. This is the code I have so far:

template<typename... Components>
struct Entity {
  std::tuple<Components...> comps;

  Entity() = default;
  Entity(Components... cs) {
    comps = std::make_tuple(cs...);
  };
};

template<typename... EntityTypes>
struct EntityCollection {
  std::tuple<std::vector<EntityTypes>...> entities;

  template<typename EntityType>
  void add(EntityType e) {
    auto& cont = std::get<std::vector<EntityType>>(entities);
    cont.push_back(e);
  };

  template<typename... Components, typename F>
  void for_each_containing(F f) {
     // todo... apply f on all entities for which 'Components' 
     // are a subset of those in the entity.
  };

};

      

I've seen code that iterates over all the elements in a tuple using boost-fusion (and with an stl adapter) and generic lambdas:

// inside entity class
void update() {
  for_each(comps, [](auto& c) { c.update(); }; // assuming all components has an update function
};

      

However, I would like to apply the function to all objects that contain a specific set of components, for example for the for_each_conttaining function. I am trying to write something using filter_view in fusion, however with no success ...

Any help is greatly appreciated!

+3


source to share


1 answer


Utilities:

#include <iostream>
#include <tuple>
#include <vector>
#include <type_traits>

template <typename... Ts>
struct pack {};

template <typename T, typename... Ts>
constexpr bool Contains = false;

template <typename T, typename U, typename... Ts>
constexpr bool Contains<T, U, Ts...> = Contains<T, Ts...>;

template <typename T, typename... Ts>
constexpr bool Contains<T, T, Ts...> = true;

template <bool...> 
constexpr bool All = true;

template <bool Head, bool... Tail>
constexpr bool All<Head, Tail...> = Head && All<Tail...>;

template <typename Subset, typename Set>
struct IsSubset;

template <typename... Ts, typename... Us>
struct IsSubset<pack<Ts...>, pack<Us...>>
    : std::integral_constant<bool, All<Contains<Ts, Us...>...>> {};

      

Entity

using a member function update

:

template <typename... Components>
struct Entity
{
    std::tuple<Components...> comps;

    Entity() = default;

    Entity(Components... cs)
        : comps(cs...)
    {
    }

    void update()
    {
        std::cout << "Updating " << __PRETTY_FUNCTION__ << std::endl;
    }
};

      

EntityCollection

using the function apply

:

template <typename... EntityTypes>
struct EntityCollection
{
    std::tuple<std::vector<EntityTypes>...> entities;

    template <typename EntityType>
    void add(EntityType e)
    {
        auto& cont = std::get<std::vector<EntityType>>(entities);
        cont.push_back(e);
    }

    template <typename... Components, typename F>
    void for_each_containing(F f)
    {        
        using expander = int[];
        (void)expander{ 0, (void(
             apply<Components...>(std::get<std::vector<EntityTypes>>(entities), f)
        ), 0)... };
    }

    template <typename... Components, typename... Ts, typename F>
    auto apply(std::vector<Entity<Ts...>>& entities, F f)
        -> std::enable_if_t<IsSubset<pack<Components...>, pack<Ts...>>{}>
    {
        for (auto& v : entities)
        {
            f(v);
        }
    }   

    template <typename... Components, typename... Ts, typename F>
    auto apply(std::vector<Entity<Ts...>>&, F)
        -> std::enable_if_t<!IsSubset<pack<Components...>, pack<Ts...>>{}>
    {
    } 
};

      



Tests:

int main()
{
    Entity<int, short> a;
    Entity<long, int> b;
    Entity<float, double> c;

    EntityCollection<Entity<int, short>,
                     Entity<long, int>,
                     Entity<float, double>> collection;

    collection.add(a);
    collection.add(b);
    collection.add(c);

    collection.for_each_containing<int>([](auto& c){ c.update(); });

    collection.for_each_containing<float>([](auto& c){ c.update(); });

    collection.for_each_containing<short>([](auto& c){ c.update(); });

    collection.for_each_containing<short, int>([](auto& c){ c.update(); });
}

      

Output:

Updating void Entity<int, short>::update() [Components = <int, short>]
Updating void Entity<long, int>::update() [Components = <long, int>]

Updating void Entity<float, double>::update() [Components = <float, double>]

Updating void Entity<int, short>::update() [Components = <int, short>]

Updating void Entity<int, short>::update() [Components = <int, short>]

      

DEMO

+5


source







All Articles