Browse Source

stl: functional.hpp for identity

skypjack 2 months ago
parent
commit
09b9099b1c
5 changed files with 51 additions and 0 deletions
  1. 1 0
      CMakeLists.txt
  2. 1 0
      src/entt/entt.hpp
  3. 35 0
      src/entt/stl/functional.hpp
  4. 1 0
      test/CMakeLists.txt
  5. 13 0
      test/entt/stl/functional.cpp

+ 1 - 0
CMakeLists.txt

@@ -194,6 +194,7 @@ if(ENTT_INCLUDE_HEADERS)
         signal/emitter.hpp
         signal/fwd.hpp
         signal/sigh.hpp
+        stl/functional.hpp
         stl/memory.hpp
         tools/davey.hpp
         entt.hpp

+ 1 - 0
src/entt/entt.hpp

@@ -65,5 +65,6 @@ namespace entt {}
 #include "signal/dispatcher.hpp"
 #include "signal/emitter.hpp"
 #include "signal/sigh.hpp"
+#include "stl/functional.hpp"
 #include "stl/memory.hpp"
 // IWYU pragma: end_exports

+ 35 - 0
src/entt/stl/functional.hpp

@@ -0,0 +1,35 @@
+#ifndef ENTT_STL_FUNCTIONAL_HPP
+#define ENTT_STL_FUNCTIONAL_HPP
+
+#include "../config/config.h"
+
+namespace entt::stl {
+
+#ifndef ENTT_FORCE_STL
+#    if __has_include(<version>)
+#        include <version>
+#
+#        if defined(__cpp_lib_ranges)
+#            define ENTT_HAS_IDENTITY
+#            include <functional>
+using std::identity;
+#        endif
+#    endif
+#endif
+
+#ifndef ENTT_HAS_IDENTITY
+#    include <utility>
+
+struct identity {
+    using is_transparent = void;
+
+    template<typename Type>
+    [[nodiscard]] constexpr Type &&operator()(Type &&value) const noexcept {
+        return std::forward<Type>(value);
+    }
+};
+#endif
+
+} // namespace entt::stl
+
+#endif

+ 1 - 0
test/CMakeLists.txt

@@ -304,4 +304,5 @@ SETUP_BASIC_TEST(sigh entt/signal/sigh.cpp)
 
 # Test stl
 
+SETUP_BASIC_TEST(stl_functional entt/stl/functional.cpp ENTT_USE_STL)
 SETUP_BASIC_TEST(stl_memory entt/stl/memory.cpp ENTT_USE_STL)

+ 13 - 0
test/entt/stl/functional.cpp

@@ -0,0 +1,13 @@
+#include <memory>
+#include <gtest/gtest.h>
+#include <entt/core/type_traits.hpp>
+#include <entt/stl/functional.hpp>
+
+TEST(Identity, Functionalities) {
+    const entt::stl::identity identity;
+    int value = 2;
+
+    ASSERT_TRUE(entt::is_transparent_v<entt::stl::identity>);
+    ASSERT_EQ(identity(value), value);
+    ASSERT_EQ(&identity(value), &value);
+}