From acd75ffe1ca1e2df782ad9d1b893bfd40e9d050e Mon Sep 17 00:00:00 2001 From: Michele Caini Date: Thu, 30 Sep 2021 16:34:09 +0200 Subject: [PATCH] iterator: added utility input_iterator_proxy --- src/entt/core/iterator.hpp | 43 +++++++++++++++++++++++++++++++++++++ src/entt/entt.hpp | 1 + test/CMakeLists.txt | 1 + test/entt/core/iterator.cpp | 13 +++++++++++ 4 files changed, 58 insertions(+) create mode 100644 src/entt/core/iterator.hpp create mode 100644 test/entt/core/iterator.cpp diff --git a/src/entt/core/iterator.hpp b/src/entt/core/iterator.hpp new file mode 100644 index 000000000..f0370ea5e --- /dev/null +++ b/src/entt/core/iterator.hpp @@ -0,0 +1,43 @@ +#ifndef ENTT_CORE_ITERATOR_HPP +#define ENTT_CORE_ITERATOR_HPP + +#include +#include "../config/config.h" + +namespace entt { + +/** + * @brief Helper type to use as pointer with input iterators. + * @tparam Type of wrapped value. + */ +template +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(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 +input_iterator_proxy(Type &&) -> input_iterator_proxy; + +} // namespace entt + +#endif diff --git a/src/entt/entt.hpp b/src/entt/entt.hpp index 64b18b19d..1c9077f1e 100644 --- a/src/entt/entt.hpp +++ b/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" diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index b56e3385a..f15fab825 100644 --- a/test/CMakeLists.txt +++ b/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) diff --git a/test/entt/core/iterator.cpp b/test/entt/core/iterator.cpp new file mode 100644 index 000000000..cb4f8bafc --- /dev/null +++ b/test/entt/core/iterator.cpp @@ -0,0 +1,13 @@ +#include +#include + +struct clazz { + int value{0}; +}; + +TEST(Iterator, InputIteratorProxy) { + entt::input_iterator_proxy proxy{clazz{}}; + proxy->value = 42; + + ASSERT_EQ(proxy->value, 42); +}