Skip to content

Instantly share code, notes, and snippets.

@Cylix
Cylix / macros_ex_5.cpp
Created October 19, 2020 06:05
Reflection in C++14 - Macros #5
REGISTER_CLASS(SomeClass, (fct_1)(fct_2)(fct_3))
@Cylix
Cylix / macros_ex_4.cpp
Created October 19, 2020 06:03
Reflection in C++14 - Macros #4
//! macro to convert any kind of value to string
#define __REFLEX_TO_STRING(val) #val
//! macro called for each member function to build a pair of <string, member_function_pointer>
#define __REFLEX_MAKE_REGISTERABLE_FUNCTION(r, type, i, function) \
BOOST_PP_COMMA_IF(i) std::make_pair(std::string(__REFLEX_TO_STRING(function)), &type::function)
@Cylix
Cylix / macros_ex_3.cpp
Created October 19, 2020 06:01
Reflection in C++14 - Macros #3
//! main macro who build the static variable
#define REGISTER_CLASS(type, functions) \
static reflectable<type> \
reflectable_##type(#type, BOOST_PP_SEQ_FOR_EACH_I( __REFLEX_MAKE_REGISTERABLE_FUNCTION, \
type, \
functions ));
@Cylix
Cylix / macros_ex_2.cpp
Created October 19, 2020 06:00
Reflection in C++14 - Macros #2
//! macro to convert any kind of value to string
#define __REFLEX_TO_STRING(val) #val
//! macro called for each member function to build a pair of <string, member_function_pointer>
#define __REFLEX_MAKE_REGISTERABLE_FUNCTION(r, type, i, function) \
BOOST_PP_COMMA_IF(i) std::make_pair(std::string(__REFLEX_TO_STRING(function)), &type::function)
//! main macro who build the static variable
#define REGISTER_CLASS(type, functions) \
static reflectable<type> \
@Cylix
Cylix / macros_ex_1.cpp
Created October 19, 2020 05:59
Reflection in C++14 - Macros #1
static reflectable<SomeClass> register_some_class("SomeClass",
std::make_pair(std::string("some_fct_1"), &SomeClass::some_fct_1),
std::make_pair(std::string("some_fct_2"), &SomeClass::some_fct_2));
@Cylix
Cylix / reflection_ex_2.cpp
Created October 19, 2020 05:58
Reflection in C++14 - Reflection #2
template <typename ReturnType, typename... Params>
struct reflection_maker;
template <typename ReturnType, typename... Params>
struct reflection_maker<ReturnType(Params...)> {
static ReturnType invoke(const std::string& class_name, const std::string& function_name, Params... params) {
return reflection_manager::get_instance().reflect<ReturnType, Params...>(class_name, function_name, params...);
}
};
@Cylix
Cylix / reflection_ex_1.cpp
Created October 19, 2020 05:54
Reflection in C++14 - Reflection #1
class reflection_manager {
public:
~reflection_manager(void) = default;
reflection_manager(const reflection_manager&) = delete;
reflection_manager& operator=(const reflection_manager&) = delete;
static reflection_manager& get_instance(void) {
static reflection_manager instance;
return instance;
}
@Cylix
Cylix / member_function_ex_5.cpp
Created October 19, 2020 05:52
Reflection in C++14 - Member Function #5
//! register specific member function
template <typename ReturnType, typename... Params>
void register_function(const std::pair<std::string, ReturnType (Type::*)(Params...)>& f) {
//! wrap the instanciation of a new object of type Type and the call to the function f in a C++11 lambda
auto f_wrapper = [f](Params... params) -> ReturnType {
return (Type().*f.second)(params...);
};
//! store function information
functions.push_back({
@Cylix
Cylix / member_function_ex_4.cpp
Created October 19, 2020 05:50
Reflection in C++14 - Member Function #4
//! iterate over the parameters pack
template <typename Head, typename... Tail>
void register_function(const Head& head, Tail... tail) {
register_function(head);
register_function(tail...);
}
@Cylix
Cylix / member_function_ex_3.cpp
Created October 19, 2020 05:48
Reflection in C++14 - Member Function #3
template <typename Type>
class reflectable : reflectable_base {
public:
//! constructor of a reflectable class where we can process the registration
//! note the presence of variadic template to accept any number of member functions
template <typename... Fcts>
reflectable(const std::string& class_name, Fcts... fcts) : name(class_name) {
reflection_manager::get_instance().register_reflectable(*this);
register_function(fcts...);
}