meta: return a string_view from name()

This commit is contained in:
skypjack
2026-01-05 12:15:34 +01:00
parent d26a9baa1c
commit deed237bbe
4 changed files with 23 additions and 19 deletions

View File

@@ -5,6 +5,7 @@
#include <cstddef>
#include <iterator>
#include <memory>
#include <string_view>
#include <type_traits>
#include <utility>
#include "../config/config.h"
@@ -803,8 +804,8 @@ public:
* @brief Returns the name assigned to a data member, if any.
* @return The name assigned to the data member, if any.
*/
[[nodiscard]] const char *name() const noexcept {
return node_or_assert().name;
[[nodiscard]] std::string_view name() const noexcept {
return (node_or_assert().name == nullptr) ? std::string_view{} : std::string_view{node_or_assert().name};
}
/**
@@ -943,8 +944,8 @@ public:
* @brief Returns the name assigned to a member function, if any.
* @return The name assigned to the member function, if any.
*/
[[nodiscard]] const char *name() const noexcept {
return node_or_assert().name;
[[nodiscard]] std::string_view name() const noexcept {
return (node_or_assert().name == nullptr) ? std::string_view{} : std::string_view{node_or_assert().name};
}
/**
@@ -1153,8 +1154,8 @@ public:
* @brief Returns the name assigned to a type, if any.
* @return The name assigned to the type, if any.
*/
[[nodiscard]] const char *name() const noexcept {
return fetch_node().name;
[[nodiscard]] std::string_view name() const noexcept {
return (fetch_node().name == nullptr) ? std::string_view{} : std::string_view{fetch_node().name};
}
/**

View File

@@ -1,5 +1,6 @@
#include <cstdlib>
#include <string>
#include <string_view>
#include <utility>
#include <gtest/gtest.h>
#include <entt/core/hashed_string.hpp>
@@ -169,13 +170,13 @@ TEST_F(MetaData, Name) {
const entt::meta_type type = entt::resolve<clazz>();
const entt::meta_type other = entt::resolve<setter_getter>();
ASSERT_EQ(type.data("i"_hs).name(), nullptr);
ASSERT_STREQ(type.data("j"_hs).name(), "j");
ASSERT_STREQ(type.data("h"_hs).name(), "hhh");
ASSERT_EQ(type.data("i"_hs).name(), std::string_view{});
ASSERT_EQ(type.data("j"_hs).name(), std::string_view{"j"});
ASSERT_EQ(type.data("h"_hs).name(), std::string_view{"hhh"});
ASSERT_EQ(other.data("z"_hs).name(), nullptr);
ASSERT_STREQ(other.data("w"_hs).name(), "w");
ASSERT_STREQ(other.data("z_ro"_hs).name(), "readonly");
ASSERT_EQ(other.data("z"_hs).name(), std::string_view{});
ASSERT_EQ(other.data("w"_hs).name(), std::string_view{"w"});
ASSERT_EQ(other.data("z_ro"_hs).name(), std::string_view{"readonly"});
}
TEST_F(MetaData, Comparison) {

View File

@@ -1,4 +1,5 @@
#include <cstddef>
#include <string_view>
#include <type_traits>
#include <utility>
#include <gtest/gtest.h>
@@ -214,9 +215,9 @@ TEST_F(MetaFunc, Name) {
const entt::meta_type type = entt::resolve<derived>();
ASSERT_EQ(type.func("setter_from_base"_hs).name(), nullptr);
ASSERT_STREQ(type.func("getter_from_base"_hs).name(), "getter_from_base");
ASSERT_STREQ(type.func("static_setter_from_base"_hs).name(), "static setter");
ASSERT_EQ(type.func("setter_from_base"_hs).name(), std::string_view{});
ASSERT_EQ(type.func("getter_from_base"_hs).name(), std::string_view{"getter_from_base"});
ASSERT_EQ(type.func("static_setter_from_base"_hs).name(), std::string_view{"static setter"});
}
TEST_F(MetaFunc, Comparison) {

View File

@@ -2,6 +2,7 @@
#include <cstdint>
#include <map>
#include <memory>
#include <string_view>
#include <utility>
#include <vector>
#include <gtest/gtest.h>
@@ -313,10 +314,10 @@ TEST_F(MetaType, IdAndInfo) {
TEST_F(MetaType, Name) {
using namespace entt::literals;
ASSERT_EQ(entt::resolve<base>().name(), nullptr);
ASSERT_STREQ(entt::resolve<derived>().name(), "derived");
ASSERT_STREQ(entt::resolve<unsigned int>().name(), "uint");
ASSERT_EQ(entt::resolve<void>().name(), nullptr);
ASSERT_EQ(entt::resolve<base>().name(), std::string_view{});
ASSERT_EQ(entt::resolve<derived>().name(), std::string_view{"derived"});
ASSERT_EQ(entt::resolve<unsigned int>().name(), std::string_view{"uint"});
ASSERT_EQ(entt::resolve<void>().name(), std::string_view{});
}
TEST_F(MetaType, SizeOf) {