I am doing a subclassing system which may be defined in runtime. I have a subclass that forwards the method for a table (std::map
), if a method is not available in the table, the super class method is used.
Example (function parameters and return type are not the problem, I just simplified it):
class Superclass {
public:
virtual void doSomething();
};
class Superclass_subclass : public Superclass {
public:
std::map< std::string,std::function<void (Superclass_subclass*)> > table;
int doSomething(int a, int b) {
if( table.count("doSomething") == 0 ) return Superclass::doSomething(a,b);
return table.at("doSomething")(this,a,b);
}
};
Now I am working on macros to ease the process of creating the _subclass
classes. Right now I got the following macros:
#define runtime_subclass_begin(Superclass) //...
#define runtime_subclass_method(Superclass,rtype,method,args_def,args_call) //...
#define runtime_subclass_end
#define GROUP(...) __VA_ARGS__
runtime_subclass_begin(Superclass)
runtime_subclass_method(Superclass,int,doSomething,GROUP(int a,int b),GROUP(a,b))
runtime_subclass_end
It is working well for me, except that I have to repeat the arguments, once with the types(int a, int b
), and once for calling the underlying function(a,b
). I was wondering if there is a better way to do that.