rtti.hpp
Go to the documentation of this file.
1
2#pragma once
3
4#include <iostream>
5
6#include "meta/common.hpp"
7#include "meta/type/adam.hpp"
8
9namespace by {
10
15 struct _nout metaIf {
16 typedef short yes;
17 typedef char no;
18 };
19
24 template <typename T> struct tifHasDefaultCtor: public metaIf {
25 template <typename X, typename = decltype(X())> static yes test(X*);
26
27 template <typename X> static no test(...);
28
29 static constexpr nbool is = sizeof(decltype(test<T>(nullptr))) == sizeof(yes);
30 };
31
36 template <typename T> struct tifTemplate: public metaIf {
37 template <template <typename> class Template, typename X> static yes _foo(Template<X>*);
38 static no _foo(...);
39
40 static inline constexpr nbool is = sizeof(_foo((T*) 0)) == sizeof(yes);
41 };
42
47 template <typename T, typename super> // is T is sub of super
48 struct tifSub {
49 static inline constexpr nbool is = std::is_base_of<super, T>::value;
50 };
51
52 template <typename T, typename super> struct tifSub<T&, super> {
53 static inline constexpr nbool is = std::is_base_of<super, T>::value;
54 };
55
56 template <typename T, typename super> struct tifSub<T, super&> {
57 static inline constexpr nbool is = std::is_base_of<super, T>::value;
58 };
59
60 template <typename T, typename super> struct tifSub<T&, super&> {
61 static inline constexpr nbool is = std::is_base_of<super, T>::value;
62 };
63
68 template <typename T> struct taEmptyCan {
69 typedef void is;
70 };
71
76 template <typename T, typename = void> struct tifHasSuperTypedef: public metaIf {
77 static inline constexpr nbool is = false;
78 };
79
80 template <typename T> struct tifHasSuperTypedef<T, typename taEmptyCan<typename T::super>::is>: public metaIf {
81 static inline constexpr nbool is = true;
82 };
83
88 template <typename T, nbool typedefsuper = tifHasSuperTypedef<T>::is> struct tadaptiveSuper: metaIf {
89 typedef adam super;
90 };
91
92 template <typename T> struct tadaptiveSuper<T, true> {
93 typedef typename T::super super;
94 };
95
100 template <typename T, nbool canMake = std::is_constructible<T>::value> struct tinstanceMaker {
101 static void* make() { return nullptr; }
102 };
103
104 template <typename T> struct tinstanceMaker<T, true> {
105 static void* make() { return (void*) new T(); }
106 };
107
122 template <typename T> struct tnameGetter {
123 static const nchar* getRawName() { return typeid(T).name(); }
124
125 static std::string getName() {
126 std::string ret = platformAPI::filterDemangle(getRawName());
127
128 return ret;
129 }
130 };
131
132 // famous void_t def:
133 // predefined at c++17 (if we use c++17, we can remove this)
134 template <typename T> using void_t = void;
135
140 template <typename T, typename = void> struct tifHasMetaTypeDef: public metaIf {
141 static inline constexpr nbool is = false;
142 };
143
144 template <typename T> struct tifHasMetaTypeDef<T, typename taEmptyCan<typename T::metaType>::is>: public metaIf {
145 static inline constexpr nbool is = true;
146 };
147
152 template <typename T, nbool hasMeta = tifHasMetaTypeDef<T>::is> struct tmetaTypeDef {
153 using is = type;
154 };
155
156 template <typename T> struct tmetaTypeDef<T, true> {
157 using is = typename T::metaType;
158 };
159
160 template <typename T, typename... Es> using areBaseOfT = std::conjunction<std::is_base_of<T, Es>...>;
161} // namespace by
Base class for all types without explicit super class.
Definition: adam.hpp:11
Base class for runtime type information in byeol language.
Definition: type.hpp:12
Base interface for SFINAE-based type checking.
Definition: rtti.hpp:15
Helper struct for SFINAE detection patterns.
Definition: rtti.hpp:68
Adaptive super type selector.
Definition: rtti.hpp:88
Type trait to check if type has default constructor.
Definition: rtti.hpp:24
Type trait to check if type has metaType definition.
Definition: rtti.hpp:140
Type trait to check if type has super typedef.
Definition: rtti.hpp:76
Type trait to check if T is subclass of super.
Definition: rtti.hpp:48
Type trait to check if type is a template specialization.
Definition: rtti.hpp:36
Instance factory for type creation.
Definition: rtti.hpp:100
Meta type definition selector.
Definition: rtti.hpp:152
Template name getter for type introspection.
Definition: rtti.hpp:122