Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions include/rfl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
9 changes: 7 additions & 2 deletions include/rfl/internal/get_type_name.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#ifndef RFL_INTERNAL_GETTYPENAME_HPP_
#define RFL_INTERNAL_GETTYPENAME_HPP_

#include <string_view>

#ifndef REFLECTCPP_USE_CPP26_REFLECTION
#include "cpp20/get_type_name.hpp"
#else
Expand All @@ -19,8 +21,11 @@ consteval auto get_type_name() {
}

template <class T>
consteval auto get_type_name_str_view() {
return get_type_name<T>().string_view();
inline constexpr auto type_name_literal_v = get_type_name<T>();

template <class T>
consteval std::string_view get_type_name_str_view() noexcept {
return type_name_literal_v<T>.string_view();
}

} // namespace rfl::internal
Expand Down
5 changes: 5 additions & 0 deletions include/rfl/num_fields.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ template <class T>
constexpr std::size_t num_fields = internal::cpp26::num_fields<T>;
#endif

/// Variable-template alias for rfl::num_fields<T> following the standard _v
/// naming convention.
template <class T>
inline constexpr std::size_t num_fields_v = num_fields<T>;

} // namespace rfl

#endif
27 changes: 27 additions & 0 deletions include/rfl/type_name.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#ifndef RFL_TYPE_NAME_HPP_
#define RFL_TYPE_NAME_HPP_

#include <string_view>

#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 <class T>
inline constexpr std::string_view type_name_v =
internal::type_name_literal_v<T>.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 <class T>
constexpr std::string_view type_name() noexcept {
return type_name_v<T>;
}

} // namespace rfl

#endif
55 changes: 55 additions & 0 deletions tests/json/test_type_name_and_num_fields.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#include <gtest/gtest.h>

#include <rfl.hpp>
#include <rfl/num_fields.hpp>
#include <rfl/type_name.hpp>
#include <string>
#include <string_view>

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<T>() and rfl::type_name_v<T>
static_assert(rfl::type_name<Point>() == rfl::type_name_v<Point>);
static_assert(rfl::type_name<Point>() ==
rfl::internal::get_type_name_str_view<Point>());
static_assert(rfl::type_name<Point>().find("Point") != std::string_view::npos);
static_assert(rfl::type_name_v<nested::Config>.find("nested::Config") !=
std::string_view::npos);
static_assert(rfl::type_name_v<Point>.data()[rfl::type_name_v<Point>.size()] ==
'\0');

// Compile-time checks: rfl::num_fields<T> and rfl::num_fields_v<T>
static_assert(rfl::num_fields<Empty> == 0);
static_assert(rfl::num_fields_v<Empty> == 0);
static_assert(rfl::num_fields<Point> == 2);
static_assert(rfl::num_fields_v<Point> == 2);
static_assert(rfl::num_fields<nested::Config> == 3);
static_assert(rfl::num_fields_v<nested::Config> == 3);

TEST(json, test_type_name_and_num_fields) {
constexpr std::string_view sv = rfl::type_name<nested::Config>();
EXPECT_EQ(sv, rfl::type_name_v<nested::Config>);
EXPECT_EQ(sv, rfl::type_name_t<nested::Config>().str());
EXPECT_NE(sv.find("nested::Config"), std::string_view::npos);

EXPECT_EQ(rfl::num_fields_v<Empty>, 0u);
EXPECT_EQ(rfl::num_fields_v<Point>, 2u);
EXPECT_EQ(rfl::num_fields_v<nested::Config>, 3u);
}

} // namespace test_type_name_and_num_fields
Loading