Here, I have a custom collection of entities (vectors of different types: Type1
, Type2
, Type3
, Type4
, for simplicity I reduced to two types) with different sizes:
#include <stdio.h>
#include <vector>
template<typename DataType>
struct AbstractEntity
{
public:
virtual std::vector<DataType>& getParticlesCPU() = 0;
virtual void print() {};
};
// there is one of
// realization of AbstractEntity class
template<typename DataType>
class ClothEntity : public AbstractEntity<DataType>
{
public:
ClothEntity(){
particles.push_back(DataType(1));
particles.push_back(DataType(2));
}
virtual std::vector<DataType>& getParticlesCPU() override {
return particles;
}
virtual void print(){
for (auto& a : particles)
printf("%d\n", a.x);
}
private:
std::vector<DataType> particles;
};
struct Type1{
int x;
Type1(int x_){x = x_;}
};
struct Type2{
int x;
float y;
Type2(int x_){x = x_; y = 0;}
};
class EntityCollection{
using Entity1 = AbstractEntity<Type1>;
using Entity2 = AbstractEntity<Type2>;
public:
EntityCollection(){}
void push_back(Entity1* entity){
entities1.push_back(entity);
}
void push_back(Entity2* entity){
entities2.push_back(entity);
}
std::vector<Entity1*> getEntity1(){
return entities1;
}
std::vector<Entity2*> getEntity2(){
return entities2;
}
private:
template< typename... Ts >
using custom_function_t = void (*) ( Ts... );
template<typename Entity, typename... Ts >
double fun_per_entity( std::vector<Entity>& entities, custom_function_t<Ts...> f, Ts... args ) {
for(auto& entity : entities){
f(entity, args...);
}
}
public:
template<typename... Ts >
void run_function(custom_function_t<Ts...> f, Ts... args){
fun_per_entity(entities1, f, args...);
fun_per_entity(entities2, f, args...);
}
private:
std::vector<Entity1*> entities1;
std::vector<Entity2*> entities2;
};
int main()
{
EntityCollection ec;
ClothEntity<Type1> e1;
ClothEntity<Type2> e2;
ec.push_back(&e1);
ec.push_back(&e2);
// ec.run_function(print); // does not work
for (auto& ev: ec.getEntity1())
ev->print();
for (auto& ev: ec.getEntity2())
ev->print();
return 0;
}
My goal is safely iterate over all vectors (entities1, entities2, ...) to run a given external function f_external(Entity e, Args...)
:
for (auto& e : entities1)
f_external(e, args...)
for (auto& e : entities2)
f_external(e, args...)
or internal function f_internal(Args...)
:
for (auto& e : entities1)
e->f_internal(args...)
for (auto& e : entities2)
e->f_internal(args...)
Right now, I am writing a lot of code each time:
EntityCollection ec;
for (auto& e : ec.getEntity1())
e->f_internal(args...)
for (auto& e : ec.getEntity2())
e->f_internal(args...)
How to automate it in order to minimize the mistakes and changes with adding new Types? I would be grateful if you use C 17
.
CodePudding user response:
Make your run_function
member function take a functor instead of a function pointer:
template<typename Entity, typename F, typename... Ts>
static void fun_per_entity(std::vector<Entity*>& entities, F&& f, Ts&&... args) {
for (auto& entity : entities) {
f(entity, args...);
}
}
public:
template<typename F, typename... Ts>
void run_function(F&& f, Ts&&... args) {
fun_per_entity(entities1, f, args...);
fun_per_entity(entities2, f, args...);
fun_per_entity(entities3, f, args...);
// ...
}
And you can use it with a lambda:
ec.run_function([&](auto* e) { f_external(e, args...); });
ec.run_function([&](auto* e) { e->f_internal(args...); });
Also consider making template<typename DataType> class AbstractEntity
derive from some non-template base so you only need one vector