Selaa lähdekoodia

tuple: is_tuple[_v] implementation

Michele Caini 3 vuotta sitten
vanhempi
commit
95b443af16
2 muutettua tiedostoa jossa 42 lisäystä ja 0 poistoa
  1. 35 0
      src/entt/core/tuple.hpp
  2. 7 0
      test/entt/core/tuple.cpp

+ 35 - 0
src/entt/core/tuple.hpp

@@ -7,6 +7,41 @@
 
 namespace entt {
 
+/**
+ * @cond TURN_OFF_DOXYGEN
+ * Internal details not to be documented.
+ */
+
+namespace internal {
+
+template<typename>
+struct is_tuple_impl: std::false_type {};
+
+template<typename... Args>
+struct is_tuple_impl<std::tuple<Args...>>: std::true_type {};
+
+} // namespace internal
+
+/**
+ * Internal details not to be documented.
+ * @endcond
+ */
+
+/**
+ * @brief Provides the member constant `value` to true if a given type is a
+ * tuple, false otherwise.
+ * @tparam Type The type to test.
+ */
+template<typename Type>
+struct is_tuple: internal::is_tuple_impl<std::remove_cv_t<Type>> {};
+
+/**
+ * @brief Helper variable template.
+ * @tparam Type The type to test.
+ */
+template<typename Type>
+inline constexpr bool is_tuple_v = is_tuple<Type>::value;
+
 /**
  * @brief Utility function to unwrap tuples of a single element.
  * @tparam Type Tuple type of any sizes.

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

@@ -2,6 +2,13 @@
 #include <gtest/gtest.h>
 #include <entt/core/tuple.hpp>
 
+TEST(Tuple, IsTuple) {
+    static_assert(!entt::is_tuple_v<int>);
+    static_assert(entt::is_tuple_v<std::tuple<>>);
+    static_assert(entt::is_tuple_v<std::tuple<int>>);
+    static_assert(entt::is_tuple_v<std::tuple<int, char>>);
+}
+
 TEST(Tuple, UnwrapTuple) {
     auto single = std::make_tuple(42);
     auto multi = std::make_tuple(42, 'c');