David Peicho
David Peicho

Reputation: 492

Retrieve vector of vector according to contained type

I am working on a small 3D engine, and I was wondering how to avoid looping through my whole hierarchy (models, lights) each time to access lights, materials, etc...

I decided to create a singleton DataProvider, keeping all the lists, the list of pointers to materials, or list of pointers to lights, any list of type.

However, my code looks too intrusive, each time I have to add a new list, I have to add it to the class attributes.

I finally decided to do something like:

class DataProvider
{
  public:
    using DataListContainer = std::vector<std::vector<boost::any>>;
    ...
private:
   DataListContainer data_;

The problem that I am copping to is to get the inner vector according to a given type? The whole process here seems to be messy. In addition to this, using the boost::any type can not really guaranty that the type inside the inner vector is unique, it could be mixed. What would be the proper way to store a vector of vectors, retrieving inner vectors according to the type they store?

Upvotes: 0

Views: 60

Answers (1)

Jarod42
Jarod42

Reputation: 218323

If I understand correctly what you want, you may use something like:

template <typename ... Ts>
class DataProviderT
{
public:
    using DataListContainer = std::tuple<std::vector<Ts>...>;

    template <typename T>
    std::vector<T>& GetVector() { return std::get<std::vector<T>>(data_); }

private:
   DataListContainer data_;
};

using DataProvider = DataProviderT<Model, Light>;

Upvotes: 3

Related Questions