Comments (2)
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):
https://godbolt.org/z/q6f5f5
#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';
}
}
from refl-cpp.
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!
from refl-cpp.
Related Issues (20)
- GetDateFormat getter vs. the Win32 API HOT 2
- Use of `get_reader` and `has_writer` produces absurdly long compile times HOT 18
- Q: make REFL_AUTO, REFL_TYPE etc. callable from nested namespace HOT 7
- Port to vcpkg HOT 3
- Fix warnings for strict variadic macro checks (stronger C++20 enforcement)
- refl::util::detail::filter(..) inverts order of member list HOT 2
- Is there a way to reflect private fields? HOT 5
- Q: how to reflect on member functions' parameters and return types? HOT 5
- Is there a possibility of adding features like Boost.PFR? HOT 3
- Ability to break class declarations up HOT 9
- How to implement my case with this library? HOT 1
- Question. a generic getter function for reflected type HOT 1
- Enum reflection HOT 2
- Access member descriptor from pointer-to-member HOT 4
- Field offsetof HOT 4
- Member list empty when using reflect<T>(), but works fine with reflect(const T&) HOT 1
- How to resolve the non-const signature of a class method. HOT 2
- Q: Implementing a member attribute for "Default value" to use in ctors
- (unknown)**ย that is all i get for typename HOT 1
- Deleted HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. ๐๐๐
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google โค๏ธ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from refl-cpp.