Browse Source

iterator: added utility input_iterator_proxy

Michele Caini 4 years ago
parent
commit
acd75ffe1c
4 changed files with 58 additions and 0 deletions
  1. 43 0
      src/entt/core/iterator.hpp
  2. 1 0
      src/entt/entt.hpp
  3. 1 0
      test/CMakeLists.txt
  4. 13 0
      test/entt/core/iterator.cpp

+ 43 - 0
src/entt/core/iterator.hpp

@@ -0,0 +1,43 @@
+#ifndef ENTT_CORE_ITERATOR_HPP
+#define ENTT_CORE_ITERATOR_HPP
+
+#include <memory>
+#include "../config/config.h"
+
+namespace entt {
+
+/**
+ * @brief Helper type to use as pointer with input iterators.
+ * @tparam Type of wrapped value.
+ */
+template<typename Type>
+struct input_iterator_proxy {
+    /**
+     * @brief Constructs a proxy object from a given value.
+     * @param val Value to use to initialize the proxy object.
+     */
+    input_iterator_proxy(Type &&val)
+        : value{std::forward<Type>(val)} {}
+
+    /**
+     * @brief Access operator for accessing wrapped values.
+     * @return A pointer to the wrapped value.
+     */
+    Type *operator->() ENTT_NOEXCEPT {
+        return std::addressof(value);
+    }
+
+private:
+    Type value;
+};
+
+/**
+ * @brief Deduction guide.
+ * @tparam Type Type of wrapped value.
+ */
+template<typename Type>
+input_iterator_proxy(Type &&) -> input_iterator_proxy<Type>;
+
+} // namespace entt
+
+#endif

+ 1 - 0
src/entt/entt.hpp

@@ -7,6 +7,7 @@
 #include "core/family.hpp"
 #include "core/hashed_string.hpp"
 #include "core/ident.hpp"
+#include "core/iterator.hpp"
 #include "core/memory.hpp"
 #include "core/monostate.hpp"
 #include "core/tuple.hpp"

+ 1 - 0
test/CMakeLists.txt

@@ -168,6 +168,7 @@ SETUP_BASIC_TEST(enum entt/core/enum.cpp)
 SETUP_BASIC_TEST(family entt/core/family.cpp)
 SETUP_BASIC_TEST(hashed_string entt/core/hashed_string.cpp)
 SETUP_BASIC_TEST(ident entt/core/ident.cpp)
+SETUP_BASIC_TEST(iterator entt/core/iterator.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)

+ 13 - 0
test/entt/core/iterator.cpp

@@ -0,0 +1,13 @@
+#include <gtest/gtest.h>
+#include <entt/core/iterator.hpp>
+
+struct clazz {
+    int value{0};
+};
+
+TEST(Iterator, InputIteratorProxy) {
+    entt::input_iterator_proxy proxy{clazz{}};
+    proxy->value = 42;
+
+    ASSERT_EQ(proxy->value, 42);
+}