Skip to content

Releases: Cylix/Reflex

Reflection for static member functions and C-Style functions

04 Jul 15:12
Compare
Choose a tag to compare

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;
}

Reflection with parameters

04 Jul 11:03
Compare
Choose a tag to compare

More advanced reflection system in C++.

cpp_reflection can now register any type of member functions (there is no restrictions concerning the functions return values types, the number of parameters and the types of these parameters).

#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;
    }

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

REGISTER_REFLECTABLE(SomeClass, (add)(sub)(concat))

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;

    return 0;
}

Also reduces the number of includes (client-side) to one with a single cpp_reflection/cpp_reflection.hpp header file.

First release

04 Jul 10:55
Compare
Choose a tag to compare
First release Pre-release
Pre-release

Basic reflection system in C++.
Registered functions must have the same prototype and do not support parameters.

Reflection with instance

06 Jul 21:07
Compare
Choose a tag to compare

Reflection with instance

By default, reflection on member function is done on a newly created object.
Until now, there were no alternatives.
However, it has been added the ability to operate member functions reflection on a custom object instance.

cpp_reflection::reflection_maker<void(t)>::invoke(&some_obj, "SomeClass", "other_fct"); will invoke SomeClass::other_fct on some_obj instance.

For the moment, this feature only works on raw-pointer to the given instance. Support for other types (value, ref, std::shared_ptr, std::unique_ptr) is under development.

Tests coverage

Tests coverage has been added for all working features.
This way, it is possible to keep track of working features and to avoid to break features in future releases.

For compiling tests, juste run cmake .. -DBUILD_TESTING in the build directory. This will generate the cpp_reflection_tests binary.

ToDo

Add tasks to the ToDo list in order to see what bugs have been detected, which features will be developed and what will be improved.

Release feature example

#include <iostream>

#include "cpp_reflection/cpp_reflection.hpp"

//! some class with member functions and static member function
class SomeClass {
public:
    unsigned int nb;
    SomeClass() : nb(42) {}

    void set_nb(unsigned int n) {
        std::cout << "set to " << n << std::endl;
        nb = n;
    }

    unsigned int get_nb(void) {
        return nb;
    }

    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 this class and its functions
REGISTER_CLASS_FUNCTIONS(SomeClass, (add)(sub)(concat)(get_nb)(set_nb))

//! some c-style functions
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 c-style functions
REGISTER_FUNCTIONS((basic_fct_1)(basic_fct_2))

int main(void) {
    //! reflection on member functions
    auto res1 = cpp_reflection::reflection_maker<int(int, int)>::invoke("SomeClass", "add", 30, 12);
    std::cout << res1 << std::endl;

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

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

    //! reflection on c-style functions
    auto res4 = cpp_reflection::reflection_maker<int(float, char)>::invoke("basic_fct_1", 4.2, 'z');
    std::cout << res4 << std::endl;

    cpp_reflection::reflection_maker<void()>::invoke("basic_fct_2");

    //! reflection on member functions with custom object instance
    SomeClass s;
    auto res5 = cpp_reflection::reflection_maker<unsigned int()>::invoke(&s, "SomeClass", "get_nb");
    std::cout << res5 << std::endl;

    cpp_reflection::reflection_maker<void(unsigned int)>::invoke(&s, "SomeClass", "set_nb", 1234);

    auto res6 = cpp_reflection::reflection_maker<unsigned int()>::invoke(&s, "SomeClass", "get_nb");
    std::cout << res6 << std::endl;

    return 0;
}