Prechádzať zdrojové kódy

core: type_traits, added is_dereferenceable[_v]

Michele Caini 5 rokov pred
rodič
commit
d1c5a62a9d
4 zmenil súbory, kde vykonal 41 pridanie a 3 odobranie
  1. 1 0
      TODO
  2. 4 1
      docs/md/core.md
  3. 27 1
      src/entt/core/type_traits.hpp
  4. 9 1
      test/entt/core/type_traits.cpp

+ 1 - 0
TODO

@@ -30,3 +30,4 @@ Next:
  - remove internal::find_if
  - add meta_handle::operator-> that returns a meta_any * (easier to use directly)
  - use a dedicate class template to specialize meta views for a better support to customizations
+ - remove operator* from meta_any, meta_handle

+ 4 - 1
docs/md/core.md

@@ -347,7 +347,10 @@ break an assumption and would likely lead to undesired behaviors.
 ## Type traits
 
 A handful of utilities and traits not present in the standard template library
-but which can be useful in everyday life.
+but which can be useful in everyday life.<br/>
+This list **is not** exhaustive and contains only some of the most useful
+classes. Refer to the inline documentation for more information on the features
+offered by this module.
 
 ### Member class type
 

+ 27 - 1
src/entt/core/type_traits.hpp

@@ -193,7 +193,9 @@ struct is_equality_comparable: std::false_type {};
 
 /*! @copydoc is_equality_comparable */
 template<typename Type>
-struct is_equality_comparable<Type, std::void_t<decltype(std::declval<Type>() == std::declval<Type>())>>: std::true_type {};
+struct is_equality_comparable<Type, std::void_t<decltype(std::declval<Type>() == std::declval<Type>())>>
+        : std::true_type
+{};
 
 
 /**
@@ -324,6 +326,30 @@ template<typename Type>
 inline constexpr auto is_dynamic_sequence_container_v = is_dynamic_sequence_container<Type>::value;
 
 
+/**
+ * @brief Provides the member constant `value` to true if a given type is
+ * dereferenceable, false otherwise.
+ * @tparam Type Potentially dereferenceable type.
+ */
+template<typename Type, typename = void>
+struct is_dereferenceable: std::false_type {};
+
+
+/*! @copydoc is_dereferenceable */
+template<typename Type>
+struct is_dereferenceable<Type, std::void_t<decltype(*std::declval<Type>())>>
+        : std::true_type
+{};
+
+
+/**
+ * @brief Helper variable template.
+ * @tparam Type Potentially dereferenceable type.
+ */
+template<typename Type>
+inline constexpr auto is_dereferenceable_v = is_dereferenceable<Type>::value;
+
+
 /**
  * @brief Extracts the class of a non-static member object or function.
  * @tparam Member A pointer to a non-static member object or function.

+ 9 - 1
test/entt/core/type_traits.cpp

@@ -1,5 +1,6 @@
 #include <array>
 #include <map>
+#include <memory>
 #include <set>
 #include <type_traits>
 #include <vector>
@@ -51,7 +52,7 @@ TEST(TypeTraits, IsEqualityComparable) {
     ASSERT_FALSE(entt::is_equality_comparable_v<void>);
 }
 
-TEST(TypeTraits, IsContainerType) {
+TEST(TypeTraits, IsContainer) {
     ASSERT_TRUE(entt::is_container_v<std::vector<int>>);
     ASSERT_FALSE(entt::is_associative_container_v<std::vector<int>>);
     ASSERT_FALSE(entt::is_key_only_associative_container_v<std::vector<int>>);
@@ -77,6 +78,13 @@ TEST(TypeTraits, IsContainerType) {
     ASSERT_FALSE(entt::is_dynamic_sequence_container_v<std::set<int>>);
 }
 
+TEST(TypeTraits, IsDereferenceable) {
+    ASSERT_TRUE(entt::is_dereferenceable_v<int *>);
+    ASSERT_TRUE(entt::is_dereferenceable_v<std::shared_ptr<int>>);
+    ASSERT_TRUE(entt::is_dereferenceable_v<std::unique_ptr<int>>);
+    ASSERT_FALSE(entt::is_dereferenceable_v<int>);
+}
+
 TEST(TypeTraits, MemberClass) {
     struct clazz {
         char foo(int) { return {}; }