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

This commit is contained in:
Michele Caini
2021-08-20 15:28:46 +02:00
parent a9ae9ce758
commit 99621cf08a
3 changed files with 12 additions and 12 deletions

View File

@@ -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->());
}
}

View File

@@ -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)...);
}
}

View File

@@ -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);
}