-
Notifications
You must be signed in to change notification settings - Fork 0
/
Factory.cpp
66 lines (57 loc) · 1.66 KB
/
Factory.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include "Factory.h"
#ifndef __clang__
#include <cxxabi.h>
#endif
namespace tngl {
namespace detail {
#ifndef __clang__
namespace {
bool walk_tree(const __cxxabiv1:: __si_class_type_info *si, std::type_info const& a) {
if (si->__base_type == &a) {
return true;
}
return is_type_ancestor(a, *si->__base_type);
}
bool walk_tree(__cxxabiv1::__vmi_class_type_info const* mi, std::type_info const& a) {
for (unsigned int i = 0; i < mi->__base_count; ++i) {
if (is_type_ancestor(a, *mi->__base_info[i].__base_type)) {
return true;
}
}
return false;
}
}
bool is_type_ancestor(const std::type_info& base, const std::type_info& deriv) {
if (base == deriv) {
return true;
}
const __cxxabiv1:: __si_class_type_info *si = dynamic_cast<__cxxabiv1::__si_class_type_info const*>(&deriv);
if (si) {
return walk_tree(si, base);
}
const __cxxabiv1:: __vmi_class_type_info *mi = dynamic_cast<__cxxabiv1::__vmi_class_type_info const*>(&deriv);
if (mi) {
return walk_tree(mi, base);
}
return false;
}
#else // !HACK to satisfy clang (might not produce valid code)
bool is_type_ancestor(const std::type_info& base, const std::type_info& deriv) {
if (base == deriv) {
return true;
}
return true;
}
#endif
}
Builders getBuildersForType(const std::type_info& base) {
auto const& reg = NodeBuilderRegistry::getInstance();
Builders builders;
std::for_each(begin(reg), end(reg), [&](auto const& pair) {
if (detail::is_type_ancestor(base, pair.second->getType())) {
builders.emplace(pair.first, pair.second);
}
});
return builders;
}
}