Skip to content

Reflection for static member functions and C-Style functions

Latest
Compare
Choose a tag to compare
@Cylix Cylix released this 04 Jul 15:12
· 25 commits to master since this release

Static member functions and C-Style functions can now be registered through REGISTER_CLASS_FUNCTIONS and REGISTER_FUNCTIONS macros.

This way, they can be invoked during reflection by calling cpp_reflection::make_reflection<>::call(...)

#include <iostream>

#include "cpp_reflection/cpp_reflection.hpp"

class SomeClass {
public:
    int add(int nb1, int nb2) {
        std::cout << "add(" << nb1 << ", " << nb2 << ")" << std::endl;
        return nb1 + nb2;
    }

    int sub(int nb1, int nb2) {
        std::cout << "sub(" << nb1 << ", " << nb2 << ")" << std::endl;
        return nb1 - nb2;
    }

    static std::string concat(const std::string& str, unsigned int nb) {
        std::cout << "concat(" << str << ", " << nb << ")" << std::endl;
        return str + std::to_string(nb);
    }
};

REGISTER_CLASS_FUNCTIONS(SomeClass, (add)(sub)(concat))

int basic_fct_1(float f, char c) {
    std::cout << "basic_fct_1(" << f << ", " << c << ")" << std::endl;
    return 42;
}

void basic_fct_2(void) {
    std::cout << "basic_fct_2()" << std::endl;
}

REGISTER_FUNCTIONS((basic_fct_1)(basic_fct_2))

int main(void) {
    auto res1 = cpp_reflection::make_reflection<int(int, int)>::call("SomeClass", "add", 30, 12);
    std::cout << res1 << std::endl;

    auto res2 = cpp_reflection::make_reflection<int(int, int)>::call("SomeClass", "sub", 44, 2);
    std::cout << res2 << std::endl;

    auto res3 = cpp_reflection::make_reflection<std::string(const std::string&, unsigned int)>::call("SomeClass", "concat", std::string("hello"), 42);
    std::cout << res3 << std::endl;

    auto res4 = cpp_reflection::make_reflection<int(float, char)>::call("basic_fct_1", 4.2, 'z');
    std::cout << res4 << std::endl;

    cpp_reflection::make_reflection<void()>::call("basic_fct_2");

    return 0;
}