Skip to content

Commit ff05444

Browse files
Get type name using C++ 26
1 parent 4fdb50f commit ff05444

2 files changed

Lines changed: 44 additions & 9 deletions

File tree

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#ifndef RFL_INTERNAL_CPP26_GETTYPENAME_HPP_
2+
#define RFL_INTERNAL_CPP26_GETTYPENAME_HPP_
3+
4+
#include <meta>
5+
#include <type_traits>
6+
7+
#include "../StringLiteral.hpp"
8+
9+
namespace rfl::internal::cpp26 {
10+
11+
template <class T>
12+
consteval auto get_type_name() {
13+
using Type = std::remove_cvref_t<T>;
14+
if constexpr (std::is_pointer_v<Type>) {
15+
return get_type_name<std::remove_pointer_t<Type>>();
16+
} else {
17+
// TODO: Is there a way to get the name of the type without using
18+
// compiler-specific extensions?
19+
#if defined(__GNUC__)
20+
constexpr auto display_string = std::meta::display_string_of(^^Type);
21+
constexpr auto drop_last_char =
22+
display_string.substr(0, display_string.size() - 1);
23+
constexpr auto name =
24+
drop_last_char.substr(drop_last_char.find("{aka ") + 5);
25+
return StringLiteral<name.size() + 1>(name);
26+
#else
27+
static_assert(false,
28+
"You are using an unsupported compiler. Please use GCC or "
29+
"explicitly tag your structs using 'Tag' or 'Name'.");
30+
#endif
31+
}
32+
}
33+
} // namespace rfl::internal::cpp26
34+
35+
#endif

include/rfl/internal/get_type_name.hpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
#ifndef RFL_INTERNAL_GETTYPENAME_HPP_
22
#define RFL_INTERNAL_GETTYPENAME_HPP_
3-
// TODO
4-
// #ifndef REFLECTCPP_USE_CPP26_REFLECTION
3+
4+
#ifndef REFLECTCPP_USE_CPP26_REFLECTION
55
#include "cpp20/get_type_name.hpp"
6-
// #else
7-
// TODO
8-
// #endif
6+
#else
7+
#include "cpp26/get_type_name.hpp"
8+
#endif
99

1010
namespace rfl::internal {
1111

1212
template <class T>
1313
consteval auto get_type_name() {
14-
// #ifndef REFLECTCPP_USE_CPP26_REFLECTION
14+
#ifndef REFLECTCPP_USE_CPP26_REFLECTION
1515
return cpp20::get_type_name<T>();
16-
// #else
17-
// TODO
18-
// #endif
16+
#else
17+
return cpp26::get_type_name<T>();
18+
#endif
1919
}
2020

2121
} // namespace rfl::internal

0 commit comments

Comments
 (0)