Put different types of C++ functions into one package

76 Views Asked by At

I have a simple idea: to package functions into a resource and retrieve them directly from this resource when needed. As a result, I have the following class (extremely simplified for illustration purposes).

class FunctionPackage {
public:
    void register_id(std::string name, std::function<void()> function) {
        functions[name] = function;
    }
public:
    std::unordered_map<std::string, std::function<void()>> functions;
};

But this class can only store functions with fixed input and output types, such as void(). I want to implement the functionality of storing various types of functions, which may require some modifications to the class above.

class FunctionPackage {
public:
    template<typename T>
    void register_id(std::string name, T function) {
        functions[name] = function;
    }
public:
    std::unordered_map<std::string, ...> functions;
};

The definition of functions here may need to be diverse, or perhaps we need to define a macro to implement runtime registration. At this point, I am completely confused and cannot grasp any useful clues. I hope everyone can help me with my problem. Thank you.

1

There are 1 best solutions below

1
degawong On

After going through the source code of rttr, I've gained a lot of inspiration. Now, tackling this issue is a breeze. I recommend all beginners to read rttr as well.