How to get all the public methods of a c++ subclass and put them in a vector?

I'm building a c++ aplication where the user create a class like that:

class MyClass : public FrameworkClass {
/** Some user attributes and methods
 * 
 */

class ROUTINE {
   private:
    void privateMethod();

   public:
    void method1();
    void foo();
    void AnyName();
}};

The idea is that all public methods of the ROUTINE subclass are executed on separate threads in a loop. Currently, the user himself has to register all the methods pushing them in a vector that the framework will iterate starting the threads, but I want to know if there is any way to make this process automatic, that is, create the array automatically since all the methods inside the ROUTINE subclass should always run this way, preferably in a non-intrusive way like a macro.

OBS:

the loop is something simple like:

void routineLoop() {
    while(Framework::IsRunning) {
        //call the method
    }
}

and it's alreandy working, the only problem it's the usability.

Upvotes: 0

Views: 179

Answers (1)

user17732522
user17732522

Reputation: 76658

It is currently not possible to retrieve a list of the member functions of a class in C++ in any form. Reflection capabilities in current C++ are very limited.

You will always need to have the user provide you with at least the member function names if not member function pointers as you seem to be doing now.

To make it easier for the user, e.g. a macro could be provided which creates an array of member function pointers with a fixed name from a list of member function names in the macro arguments. Then the user only needs to place the macro inside the class definition and list the member function names and you can retrieve it as a member with the known name.

Upvotes: 2

Related Questions