Skip to content
Merged
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
30 changes: 27 additions & 3 deletions lib/include/lib/system/reflection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include <utility>
#include <string>
#include <string_view>
#include <vector>
#include <array>
#include <type_traits>
Expand All @@ -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<std::remove_cv_t<std::remove_reference_t<decltype(*this)>>>()
#define classNameString() std::string(className()) + std::literals::string_literals::operator""s(" ")

#define funcName() __func__
Expand All @@ -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<std::remove_cv_t<std::remove_reference_t<decltype(__VA_ARGS__)>>>()
#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 <typename T>
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<T>();
#endif
}
} // namespace reflection

// vector
template <typename T>
Expand Down
22 changes: 22 additions & 0 deletions tests/lib_system/lib_system_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <lib/system/lockfreechanger.hpp>
#include <lib/system/console.hpp>
#include <lib/system/pmrfactory.hpp>
#include <lib/system/reflection.hpp>

#ifdef _MSC_VER
#pragma warning(push, 0)
Expand Down Expand Up @@ -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");
}
Loading