Skip to content

Instantly share code, notes, and snippets.

@Cylix
Created October 19, 2020 05:30
Show Gist options
  • Save Cylix/9fe17a399d2a2ae58344b2ee0109a4ff to your computer and use it in GitHub Desktop.
Save Cylix/9fe17a399d2a2ae58344b2ee0109a4ff to your computer and use it in GitHub Desktop.
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;
}
void register_reflectable(reflectable& r) {
// store the reflectable
this->reflectables.push_back({ r });
}
void process_reflection(const std::string& class_name) {
// process reflection here
}
private:
reflection_manager(void) = default;
std::vector<std::reference_wrapper<reflectable>> reflectables;
};
class reflectable {
public:
//! constructor of a reflectable class where we can process the registration
reflectable(const std::string& class_name) {
reflection_manager::get_instance().register_reflectable(*this);
}
};
static reflectable register_some_class("SomeClass");
int main(void) {
reflection_manager::get_instance().process_reflection("SomeClass");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment