Преглед изворни кода

family: back to the old-fashioned model

Michele Caini пре 6 година
родитељ
комит
4f438e5228
3 измењених фајлова са 26 додато и 41 уклоњено
  1. 1 2
      TODO
  2. 16 27
      src/entt/core/family.hpp
  3. 9 12
      test/entt/core/family.cpp

+ 1 - 2
TODO

@@ -32,9 +32,8 @@
     * add discard pool functionality (+ test)
   - clean up target_compile_definitions test/CMakeLists.txt (_EXPORT/_IMPORT)
   - use type_id also for groups, get rid of extent and subfunctions, is it possible?
-  - reintroduce old-fashion family and add a new family-like thing with generators
   - families should be defined as out-of-class to guarantee the same identifiers for the same types
-  - update doc: family, dispatcher, emitter, registry, meta, across boundaries
+  - update doc: dispatcher, emitter, registry, meta, across boundaries
   - update/improve/review tests
   - review and suppress warnings, if any
   - remove ENTT_API from OPAQUE_TYPE

+ 16 - 27
src/entt/core/family.hpp

@@ -3,41 +3,30 @@
 
 
 #include "../config/config.h"
-#include "../core/attribute.h"
 
 
 namespace entt {
 
 
-/*! @brief Sequential number generator. */
+/**
+ * @brief Dynamic identifier generator.
+ *
+ * Utility class template that can be used to assign unique identifiers to types
+ * at runtime. Use different specializations to create separate sets of
+ * identifiers.
+ */
 template<typename...>
-struct ENTT_API generator {
-    /**
-     * @brief Returns the next available value.
-     * @return The next available value.
-     */
-    static ENTT_ID_TYPE next() ENTT_NOEXCEPT {
-        static ENTT_MAYBE_ATOMIC(ENTT_ID_TYPE) value{};
-        return value++;
-    }
-};
+class family {
+    inline static ENTT_MAYBE_ATOMIC(ENTT_ID_TYPE) identifier{};
 
+public:
+    /*! @brief Unsigned integer type. */
+    using family_type = ENTT_ID_TYPE;
 
-/**
- * @brief Runtime type unique identifier.
- * @tparam Type Type for which to generate an unique identifier.
- * @tparam Generator Tags to use to discriminate between different generators.
- */
-template<typename Type, typename... Generator>
-struct ENTT_API family {
-    /**
-     * @brief Statically generated unique identifier for a given type.
-     * @return The runtime unique identifier for the given type.
-     */
-    static ENTT_ID_TYPE type() ENTT_NOEXCEPT {
-        static const ENTT_ID_TYPE value = generator<Generator...>::next();
-        return value;
-    }
+    /*! @brief Statically generated unique identifier for the given type. */
+    template<typename... Type>
+    // at the time I'm writing, clang crashes during compilation if auto is used instead of family_type
+    inline static const family_type type = identifier++;
 };
 
 

+ 9 - 12
test/entt/core/family.cpp

@@ -1,17 +1,14 @@
 #include <gtest/gtest.h>
 #include <entt/core/family.hpp>
 
-template<typename Type>
-using a_family = entt::family<Type, struct a_family_type>;
-
-template<typename Type>
-using another_family = entt::family<Type, struct another_family_type>;
+using a_family = entt::family<struct a_family_type>;
+using another_family = entt::family<struct another_family_type>;
 
 TEST(Family, Functionalities) {
-    auto t1 = a_family<int>::type();
-    auto t2 = a_family<int>::type();
-    auto t3 = a_family<char>::type();
-    auto t4 = another_family<double>::type();
+    auto t1 = a_family::type<int>;
+    auto t2 = a_family::type<int>;
+    auto t3 = a_family::type<char>;
+    auto t4 = another_family::type<double>;
 
     ASSERT_EQ(t1, t2);
     ASSERT_NE(t1, t3);
@@ -19,7 +16,7 @@ TEST(Family, Functionalities) {
 }
 
 TEST(Family, Uniqueness) {
-    ASSERT_NE(a_family<int>::type(), a_family<int &>::type());
-    ASSERT_NE(a_family<int>::type(), a_family<int &&>::type());
-    ASSERT_NE(a_family<int>::type(), a_family<const int &>::type());
+    ASSERT_NE(a_family::type<int>, a_family::type<int &>);
+    ASSERT_NE(a_family::type<int>, a_family::type<int &&>);
+    ASSERT_NE(a_family::type<int>, a_family::type<const int &>);
 }