7#include "fixed_string.hpp"
9#include "../macros.hpp"
12#include "exception.hpp"
18hi_export_module(hikogui.utility.reflection);
20hi_export
namespace hi {
inline namespace v1 {
26 [[nodiscard]]
constexpr operator T()
const noexcept
34template<
typename T,
typename... C>
35[[nodiscard]]
constexpr size_t count_data_members() noexcept
37 static_assert(std::is_trivially_constructible_v<T>);
40 if constexpr (
sizeof...(C) <
sizeof(T)) {
41 if constexpr (
constexpr auto next = count_data_members<T, C...,
convertible_to_any>()) {
48 if constexpr (
requires { T{C{}...}; }) {
57[[nodiscard]]
constexpr std::string_view type_name_token(std::string_view str)
noexcept
59 for (
auto i = 0_uz; i != str.size(); ++i) {
60 auto const c = str[i];
62 if (not((c >=
'a' and c <=
'z') or (c >=
'A' and c <=
'Z') or (c >=
'0' and c <=
'9') or c ==
'_')) {
64 return str.substr(0, i + 1);
71[[nodiscard]]
constexpr std::string type_name_short_hand(std::string_view type_name)
noexcept
73 if (type_name ==
"std::basic_string<char,std::char_traits<char>,std::allocator<char>>") {
75 }
else if (type_name ==
"std::basic_string<char,std::char_traits<char>,std::allocator<char>>&") {
76 return "std::string&";
77 }
else if (type_name ==
"const std::basic_string<char,std::char_traits<char>,std::allocator<char>>&") {
78 return "const std::string&";
84#if HI_COMPILER == HI_CC_MSVC
86[[nodiscard]]
constexpr std::string type_name() noexcept
90 auto signature = std::string_view{__FUNCSIG__};
93 auto first = signature.
find(
"type_name<");
94 hi_assert(first != std::string_view::npos);
97 auto last = signature.rfind(
'>');
98 hi_assert(last != std::string_view::npos);
100 signature = signature.substr(first, last - first);
103 while (not signature.empty()) {
104 if (signature.starts_with(
"class ")) {
105 signature = signature.
substr(6);
106 }
else if (signature.starts_with(
"struct ")) {
107 signature = signature.substr(7);
108 }
else if (signature.starts_with(
" >")) {
110 signature = signature.substr(2);
111 }
else if (signature.starts_with(
" *")) {
113 signature = signature.substr(2);
115 auto token = type_name_token(signature);
117 signature = signature.substr(token.size());
121 return type_name_short_hand(r);
123#elif HI_COMPILER == HI_CC_CLANG
125[[nodiscard]]
constexpr std::string type_name() noexcept
129 auto signature = std::string_view{__PRETTY_FUNCTION__ };
132 auto first = signature.
find(
"type_name<");
133 hi_assert(first != std::string_view::npos);
136 auto last = signature.rfind(
'>');
137 hi_assert(last != std::string_view::npos);
139 signature = signature.substr(first, last - first);
142 while (not signature.empty()) {
143 if (signature.starts_with(
"class ")) {
144 signature = signature.
substr(6);
145 }
else if (signature.starts_with(
"struct ")) {
146 signature = signature.substr(7);
147 }
else if (signature.starts_with(
" >")) {
149 signature = signature.substr(2);
150 }
else if (signature.starts_with(
" *")) {
152 signature = signature.substr(2);
154 auto token = type_name_token(signature);
156 signature = signature.substr(token.size());
160 return type_name_short_hand(r);
164[[nodiscard]]
constexpr std::string type_name() noexcept
166 return typeid(T).name();
178template<
size_t Index,
typename Type>
179constexpr decltype(
auto) get_data_member(Type&& rhs)
noexcept
181#define HI_X_FORWARD(x) forward_like<Type>(x),
185 if constexpr (number_of_members == 0) {
186 hi_static_no_default();
189#define HI_X(count, ...) \
190 } else if constexpr (number_of_members == count) { \
191 auto&& [__VA_ARGS__] = rhs; \
192 return std::get<Index>(std::forward_as_tuple(hi_for_each(HI_X_FORWARD, __VA_ARGS__) 0));
199 HI_X( 6, a,b,c,d,e,f)
200 HI_X( 7, a,b,c,d,e,f,g)
201 HI_X( 8, a,b,c,d,e,f,g,h)
202 HI_X( 9, a,b,c,d,e,f,g,h,i)
203 HI_X(10, a,b,c,d,e,f,g,h,i,j)
204 HI_X(11, a,b,c,d,e,f,g,h,i,j,k)
205 HI_X(12, a,b,c,d,e,f,g,h,i,j,k,l)
206 HI_X(13, a,b,c,d,e,f,g,h,i,j,k,l,m)
207 HI_X(14, a,b,c,d,e,f,g,h,i,j,k,l,m,n)
208 HI_X(15, a,b,c,d,e,f,g,h,i,j,k,l,m,n,o)
209 HI_X(16, a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p)
210 HI_X(17, a,b,c,d,e,f,g,h,i,j,k,l,m,n,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,R,S)
239 HI_X(46, a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T)
240 HI_X(47, a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U)
241 HI_X(48, a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V)
242 HI_X(49, a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W)
243 HI_X(50, a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X)
244 HI_X(51, a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y)
245 HI_X(52, a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z)
249 hi_static_not_implemented();
256#if HI_COMPILER == HI_CC_MSVC
258[[nodiscard]]
constexpr auto type_name() noexcept
260 return hi_to_fixed_string(detail::type_name<T>());
Utilities to assert and bound check.
Utilities for throwing exceptions and terminating the application.
The HikoGUI namespace.
Definition array_generic.hpp:20
DOXYGEN BUG.
Definition algorithm_misc.hpp:20
A type that can be implicitly converted to any type.
Definition reflection.hpp:24
Definition reflection.hpp:173