Skip to content

Instantly share code, notes, and snippets.

@Cylix
Cylix / member_function_ex_2.cpp
Created October 19, 2020 05:46
Reflection in C++14 - Member Function #2
//! our function base class, non-templated.
//! it does really nothing, except helping us to store functions in the same container
class function_base {
public:
virtual ~function_base(void) = default;
};
//! our function class, templated on return and parameters types
template <typename ReturnType, typename... Params>
class function;
@Cylix
Cylix / member_function_ex_1.cpp
Created October 19, 2020 05:39
Reflection in C++14 - Member Function #1
template <typename T>
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);
}
@Cylix
Cylix / registration_ex_3.cpp
Created October 19, 2020 05:36
Reflection in C++14 - Registration #3
class reflection_manager {
public:
~reflection_manager(void) = default;
reflection_manager(const reflection_manager&) = delete;
refleection_manager& operator=(const reflection_manager&) = delete;
static reflection_manager& get_instance(void) {
static reflection_manager instance;
return instance;
}
@Cylix
Cylix / registration_ex_2.cpp
Created October 19, 2020 05:34
Reflection in C++14 - Registration #2
class reflectable_base {
public:
virtual ~reflectable_base(void) = default;
virtual const std::string& get_name(void) const = 0;
};
template <typename T>
class reflectable : reflectable_base {
public:
@Cylix
Cylix / registration_ex_1.cpp
Last active October 19, 2020 05:34
Reflection in C++14 - Registration #1
template <typename T>
class reflectable {
public:
//! constructor of a reflectable class where we can process the registration
reflectable(const std::string& class_name) : name(class_name) {
reflection_manager::get_instance().register_reflectable(*this);
}
//! function to get reflectable name
const std::string& get_name(void) const {
@Cylix
Cylix / manager.cpp
Created October 19, 2020 05:30
Reflection in C++14 - Manager
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 / static_var_ex_3.cpp
Created October 19, 2020 05:27
Reflection in C++14 - Static Variables Exemple #3
class reflectable {
public:
//! constructor of a reflectable class where we can process the registration
reflectable(/* ... */) {
//! registration of a class or member function here
}
};
static reflectable register_some_class;
@Cylix
Cylix / static_var_ex_2.cpp
Created October 19, 2020 05:25
Reflection in C++14 - Static Variables Exemple #2
#include <iostream>
class A {
public:
A(void) { std::cout << "A::A()" << std::endl; }
};
static A a;
int main(void) {
@Cylix
Cylix / static_var_ex_1.cpp
Created October 19, 2020 05:21
Reflection in C++14 - Static Variables Exemple #1
int main(void) {
REGISTER_CLASS(SomeClass)
REGISTER_MEMBER_FUNCTION(SomeClass::some_function)
// ...
}
@Cylix
Cylix / reflection_dlopen_dlsym.cpp
Created October 19, 2020 05:06
Reflection in C++14 - dlopen & dlsym
#include <dlfcn.h>
#include <iostream>
extern "C" {
void display_nb(int nb) {
std::cout << nb << std::endl;
}
}