Преглед изворни кода

core: added tuple header and unwrap_tuple utility

Michele Caini пре 4 година
родитељ
комит
6cd44248c7
4 измењених фајлова са 60 додато и 0 уклоњено
  1. 33 0
      src/entt/core/tuple.hpp
  2. 1 0
      src/entt/entt.hpp
  3. 1 0
      test/CMakeLists.txt
  4. 25 0
      test/entt/core/tuple.cpp

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

@@ -0,0 +1,33 @@
+#ifndef ENTT_CORE_TUPLE_HPP
+#define ENTT_CORE_TUPLE_HPP
+
+
+#include <tuple>
+#include <type_traits>
+#include "../config/config.h"
+
+
+namespace entt {
+
+
+/**
+ * @brief Utility function to unwrap tuples of a single element.
+ * @tparam Type Tuple type of any sizes.
+ * @param value A tuple object of the given type.
+ * @return The tuple itself if it contains more than one element, the first
+ * element otherwise.
+ */
+template<typename Type>
+constexpr decltype(auto) unwrap_tuple(Type && value) ENTT_NOEXCEPT {
+	if constexpr(std::tuple_size_v<std::remove_reference_t<Type>> == 1u) {
+		return std::get<0>(std::forward<Type>(value));
+	} else {
+		return std::forward<Type>(value);
+	}
+}
+
+
+}
+
+
+#endif

+ 1 - 0
src/entt/entt.hpp

@@ -9,6 +9,7 @@
 #include "core/ident.hpp"
 #include "core/memory.hpp"
 #include "core/monostate.hpp"
+#include "core/tuple.hpp"
 #include "core/type_info.hpp"
 #include "core/type_traits.hpp"
 #include "core/utility.hpp"

+ 1 - 0
test/CMakeLists.txt

@@ -170,6 +170,7 @@ SETUP_BASIC_TEST(hashed_string entt/core/hashed_string.cpp)
 SETUP_BASIC_TEST(ident entt/core/ident.cpp)
 SETUP_BASIC_TEST(memory entt/core/memory.cpp)
 SETUP_BASIC_TEST(monostate entt/core/monostate.cpp)
+SETUP_BASIC_TEST(tuple entt/core/tuple.cpp)
 SETUP_BASIC_TEST(type_info entt/core/type_info.cpp)
 SETUP_BASIC_TEST(type_traits entt/core/type_traits.cpp)
 SETUP_BASIC_TEST(utility entt/core/utility.cpp)

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

@@ -0,0 +1,25 @@
+#include <tuple>
+#include <gtest/gtest.h>
+#include <entt/core/tuple.hpp>
+
+TEST(Tuple, UnwrapTuple) {
+	auto single = std::make_tuple(42);
+	auto multi = std::make_tuple(42, 'c');
+	auto ref = std::forward_as_tuple(std::get<0>(single));
+
+	ASSERT_TRUE((std::is_same_v<decltype(entt::unwrap_tuple(single)), int &>));
+	ASSERT_TRUE((std::is_same_v<decltype(entt::unwrap_tuple(multi)), std::tuple<int, char> &>));
+	ASSERT_TRUE((std::is_same_v<decltype(entt::unwrap_tuple(ref)), int &>));
+
+	ASSERT_TRUE((std::is_same_v<decltype(entt::unwrap_tuple(std::move(single))), int &&>));
+	ASSERT_TRUE((std::is_same_v<decltype(entt::unwrap_tuple(std::move(multi))), std::tuple<int, char> &&>));
+	ASSERT_TRUE((std::is_same_v<decltype(entt::unwrap_tuple(std::move(ref))), int &>));
+
+	ASSERT_TRUE((std::is_same_v<decltype(entt::unwrap_tuple(std::as_const(single))), const int &>));
+	ASSERT_TRUE((std::is_same_v<decltype(entt::unwrap_tuple(std::as_const(multi))), const std::tuple<int, char> &>));
+	ASSERT_TRUE((std::is_same_v<decltype(entt::unwrap_tuple(std::as_const(ref))), int &>));
+
+	ASSERT_EQ(entt::unwrap_tuple(single), 42);
+	ASSERT_EQ(entt::unwrap_tuple(multi), multi);
+	ASSERT_EQ(entt::unwrap_tuple(std::move(ref)), 42);
+}