Parcourir la source

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

Michele Caini il y a 6 ans
Parent
commit
8bb1518d09

+ 2 - 1
src/entt/core/type_traits.hpp

@@ -6,6 +6,7 @@
 #include <type_traits>
 #include "../config/config.h"
 #include "../core/hashed_string.hpp"
+#include "../lib/attribute.h"
 
 
 namespace entt {
@@ -176,7 +177,7 @@ constexpr auto is_equality_comparable_v = is_equality_comparable<Type>::value;
  * @param type The underlying type for the enum class.
  */
 #define ENTT_OPAQUE_TYPE(clazz, type)\
-    enum class clazz: type {};\
+    enum class ENTT_API clazz: type {};\
     constexpr auto to_integer(const clazz id) ENTT_NOEXCEPT {\
         return std::underlying_type_t<clazz>(id);\
     }\

+ 5 - 2
src/entt/entity/registry.hpp

@@ -42,11 +42,14 @@ namespace entt {
  */
 template<typename Entity>
 class basic_registry {
+    struct ENTT_API registry_context_family;
+    struct ENTT_API registry_component_family;
+
     template<typename Type>
-    using context_family = family<Type, struct ENTT_API internal_registry_context_family>;
+    using context_family = family<Type, registry_context_family>;
 
     template<typename Type>
-    using component_family = family<Type, struct ENTT_API internal_registry_component_family>;
+    using component_family = family<Type, registry_component_family>;
 
     using traits_type = entt_traits<std::underlying_type_t<Entity>>;
 

+ 1 - 1
test/CMakeLists.txt

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

+ 7 - 14
test/lib/registry/lib.cpp

@@ -1,17 +1,10 @@
+#define ENTT_API_EXPORT
+
 #include <entt/entity/registry.hpp>
+#include <entt/lib/attribute.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
-
-LIB_EXPORT typename entt::component int_type() {
+ENTT_API entt::component int_type() {
     entt::registry registry;
 
     (void)registry.type<double>();
@@ -20,7 +13,7 @@ LIB_EXPORT typename entt::component int_type() {
     return registry.type<int>();
 }
 
-LIB_EXPORT typename entt::component char_type() {
+ENTT_API entt::component char_type() {
     entt::registry registry;
 
     (void)registry.type<double>();
@@ -29,14 +22,14 @@ LIB_EXPORT typename entt::component char_type() {
     return registry.type<char>();
 }
 
-LIB_EXPORT void update_position(int delta, entt::registry &registry) {
+ENTT_API void update_position(int delta, entt::registry &registry) {
     registry.view<position, velocity>().each([delta](auto &pos, auto &vel) {
         pos.x += delta * vel.dx;
         pos.y += delta * vel.dy;
     });
 }
 
-LIB_EXPORT void assign_velocity(int vel, entt::registry &registry) {
+ENTT_API void assign_velocity(int vel, entt::registry &registry) {
     for(auto entity: registry.view<position>()) {
         registry.assign<velocity>(entity, vel, vel);
     }

+ 9 - 6
test/lib/registry/main.cpp

@@ -1,13 +1,16 @@
+#define ENTT_API_IMPORT
+
 #include <gtest/gtest.h>
 #include <entt/entity/entity.hpp>
 #include <entt/entity/registry.hpp>
+#include <entt/lib/attribute.h>
 #include "types.h"
 
-extern typename entt::component int_type();
-extern typename entt::component char_type();
+ENTT_API entt::component int_type();
+ENTT_API entt::component char_type();
 
-extern void update_position(int, entt::registry &);
-extern void assign_velocity(int, entt::registry &);
+ENTT_API void update_position(int, entt::registry &);
+ENTT_API void assign_velocity(int, entt::registry &);
 
 TEST(Lib, Types) {
     entt::registry registry;
@@ -32,8 +35,8 @@ TEST(Lib, Registry) {
 
     assign_velocity(2, registry);
 
-    ASSERT_EQ(registry.size<position>(), entt::registry::size_type{3});
-    ASSERT_EQ(registry.size<velocity>(), entt::registry::size_type{3});
+    ASSERT_EQ(registry.size<position>(), 3u);
+    ASSERT_EQ(registry.size<velocity>(), 3u);
 
     update_position(1, registry);
 

+ 9 - 7
test/lib/registry/types.h

@@ -1,14 +1,16 @@
-#include <entt/core/type_traits.hpp>
+#ifndef ENTT_LIB_REGISTRY_TYPES_H
+#define ENTT_LIB_REGISTRY_TYPES_H
 
-ENTT_NAMED_STRUCT(position, {
+#include <entt/lib/attribute.h>
+
+struct ENTT_API position {
     int x;
     int y;
-});
+};
 
-ENTT_NAMED_STRUCT(velocity, {
+struct ENTT_API velocity {
     int dx;
     int dy;
-});
+};
 
-ENTT_NAMED_TYPE(int);
-ENTT_NAMED_TYPE(char);
+#endif // ENTT_LIB_REGISTRY_TYPES_H