From 4d3e0f812553e4d89f8eb75764fd347ba1492f77 Mon Sep 17 00:00:00 2001 From: clockblocker Date: Sat, 28 Nov 2026 10:47:22 +0100 Subject: [PATCH] feat(reflection): add public rfl::type_name() / type_name_v and rfl::num_fields_v --- include/rfl.hpp | 1 + include/rfl/internal/get_type_name.hpp | 9 +++- include/rfl/num_fields.hpp | 5 ++ include/rfl/type_name.hpp | 27 ++++++++++ tests/json/test_type_name_and_num_fields.cpp | 55 ++++++++++++++++++++ 5 files changed, 95 insertions(+), 2 deletions(-) create mode 100644 include/rfl/type_name.hpp create mode 100644 tests/json/test_type_name_and_num_fields.cpp diff --git a/include/rfl.hpp b/include/rfl.hpp index 432fff2f6..ab48b0509 100644 --- a/include/rfl.hpp +++ b/include/rfl.hpp @@ -92,6 +92,7 @@ #include "rfl/to_named_tuple.hpp" #include "rfl/to_view.hpp" #include "rfl/tuple_cat.hpp" +#include "rfl/type_name.hpp" #include "rfl/type_name_t.hpp" #include "rfl/view_t.hpp" #include "rfl/visit.hpp" diff --git a/include/rfl/internal/get_type_name.hpp b/include/rfl/internal/get_type_name.hpp index 9596ad231..f606a0404 100644 --- a/include/rfl/internal/get_type_name.hpp +++ b/include/rfl/internal/get_type_name.hpp @@ -1,6 +1,8 @@ #ifndef RFL_INTERNAL_GETTYPENAME_HPP_ #define RFL_INTERNAL_GETTYPENAME_HPP_ +#include + #ifndef REFLECTCPP_USE_CPP26_REFLECTION #include "cpp20/get_type_name.hpp" #else @@ -19,8 +21,11 @@ consteval auto get_type_name() { } template -consteval auto get_type_name_str_view() { - return get_type_name().string_view(); +inline constexpr auto type_name_literal_v = get_type_name(); + +template +consteval std::string_view get_type_name_str_view() noexcept { + return type_name_literal_v.string_view(); } } // namespace rfl::internal diff --git a/include/rfl/num_fields.hpp b/include/rfl/num_fields.hpp index 7e6246b5a..8cd658f0b 100644 --- a/include/rfl/num_fields.hpp +++ b/include/rfl/num_fields.hpp @@ -19,6 +19,11 @@ template constexpr std::size_t num_fields = internal::cpp26::num_fields; #endif +/// Variable-template alias for rfl::num_fields following the standard _v +/// naming convention. +template +inline constexpr std::size_t num_fields_v = num_fields; + } // namespace rfl #endif diff --git a/include/rfl/type_name.hpp b/include/rfl/type_name.hpp new file mode 100644 index 000000000..ca84883da --- /dev/null +++ b/include/rfl/type_name.hpp @@ -0,0 +1,27 @@ +#ifndef RFL_TYPE_NAME_HPP_ +#define RFL_TYPE_NAME_HPP_ + +#include + +#include "internal/get_type_name.hpp" + +namespace rfl { + +/// Compile-time null-terminated std::string_view containing the name of type T +/// (without instantiating an rfl::Literal type). +/// @tparam T The type whose name to retrieve +template +inline constexpr std::string_view type_name_v = + internal::type_name_literal_v.string_view(); + +/// Returns the compile-time name of type T as a null-terminated +/// std::string_view (usable in both constexpr and runtime contexts). +/// @tparam T The type whose name to retrieve +template +constexpr std::string_view type_name() noexcept { + return type_name_v; +} + +} // namespace rfl + +#endif diff --git a/tests/json/test_type_name_and_num_fields.cpp b/tests/json/test_type_name_and_num_fields.cpp new file mode 100644 index 000000000..b154a980d --- /dev/null +++ b/tests/json/test_type_name_and_num_fields.cpp @@ -0,0 +1,55 @@ +#include + +#include +#include +#include +#include +#include + +namespace test_type_name_and_num_fields { + +struct Empty {}; + +struct Point { + int x; + int y; +}; + +namespace nested { +struct Config { + std::string name; + int port = 8080; + bool tls = true; +}; +} // namespace nested + +// Compile-time checks: rfl::type_name() and rfl::type_name_v +static_assert(rfl::type_name() == rfl::type_name_v); +static_assert(rfl::type_name() == + rfl::internal::get_type_name_str_view()); +static_assert(rfl::type_name().find("Point") != std::string_view::npos); +static_assert(rfl::type_name_v.find("nested::Config") != + std::string_view::npos); +static_assert(rfl::type_name_v.data()[rfl::type_name_v.size()] == + '\0'); + +// Compile-time checks: rfl::num_fields and rfl::num_fields_v +static_assert(rfl::num_fields == 0); +static_assert(rfl::num_fields_v == 0); +static_assert(rfl::num_fields == 2); +static_assert(rfl::num_fields_v == 2); +static_assert(rfl::num_fields == 3); +static_assert(rfl::num_fields_v == 3); + +TEST(json, test_type_name_and_num_fields) { + constexpr std::string_view sv = rfl::type_name(); + EXPECT_EQ(sv, rfl::type_name_v); + EXPECT_EQ(sv, rfl::type_name_t().str()); + EXPECT_NE(sv.find("nested::Config"), std::string_view::npos); + + EXPECT_EQ(rfl::num_fields_v, 0u); + EXPECT_EQ(rfl::num_fields_v, 2u); + EXPECT_EQ(rfl::num_fields_v, 3u); +} + +} // namespace test_type_name_and_num_fields