Forráskód Böngészése

dispatcher: (almost) transparent dll/.so support with ENTT_API

Michele Caini 6 éve
szülő
commit
231036784d

+ 1 - 0
TODO

@@ -39,6 +39,7 @@
 * observer: user defined filters (eg .replace<T, &function> or .group<T, U, &func>)
 * observer: user defined filters (eg .replace<T, &function> or .group<T, U, &func>)
 * any-of rule for views/groups (eg entity has A and any of B/C/D)
 * any-of rule for views/groups (eg entity has A and any of B/C/D)
 * sparse set: there exists an alternative to paginated sparse arrays?
 * sparse set: there exists an alternative to paginated sparse arrays?
+* see warning (vs2017 only): d:\a\entt\entt\src\entt\entity\registry.hpp(108)
 
 
 * Mission: get rid of named types
 * Mission: get rid of named types
   - make it possible to use custom generators (eg for plugins)
   - make it possible to use custom generators (eg for plugins)

+ 8 - 2
src/entt/core/family.hpp

@@ -30,8 +30,14 @@ struct ENTT_API generator {
  */
  */
 template<typename Type, typename... Generator>
 template<typename Type, typename... Generator>
 struct ENTT_API family {
 struct ENTT_API family {
-    /*! @brief Statically generated unique identifier for the given type. */
-    inline static const ENTT_ID_TYPE type = generator<Generator...>::next();
+    /**
+     * @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;
+    }
 };
 };
 
 
 
 

+ 4 - 4
src/entt/entity/registry.hpp

@@ -275,7 +275,7 @@ public:
      */
      */
     template<typename Component>
     template<typename Component>
     static component type() ENTT_NOEXCEPT {
     static component type() ENTT_NOEXCEPT {
-        return component{component_family<std::decay_t<Component>>::type};
+        return component{component_family<std::decay_t<Component>>::type()};
     }
     }
 
 
     /**
     /**
@@ -1630,7 +1630,7 @@ public:
      */
      */
     template<typename Type, typename... Args>
     template<typename Type, typename... Args>
     Type & set(Args &&... args) {
     Type & set(Args &&... args) {
-        const auto vtype = context_family<std::decay_t<Type>>::type;
+        const auto vtype = context_family<std::decay_t<Type>>::type();
 
 
         if(!(vtype < vars.size())) {
         if(!(vtype < vars.size())) {
             vars.resize(vtype+1);
             vars.resize(vtype+1);
@@ -1646,7 +1646,7 @@ public:
      */
      */
     template<typename Type>
     template<typename Type>
     void unset() {
     void unset() {
-        if(const auto vtype = context_family<std::decay_t<Type>>::type; vtype < vars.size()) {
+        if(const auto vtype = context_family<std::decay_t<Type>>::type(); vtype < vars.size()) {
             vars[vtype].reset();
             vars[vtype].reset();
         }
         }
     }
     }
@@ -1676,7 +1676,7 @@ public:
      */
      */
     template<typename Type>
     template<typename Type>
     const Type * try_ctx() const {
     const Type * try_ctx() const {
-        const auto vtype = context_family<std::decay_t<Type>>::type;
+        const auto vtype = context_family<std::decay_t<Type>>::type();
         return vtype < vars.size() && vars[vtype] ? &static_cast<variable_handler<Type> &>(*vars[vtype]).value : nullptr;
         return vtype < vars.size() && vars[vtype] ? &static_cast<variable_handler<Type> &>(*vars[vtype]).value : nullptr;
     }
     }
 
 

+ 4 - 2
src/entt/signal/dispatcher.hpp

@@ -31,8 +31,10 @@ namespace entt {
  * crashes.
  * crashes.
  */
  */
 class dispatcher {
 class dispatcher {
+    struct ENTT_API dispatcher_event_family;
+
     template<typename Type>
     template<typename Type>
-    using event_family = family<Type, struct ENTT_API internal_dispatcher_event_family>;
+    using event_family = family<Type, dispatcher_event_family>;
 
 
     struct basic_pool {
     struct basic_pool {
         virtual ~basic_pool() = default;
         virtual ~basic_pool() = default;
@@ -80,7 +82,7 @@ class dispatcher {
 
 
     template<typename Event>
     template<typename Event>
     pool_handler<Event> & assure() {
     pool_handler<Event> & assure() {
-        const auto etype = event_family<Event>::type;
+        const auto etype = event_family<Event>::type();
 
 
         if(!(etype < pools.size())) {
         if(!(etype < pools.size())) {
             pools.resize(etype+1);
             pools.resize(etype+1);

+ 1 - 1
src/entt/signal/emitter.hpp

@@ -118,7 +118,7 @@ class emitter {
 
 
     template<typename Event>
     template<typename Event>
     const pool_handler<Event> & assure() const {
     const pool_handler<Event> & assure() const {
-        const auto etype = event_family<std::decay_t<Event>>::type;
+        const auto etype = event_family<std::decay_t<Event>>::type();
 
 
         if(!(etype < pools.size())) {
         if(!(etype < pools.size())) {
             pools.resize(etype+1);
             pools.resize(etype+1);

+ 1 - 1
test/CMakeLists.txt

@@ -44,7 +44,7 @@ endif()
 # Test lib
 # Test lib
 
 
 if(BUILD_LIB)
 if(BUILD_LIB)
-#    SETUP_AND_ADD_LIB_TEST(dispatcher)
+    SETUP_AND_ADD_LIB_TEST(dispatcher)
 #    SETUP_AND_ADD_LIB_TEST(emitter)
 #    SETUP_AND_ADD_LIB_TEST(emitter)
     SETUP_AND_ADD_LIB_TEST(meta)
     SETUP_AND_ADD_LIB_TEST(meta)
 #    SETUP_AND_ADD_LIB_TEST(registry)
 #    SETUP_AND_ADD_LIB_TEST(registry)

+ 7 - 7
test/entt/core/family.cpp

@@ -8,10 +8,10 @@ template<typename Type>
 using another_family = entt::family<Type, struct another_family_type>;
 using another_family = entt::family<Type, struct another_family_type>;
 
 
 TEST(Family, Functionalities) {
 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<int>::type();
+    auto t2 = a_family<int>::type();
+    auto t3 = a_family<char>::type();
+    auto t4 = another_family<double>::type();
 
 
     ASSERT_EQ(t1, t2);
     ASSERT_EQ(t1, t2);
     ASSERT_NE(t1, t3);
     ASSERT_NE(t1, t3);
@@ -19,7 +19,7 @@ TEST(Family, Functionalities) {
 }
 }
 
 
 TEST(Family, Uniqueness) {
 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<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());
 }
 }

+ 6 - 11
test/lib/dispatcher/lib.cpp

@@ -1,16 +1,11 @@
+#define ENTT_API_EXPORT
+
+#include <entt/lib/attribute.h>
 #include <entt/signal/dispatcher.hpp>
 #include <entt/signal/dispatcher.hpp>
 #include "types.h"
 #include "types.h"
 
 
-#ifndef LIB_EXPORT
-#if defined _WIN32 || defined __CYGWIN__
-#define LIB_EXPORT __declspec(dllexport)
-#elif defined __GNUC__
-#define LIB_EXPORT __attribute__((visibility("default")))
-#else
-#define LIB_EXPORT
-#endif
-#endif
+template struct entt::family<event, entt::dispatcher::dispatcher_event_family>;
 
 
-LIB_EXPORT void trigger_event(int value, entt::dispatcher &dispatcher) {
-    dispatcher.trigger<event>(value);
+ENTT_EXPORT void trigger(int value, entt::dispatcher &dispatcher) {
+    dispatcher.trigger<message>(value);
 }
 }

+ 10 - 6
test/lib/dispatcher/main.cpp

@@ -1,12 +1,16 @@
+#define ENTT_API_IMPORT
+
 #include <gtest/gtest.h>
 #include <gtest/gtest.h>
+#include <entt/core/utility.hpp>
+#include <entt/lib/attribute.h>
 #include <entt/signal/dispatcher.hpp>
 #include <entt/signal/dispatcher.hpp>
 #include "types.h"
 #include "types.h"
 
 
-extern void trigger_event(int, entt::dispatcher &);
+ENTT_API void trigger(int, entt::dispatcher &);
 
 
 struct listener {
 struct listener {
-    void on_int(int) { FAIL(); }
-    void on_event(event ev) { value = ev.payload; }
+    void on(event) { FAIL(); }
+    void on(message msg) { value = msg.payload; }
     int value{};
     int value{};
 };
 };
 
 
@@ -14,9 +18,9 @@ TEST(Lib, Dispatcher) {
     entt::dispatcher dispatcher;
     entt::dispatcher dispatcher;
     listener listener;
     listener listener;
 
 
-    dispatcher.sink<int>().connect<&listener::on_int>(listener);
-    dispatcher.sink<event>().connect<&listener::on_event>(listener);
-    trigger_event(42, dispatcher);
+    dispatcher.sink<event>().connect<entt::overload<void(event)>(&listener::on)>(listener);
+    dispatcher.sink<message>().connect<entt::overload<void(message)>(&listener::on)>(listener);
+    trigger(42, dispatcher);
 
 
     ASSERT_EQ(listener.value, 42);
     ASSERT_EQ(listener.value, 42);
 }
 }

+ 10 - 3
test/lib/dispatcher/types.h

@@ -1,5 +1,12 @@
-#include <entt/core/type_traits.hpp>
+#ifndef ENTT_LIB_DISPATCHER_TYPES_H
+#define ENTT_LIB_DISPATCHER_TYPES_H
 
 
-ENTT_NAMED_STRUCT(event, {
+#include <entt/lib/attribute.h>
+
+struct ENTT_API event {};
+
+struct ENTT_API message {
     int payload;
     int payload;
-});
+};
+
+#endif // ENTT_LIB_DISPATCHER_TYPES_H