From c6c20df1a655798cbeb0860c98b5429608d1d748 Mon Sep 17 00:00:00 2001 From: BlackMATov Date: Thu, 18 Apr 2019 00:23:05 +0700 Subject: [PATCH] add utils::type_family --- headers/enduro2d/utils/_utils.hpp | 40 +++++++++++++++++++++--- untests/sources/untests_utils/_utils.cpp | 7 +++++ 2 files changed, 43 insertions(+), 4 deletions(-) diff --git a/headers/enduro2d/utils/_utils.hpp b/headers/enduro2d/utils/_utils.hpp index 61448008..d89ce9bf 100644 --- a/headers/enduro2d/utils/_utils.hpp +++ b/headers/enduro2d/utils/_utils.hpp @@ -39,13 +39,13 @@ namespace e2d namespace e2d { - using str = basic_string; - using wstr = basic_string; + using str = basic_string; + using wstr = basic_string; using str16 = basic_string; using str32 = basic_string; - using str_view = basic_string_view; - using wstr_view = basic_string_view; + using str_view = basic_string_view; + using wstr_view = basic_string_view; using str16_view = basic_string_view; using str32_view = basic_string_view; @@ -147,4 +147,36 @@ namespace e2d { namespace utils constexpr std::underlying_type_t enum_to_underlying(E e) noexcept { return static_cast>(e); } + + // + // type_family + // + + using type_family_id = u32; + + namespace impl + { + template < typename Void = void > + class type_family_base { + static_assert( + std::is_void::value && + std::is_unsigned::value, + "unexpected internal error"); + protected: + static type_family_id last_id_; + }; + + template < typename Void > + type_family_id type_family_base::last_id_ = 0u; + } + + template < typename T > + class type_family final : public impl::type_family_base<> { + public: + static type_family_id id() noexcept { + static type_family_id self_id = ++last_id_; + assert(self_id > 0u && "type_family_id overflow"); + return self_id; + } + }; }} diff --git a/untests/sources/untests_utils/_utils.cpp b/untests/sources/untests_utils/_utils.cpp index 95023e6f..99a43b7a 100644 --- a/untests/sources/untests_utils/_utils.cpp +++ b/untests/sources/untests_utils/_utils.cpp @@ -49,4 +49,11 @@ TEST_CASE("utils") { 42u, str1, str1 + std::strlen(str1) ) == utils::sdbm_hash(42u, str2)); } + { + utils::type_family_id id1 = utils::type_family::id(); + utils::type_family_id id2 = utils::type_family::id(); + REQUIRE(id1 != id2); + REQUIRE(id1 == utils::type_family::id()); + REQUIRE(id2 == utils::type_family::id()); + } }