Просмотр исходного кода

memory: unfancy -> to_address (waiting for C++20)

Michele Caini 4 лет назад
Родитель
Сommit
99621cf08a
3 измененных файлов с 12 добавлено и 12 удалено
  1. 7 7
      src/entt/core/memory.hpp
  2. 2 2
      src/entt/entity/storage.hpp
  3. 3 3
      test/entt/core/memory.cpp

+ 7 - 7
src/entt/core/memory.hpp

@@ -4,25 +4,25 @@
 
 #include <memory>
 #include <type_traits>
+#include <utility>
 #include "../config/config.h"
 
 
 namespace entt {
 
 
-
 /**
- * @brief Unwraps fancy pointers, does nothing otherwise.
+ * @brief Unwraps fancy pointers, does nothing otherwise (waiting for C++20).
  * @tparam Type Pointer type.
- * @param ptr A pointer to evaluate.
- * @return A plain pointer.
+ * @param ptr Fancy or raw pointer.
+ * @return A raw pointer that represents the address of the original pointer.
  */
 template<typename Type>
-[[nodiscard]] constexpr auto * unfancy(Type ptr) ENTT_NOEXCEPT {
-    if constexpr(std::is_pointer_v<Type>) {
+constexpr auto to_address(Type &&ptr) ENTT_NOEXCEPT {
+    if constexpr(std::is_pointer_v<std::remove_const_t<std::remove_reference_t<Type>>>) {
         return ptr;
     } else {
-        return std::addressof(*ptr);
+        return to_address(std::forward<Type>(ptr).operator->());
     }
 }
 

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

@@ -251,9 +251,9 @@ class basic_storage: public basic_sparse_set<Entity, typename std::allocator_tra
     template<typename... Args>
     void construct(alloc_pointer ptr, Args &&... args) {
         if constexpr(std::is_aggregate_v<value_type>) {
-            alloc_traits::construct(bucket.first(), unfancy(ptr), Type{std::forward<Args>(args)...});
+            alloc_traits::construct(bucket.first(), to_address(ptr), Type{std::forward<Args>(args)...});
         } else {
-            alloc_traits::construct(bucket.first(), unfancy(ptr), std::forward<Args>(args)...);
+            alloc_traits::construct(bucket.first(), to_address(ptr), std::forward<Args>(args)...);
         }
     }
 

+ 3 - 3
test/entt/core/memory.cpp

@@ -2,10 +2,10 @@
 #include <gtest/gtest.h>
 #include <entt/core/memory.hpp>
 
-TEST(Memory, Unfancy) {
+TEST(Memory, ToAddress) {
     std::shared_ptr<int> shared = std::make_shared<int>();
     auto *plain = std::addressof(*shared);
 
-    ASSERT_EQ(entt::unfancy(shared), plain);
-    ASSERT_EQ(entt::unfancy(plain), plain);
+    ASSERT_EQ(entt::to_address(shared), plain);
+    ASSERT_EQ(entt::to_address(plain), plain);
 }