diff --git a/lib/include/lib/system/reflection.hpp b/lib/include/lib/system/reflection.hpp index 664732f61..baf65ff96 100644 --- a/lib/include/lib/system/reflection.hpp +++ b/lib/include/lib/system/reflection.hpp @@ -5,6 +5,7 @@ #include #include +#include #include #include #include @@ -14,7 +15,7 @@ // compile time and rtti reflection in action, works only in methods. // if class/struct is not polimorphic - compile-time reflection, otherwise - run-time. -#define className() NAMEOF_TYPE_EXPR(*this) +#define className() ::cs::reflection::typeName>>() #define classNameString() std::string(className()) + std::literals::string_literals::operator""s(" ") #define funcName() __func__ @@ -26,10 +27,33 @@ #define csreflection() className() << ", method " << csfunc() #define csmeta(...) __VA_ARGS__() << csreflection() -#define csexpression(...) NAMEOF_TYPE_EXPR(__VA_ARGS__) -#define cstype(...) NAMEOF_FULL_TYPE(__VA_ARGS__) +#define csexpression(...) ::cs::reflection::typeName>>() +#define cstype(...) ::cs::reflection::typeName<__VA_ARGS__>() namespace cs { +namespace reflection { +// Type name read from the compiler's signature of this function. nameof cuts it out of +// __PRETTY_FUNCTION__ at fixed offsets that match older GCC versions only: with GCC 13 the length +// comes out negative, that is huge, and every log line printing a class or type name read far +// past the string and crashed the node. +template +std::string_view typeName() noexcept { +#if defined(__clang__) || defined(__GNUC__) + // GCC: "... typeName() [with T = cs::Conveyer; std::string_view = ...]", clang: "... [T = cs::Conveyer]" + const std::string_view signature = __PRETTY_FUNCTION__; + const std::string_view marker = "T = "; + const auto begin = signature.find(marker); + if (begin == std::string_view::npos) { + return {}; + } + const auto first = begin + marker.size(); + const auto end = signature.find_first_of(";]", first); + return signature.substr(first, end == std::string_view::npos ? std::string_view::npos : end - first); +#else + return ::nameof::nameof_type(); +#endif +} +} // namespace reflection // vector template diff --git a/tests/lib_system/lib_system_tests.cpp b/tests/lib_system/lib_system_tests.cpp index 43289438d..719cb1b11 100644 --- a/tests/lib_system/lib_system_tests.cpp +++ b/tests/lib_system/lib_system_tests.cpp @@ -16,6 +16,7 @@ #include #include #include +#include #ifdef _MSC_VER #pragma warning(push, 0) @@ -752,3 +753,24 @@ TEST(PmrBytes, BaseUsage) { cs::Bytes expectedBytes = { 0xFF, 0xAA, 0xBB, 0xEE }; ASSERT_TRUE(std::equal(std::begin(bytes), std::end(bytes), std::begin(expectedBytes))); } + +namespace reflection_test { +class Polymorphic { +public: + virtual ~Polymorphic() = default; + std::string_view name() const { return className(); } +}; +struct Plain { + std::string_view name() const { return className(); } +}; +} // namespace reflection_test + +// the class and type names in log lines (csmeta, csname, cstype) must be the names themselves: +// nameof's fixed offsets gave GCC 13 builds a huge length, and the node crashed on such lines +TEST(Reflection, TypeNamesAreExact) { + ASSERT_EQ(reflection_test::Polymorphic().name(), "reflection_test::Polymorphic"); + ASSERT_EQ(reflection_test::Plain().name(), "reflection_test::Plain"); + ASSERT_EQ(cstype(int), "int"); + const reflection_test::Plain value{}; + ASSERT_EQ(csexpression(value), "reflection_test::Plain"); +}