From b8fb8a69499d9fbb5384908c863a9f6ad1aa3150 Mon Sep 17 00:00:00 2001 From: Johan Mabille Date: Wed, 3 Apr 2024 17:20:33 +0200 Subject: [PATCH] replace is_value_stored with can_any_allocator_sbo concept --- include/sparrow/allocator.hpp | 55 +++++++++++++++++------------------ 1 file changed, 27 insertions(+), 28 deletions(-) diff --git a/include/sparrow/allocator.hpp b/include/sparrow/allocator.hpp index ffc85634..24a6319e 100644 --- a/include/sparrow/allocator.hpp +++ b/include/sparrow/allocator.hpp @@ -40,20 +40,19 @@ namespace sparrow { alloc.deallocate(p, n) } -> std::same_as; }; + template + concept can_any_allocator_sbo = allocator && + (std::same_as> || std::same_as>); + /* * Type erasure class for allocators. This allows to use any kind of allocator * (standard, polymorphic) without having to expose it as a template parameter. * * @tparam T value_type of the allocator - * @tparam DA default allocator, instantiated when calling the default constructor */ - template > + template class any_allocator { - template - static constexpr bool is_value_stored = - std::is_same_v> || std::is_same_v>; - public: using value_type = T; @@ -137,7 +136,7 @@ namespace sparrow } template - requires is_value_stored + requires can_any_allocator_sbo A&& make_storage(A&& alloc) const { return std::forward(alloc); @@ -148,7 +147,7 @@ namespace sparrow return std::visit([](auto&& arg) -> storage_type { using A = std::decay_t; - if constexpr (is_value_stored) + if constexpr (can_any_allocator_sbo) return { A(arg) }; else return { arg->clone() }; @@ -162,63 +161,63 @@ namespace sparrow * any_allocator implementation * ********************************/ - template - any_allocator::any_allocator() - : m_storage(make_storage(DA())) + template + any_allocator::any_allocator() + : m_storage(make_storage(std::allocator())) { } - template - any_allocator::any_allocator(const any_allocator& rhs) + template + any_allocator::any_allocator(const any_allocator& rhs) : m_storage(copy_storage(rhs.m_storage)) { } - template - [[nodiscard]] T* any_allocator::allocate(std::size_t n) + template + [[nodiscard]] T* any_allocator::allocate(std::size_t n) { return std::visit([n](auto&& arg) { using A = std::decay_t; - if constexpr (is_value_stored) + if constexpr (can_any_allocator_sbo) return arg.allocate(n); else return arg->allocate(n); }, m_storage); } - template - void any_allocator::deallocate(T* p, std::size_t n) + template + void any_allocator::deallocate(T* p, std::size_t n) { return std::visit([p, n](auto&& arg) { using A = std::decay_t; - if constexpr (is_value_stored) + if constexpr (can_any_allocator_sbo) arg.deallocate(p, n); else arg->deallocate(p, n); }, m_storage); } - template - any_allocator any_allocator::select_on_container_copy_construction() const + template + any_allocator any_allocator::select_on_container_copy_construction() const { return any_allocator(*this); } - template - bool any_allocator::equal(const any_allocator& rhs) const + template + bool any_allocator::equal(const any_allocator& rhs) const { // YOLO!! return std::visit([&rhs](auto&& arg) { using A = std::decay_t; - if constexpr (is_value_stored) + if constexpr (can_any_allocator_sbo) { return std::visit([&arg](auto&& arg2) { using A2 = std::decay_t; - if constexpr (is_value_stored && std::same_as) + if constexpr (can_any_allocator_sbo && std::same_as) return arg == arg2; else return false; @@ -229,7 +228,7 @@ namespace sparrow return std::visit([&arg](auto&& arg2) { using A2 = std::decay_t; - if constexpr (is_value_stored) + if constexpr (can_any_allocator_sbo) return false; else return arg->equal(*arg2); @@ -239,8 +238,8 @@ namespace sparrow }, m_storage); } - template - bool operator==(const any_allocator& lhs, const any_allocator& rhs) + template + bool operator==(const any_allocator& lhs, const any_allocator& rhs) { return lhs.equal(rhs); }