1
0
skypjack 3 долоо хоног өмнө
parent
commit
ed590b7ac8

+ 11 - 0
src/entt/core/concepts.hpp

@@ -1,6 +1,7 @@
 #ifndef ENTT_CORE_CONCEPTS_HPP
 #define ENTT_CORE_CONCEPTS_HPP
 
+#include <concepts>
 #include <type_traits>
 
 namespace entt {
@@ -12,6 +13,16 @@ namespace entt {
 template<typename Type>
 concept cvref_unqualified = std::is_same_v<std::remove_cvref_t<Type>, Type>;
 
+/**
+ * @brief Specifies that a type is likely an allocator type.
+ * @tparam Type Type to check.
+ */
+template<typename Type>
+concept allocator_like = requires(Type alloc, typename Type::value_type *value) {
+    { alloc.allocate(0) } -> std::same_as<decltype(value)>;
+    { alloc.deallocate(value, 0) } -> std::same_as<void>;
+};
+
 } // namespace entt
 
 #endif

+ 7 - 0
test/entt/core/concepts.cpp

@@ -12,3 +12,10 @@ TEST(Concepts, CVRefUnqualified) {
     ASSERT_FALSE(entt::cvref_unqualified<const std::shared_ptr<int>>);
     ASSERT_FALSE(entt::cvref_unqualified<std::shared_ptr<int> &>);
 }
+
+TEST(Concepts, AllocatorLike) {
+    ASSERT_FALSE(entt::allocator_like<int>);
+    ASSERT_TRUE(entt::allocator_like<std::allocator<int>>);
+    ASSERT_TRUE(entt::allocator_like<std::allocator<void>>);
+    ASSERT_FALSE(entt::allocator_like<std::shared_ptr<int>>);
+}