0

I'm trying to implement a variadic templated member function that can accept a std::function object with a varying number of template parameters or none at all.

Below is a simplified and compilable example of what I am attempting. I want to be able to dynamically add any user defined data object to my manager. Then, I want to be able to iterate over any of the data types stored within the manager using variadic templates. Where I've commented // HowTo? // is what I'm stuck on. How to I implement my commented out code using variadic templates?

I'm attempting to derive a solution from this post: Possible Solution?

#include <functional>
#include <map>
#include <memory>
#include <typeindex>
#include <vector>

constexpr auto Count = 10;

struct Base
{
    virtual ~Base()
    {
    }
};

template <typename T>
struct Container : public Base
{
    std::vector<T> data;
    std::vector<bool> valid;
};

struct Manager
{
    template <typename T>
    void add()
    {
        Container<T>* container{};

        if(this->containers[typeid(T)] == nullptr)
        {
            auto c = std::make_unique<Container<T>>();
            container = c.get();
            this->containers[typeid(T)] = std::move(c);
        }
        else
        {
            container = static_cast<Container<T>*>(this->containers[typeid(T)].get());
        }

        container->data.push_back(T());
        container->valid.push_back(true);
    }

    template <typename ...Args>
    void each(std::function<void(Args&...)> f)
    {
        // HowTo? // auto oneContainer = static_cast<Container<T>*>(this->containers[typeid(DataOne)].get());
        // HowTo? // auto twoContainer = static_cast<Container<T>*>(this->containers[typeid(DataTwo)].get());

        for(auto i = 0; i < Count; i++)
        {
            // HowTo? // if(oneContainer->valid[i] == true && twoContainer->valid[i] == true)
            // HowTo? // f(oneContainer->data[i], twoContainer->data[i]);
        }
    }

    std::map<std::type_index, std::unique_ptr<Base>> containers;
};

struct DataOne
{
    int value{};
};

struct DataTwo
{
    double value{};
};

int main()
{
    Manager manager;

    for(auto i = 0; i < Count; i++)
    {
        manager.add<DataOne>();
        manager.add<DataTwo>();
    }

    manager.each<DataOne>([](DataOne& a) {
        a.value = 0;
    });

    manager.each<DataTwo>([](DataTwo& a) {
        a.value = 0.0;
    });

    manager.each<DataOne, DataTwo>([](DataOne& a, DataTwo& b) {
        a.value = 0;
        b.value = 0.0;
    });
}
ASxa86
  • 123
  • 10

1 Answers1

2
auto containers = std::make_tuple<Container<Args>*...>(
  static_cast<Container<Args>*>(this->containers[typeid(Args)].get())...
);

...

    for(auto i = 0; i < Count; i++)
    {
      std::apply([&f](auto*... containers){
        bool valid = (containers&&...) && (containers->valid[i]&&...);
        if (valid)
          f( containers->data[i]... );
      }, containers);
    }

Please excuse any typos.

Yakk - Adam Nevraumont
  • 262,606
  • 27
  • 330
  • 524
  • Thank you! This is exactly what I was wanting. I didn't know std::apply() existed. I need to read up more on c++17 features. My only problem is my compiler is MSVC 15.4.3 and doesn't appear to support fold expressions yet. Though, it does look like it will be supported in MSVC 15.5! – ASxa86 Nov 12 '17 at 03:57
  • @asxa86 Try `auto tests = { true, (containers && containers->value[i])... }; bool valid = all_of( tests.begin(), tests.end() );` – Yakk - Adam Nevraumont Nov 12 '17 at 13:40