Răsfoiți Sursa

core: make inclusion of custom to_address conditional

skypjack 2 luni în urmă
părinte
comite
884f698fab

+ 1 - 0
CMakeLists.txt

@@ -194,6 +194,7 @@ if(ENTT_INCLUDE_HEADERS)
         signal/emitter.hpp
         signal/fwd.hpp
         signal/sigh.hpp
+        stl/memory.hpp
         tools/davey.hpp
         entt.hpp
         fwd.hpp

+ 3 - 17
src/entt/core/memory.hpp

@@ -7,24 +7,10 @@
 #include <type_traits>
 #include <utility>
 #include "../config/config.h"
+#include "../stl/memory.hpp"
 
 namespace entt {
 
-/**
- * @brief Unwraps fancy pointers, does nothing otherwise (waiting for C++20).
- * @tparam Type Pointer type.
- * @param ptr Fancy or raw pointer.
- * @return A raw pointer that represents the address of the original pointer.
- */
-template<typename Type>
-[[nodiscard]] constexpr auto to_address(Type &&ptr) noexcept {
-    if constexpr(std::is_pointer_v<std::decay_t<Type>>) {
-        return ptr;
-    } else {
-        return to_address(std::forward<Type>(ptr).operator->());
-    }
-}
-
 /**
  * @brief Utility function to design allocation-aware containers.
  * @tparam Allocator Type of allocator.
@@ -91,7 +77,7 @@ struct allocation_deleter: private Allocator {
      */
     constexpr void operator()(pointer ptr) noexcept(std::is_nothrow_destructible_v<typename allocator_type::value_type>) {
         using alloc_traits = std::allocator_traits<Allocator>;
-        alloc_traits::destroy(*this, to_address(ptr));
+        alloc_traits::destroy(*this, stl::to_address(ptr));
         alloc_traits::deallocate(*this, ptr, 1u);
     }
 };
@@ -116,7 +102,7 @@ constexpr auto allocate_unique(Allocator &allocator, Args &&...args) {
     auto ptr = alloc_traits::allocate(alloc, 1u);
 
     ENTT_TRY {
-        alloc_traits::construct(alloc, to_address(ptr), std::forward<Args>(args)...);
+        alloc_traits::construct(alloc, stl::to_address(ptr), std::forward<Args>(args)...);
     }
     ENTT_CATCH {
         alloc_traits::deallocate(alloc, ptr, 1u);

+ 3 - 2
src/entt/entity/storage.hpp

@@ -13,6 +13,7 @@
 #include "../core/iterator.hpp"
 #include "../core/memory.hpp"
 #include "../core/type_info.hpp"
+#include "../stl/memory.hpp"
 #include "component.hpp"
 #include "entity.hpp"
 #include "fwd.hpp"
@@ -265,7 +266,7 @@ class basic_storage: public basic_sparse_set<Entity, typename std::allocator_tra
         const auto it = base_type::try_emplace(entt, force_back);
 
         ENTT_TRY {
-            auto *elem = to_address(assure_at_least(static_cast<size_type>(it.index())));
+            auto *elem = stl::to_address(assure_at_least(static_cast<size_type>(it.index())));
             entt::uninitialized_construct_using_allocator(elem, get_allocator(), std::forward<Args>(args)...);
         }
         ENTT_CATCH {
@@ -306,7 +307,7 @@ class basic_storage: public basic_sparse_set<Entity, typename std::allocator_tra
     void move_to(const std::size_t lhs, const std::size_t rhs) {
         auto &elem = element_at(lhs);
         allocator_type allocator{get_allocator()};
-        entt::uninitialized_construct_using_allocator(to_address(assure_at_least(rhs)), allocator, std::move(elem));
+        entt::uninitialized_construct_using_allocator(stl::to_address(assure_at_least(rhs)), allocator, std::move(elem));
         alloc_traits::destroy(allocator, std::addressof(elem));
     }
 

+ 1 - 0
src/entt/entt.hpp

@@ -65,4 +65,5 @@ namespace entt {}
 #include "signal/dispatcher.hpp"
 #include "signal/emitter.hpp"
 #include "signal/sigh.hpp"
+#include "stl/memory.hpp"
 // IWYU pragma: end_exports

+ 37 - 0
src/entt/stl/memory.hpp

@@ -0,0 +1,37 @@
+#ifndef ENTT_STL_MEMORY_HPP
+#define ENTT_STL_MEMORY_HPP
+
+namespace entt::stl {
+
+/*! @cond TURN_OFF_DOXYGEN */
+namespace internal {
+
+template<typename Type>
+[[nodiscard]] constexpr auto to_address(Type &&ptr) noexcept {
+    if constexpr(std::is_pointer_v<std::decay_t<Type>>) {
+        return ptr;
+    } else {
+        return to_address(std::forward<Type>(ptr).operator->());
+    }
+}
+
+} // namespace internal
+/*! @endcond */
+
+#if __has_include(<version>)
+#    include <version>
+#
+#    if defined(__cpp_lib_to_address)
+#        include <memory>
+using std::to_address;
+#    else
+using stl::internal::to_address;
+#    endif
+#
+#else
+using stl::internal;
+#endif
+
+} // namespace entt::stl
+
+#endif

+ 4 - 0
test/CMakeLists.txt

@@ -301,3 +301,7 @@ SETUP_BASIC_TEST(delegate entt/signal/delegate.cpp)
 SETUP_BASIC_TEST(dispatcher entt/signal/dispatcher.cpp)
 SETUP_BASIC_TEST(emitter entt/signal/emitter.cpp)
 SETUP_BASIC_TEST(sigh entt/signal/sigh.cpp)
+
+# Test stl
+
+SETUP_BASIC_TEST(stl_memory entt/stl/memory.cpp)

+ 0 - 8
test/entt/core/memory.cpp

@@ -13,14 +13,6 @@
 #include "../../common/throwing_type.hpp"
 #include "../../common/tracked_memory_resource.hpp"
 
-TEST(ToAddress, Functionalities) {
-    const std::shared_ptr<int> shared = std::make_shared<int>();
-    auto *plain = &*shared;
-
-    ASSERT_EQ(entt::to_address(shared), plain);
-    ASSERT_EQ(entt::to_address(plain), plain);
-}
-
 TEST(PoccaPocmaAndPocs, Functionalities) {
     test::basic_test_allocator<int> lhs;
     test::basic_test_allocator<int> rhs;

+ 11 - 0
test/entt/stl/memory.cpp

@@ -0,0 +1,11 @@
+#include <memory>
+#include <gtest/gtest.h>
+#include <entt/stl/memory.hpp>
+
+TEST(ToAddress, Functionalities) {
+    const std::shared_ptr<int> shared = std::make_shared<int>();
+    auto *plain = &*shared;
+
+    ASSERT_EQ(entt::stl::internal::to_address(shared), plain);
+    ASSERT_EQ(entt::stl::internal::to_address(plain), plain);
+}