From 676aa359e78c7953b32c9a39d7ca29f8ec8e7506 Mon Sep 17 00:00:00 2001 From: DH Date: Tue, 12 Nov 2024 13:01:15 +0300 Subject: [PATCH] utilities: TypeId: store name of type --- rx/include/rx/TypeId.hpp | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/rx/include/rx/TypeId.hpp b/rx/include/rx/TypeId.hpp index 1943493e2..84f02351c 100644 --- a/rx/include/rx/TypeId.hpp +++ b/rx/include/rx/TypeId.hpp @@ -1,35 +1,47 @@ #pragma once +#include "refl.hpp" #include #include #include namespace rx { namespace detail { -template char mRawTypeId = 0; -template constexpr const void *getTypeIdImpl() { +template constexpr std::string_view mRawTypeId = getNameOf(); + +template +[[nodiscard]] constexpr const std::string_view *getTypeIdImpl() { return &mRawTypeId; } } // namespace detail class TypeId { - const void *mId = detail::getTypeIdImpl(); + const std::string_view *mId = detail::getTypeIdImpl(); public: - constexpr const void *getOpaque() const { return mId; } + [[nodiscard]] constexpr const void *getOpaque() const { return mId; } - constexpr static TypeId createFromOpaque(const void *id) { + [[nodiscard]] constexpr static TypeId createFromOpaque(const void *id) { TypeId result; - result.mId = id; + result.mId = static_cast(id); return result; } - template constexpr static TypeId get() { - return createFromOpaque(detail::getTypeIdImpl()); + template [[nodiscard]] constexpr static TypeId get() { + TypeId result; + result.mId = detail::getTypeIdImpl(); + return result; } + [[nodiscard]] constexpr std::string_view getName() const { return *mId; } + constexpr auto operator<=>(const TypeId &other) const = default; }; + +template +constexpr std::array getTypeIds() { + return std::array{TypeId::get()...}; +} } // namespace rx namespace std {