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;
});
}