-
-
Notifications
You must be signed in to change notification settings - Fork 75
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Automatically built list of child types #38
Comments
Hello! I have not thought about this specific use case, but what I have thought about is if there is a way to enumerate all refl-cpp types, and that doesn't seem to be the case. What you could try, though, provided that you intend to use that sort of data at runtime only, is create a lightweight structure to hold the information about the base classes, and build the reverse relationship from there. Any metadata that you would like to use would also have to be moved to runtime. Something like this (needs much improvement): #include <string>
#include <vector>
#include <map>
#include <iostream>
#include "refl.hpp"
#define CONCAT_(A, B) A##B
#define CONCAT(A, B) CONCAT_(A, B)
#define RUN_GLOBAL(...) static int CONCAT(_ignored_, __COUNTER__) = (void(__VA_ARGS__), 0)
struct TypeInfo
{
template <typename T>
static TypeInfo from(refl::type_descriptor<T> td)
{
auto bases = refl::util::map_to_array<TypeInfo>(reflect_types(get_declared_base_types(td)), [](auto base_td) {
return TypeInfo::from(base_td);
});
return TypeInfo{ td.name.str(), std::vector<TypeInfo>(bases.begin(), bases.end()) };
}
template <typename T>
static void register_type()
{
constexpr auto td = refl::reflect<T>();
s_type_info[td.name.str()] = from(td);
}
template <typename T>
static std::vector<TypeInfo> get_derived_types()
{
std::vector<TypeInfo> derived;
for (auto&& [key, value] : s_type_info) {
for (auto&& base : value.bases) {
if (base.name == refl::reflect<T>().name.str()) {
// TODO: Check if already added
derived.push_back(value);
}
}
}
return derived;
}
std::string name;
std::vector<TypeInfo> bases;
static std::map<std::string, TypeInfo> s_type_info;
};
std::map<std::string, TypeInfo> TypeInfo::s_type_info{ };
struct BaseClass { };
REFL_AUTO(type(BaseClass))
RUN_GLOBAL(TypeInfo::register_type<BaseClass>());
struct DerivedClass : BaseClass { };
REFL_AUTO(type(DerivedClass, bases<BaseClass>))
RUN_GLOBAL(TypeInfo::register_type<DerivedClass>());
int main()
{
std::cout << "Derived from BaseClass:\n";
for (auto&& derived : TypeInfo::get_derived_types<BaseClass>()) {
std::cout << '\t' << derived.name << '\n';
}
} |
Agreed, iterating through all the types would help here. Too bad it's not possible. The runtime approach is an interesting direction though. I'll what I can make of that. Thank you! |
Hi! First of all: thank you for this exquisite library! I'm currently using a simple
children
attribute on base classes to supply it with derived type list. It works but adds an extra step for type registration - I have to both specify the base in the child and the child in the base.At first I thought that due to the static nature of refl-cpp it's not possible to add new child types to the base type descriptor when the children are registered.
But maybe I'm missing something and there is a way. Did you think about this?
The text was updated successfully, but these errors were encountered: