Browse Source

meta: range_iterator are now part of ranges

Michele Caini 5 years ago
parent
commit
d0fd9e4618
4 changed files with 89 additions and 141 deletions
  1. 40 58
      src/entt/meta/internal.hpp
  2. 4 4
      src/entt/meta/meta.hpp
  3. 42 76
      src/entt/meta/range.hpp
  4. 3 3
      src/entt/meta/resolve.hpp

+ 40 - 58
src/entt/meta/internal.hpp

@@ -128,53 +128,51 @@ struct meta_type_node {
 
 
 
 
 template<typename Node>
 template<typename Node>
-struct meta_iterator {
-    using difference_type = std::ptrdiff_t;
-    using value_type = Node;
-    using pointer = value_type *;
-    using reference = value_type &;
-    using iterator_category = std::forward_iterator_tag;
-
-    meta_iterator() ENTT_NOEXCEPT = default;
-
-    meta_iterator(Node *head) ENTT_NOEXCEPT
-        : node{head}
-    {}
-
-    meta_iterator & operator++() ENTT_NOEXCEPT {
-        return node = node->next, *this;
-    }
-
-    meta_iterator operator++(int) ENTT_NOEXCEPT {
-        meta_iterator orig = *this;
-        return operator++(), orig;
-    }
+class meta_range {
+    struct range_iterator {
+        using difference_type = std::ptrdiff_t;
+        using value_type = Node;
+        using pointer = value_type *;
+        using reference = value_type &;
+        using iterator_category = std::forward_iterator_tag;
+
+        range_iterator() ENTT_NOEXCEPT = default;
+
+        range_iterator(Node *head) ENTT_NOEXCEPT
+            : node{head}
+        {}
+
+        range_iterator & operator++() ENTT_NOEXCEPT {
+            return node = node->next, *this;
+        }
 
 
-    [[nodiscard]] bool operator==(const meta_iterator &other) const ENTT_NOEXCEPT {
-        return other.node == node;
-    }
+        range_iterator operator++(int) ENTT_NOEXCEPT {
+            range_iterator orig = *this;
+            return operator++(), orig;
+        }
 
 
-    [[nodiscard]] bool operator!=(const meta_iterator &other) const ENTT_NOEXCEPT {
-        return !(*this == other);
-    }
+        [[nodiscard]] bool operator==(const range_iterator &other) const ENTT_NOEXCEPT {
+            return other.node == node;
+        }
 
 
-    [[nodiscard]] pointer operator->() const ENTT_NOEXCEPT {
-        return node;
-    }
+        [[nodiscard]] bool operator!=(const range_iterator &other) const ENTT_NOEXCEPT {
+            return !(*this == other);
+        }
 
 
-    [[nodiscard]] reference operator*() const ENTT_NOEXCEPT {
-        return *operator->();
-    }
+        [[nodiscard]] pointer operator->() const ENTT_NOEXCEPT {
+            return node;
+        }
 
 
-private:
-    Node *node{nullptr};
-};
+        [[nodiscard]] reference operator*() const ENTT_NOEXCEPT {
+            return *operator->();
+        }
 
 
+    private:
+        Node *node{nullptr};
+    };
 
 
-template<typename Node>
-struct meta_range {
-    using iterator = meta_iterator<Node>;
-    using const_iterator = meta_iterator<const Node>;
+public:
+    using iterator = range_iterator;
 
 
     meta_range() ENTT_NOEXCEPT = default;
     meta_range() ENTT_NOEXCEPT = default;
 
 
@@ -182,30 +180,14 @@ struct meta_range {
         : node{head}
         : node{head}
     {}
     {}
 
 
-    iterator begin() ENTT_NOEXCEPT {
+    iterator begin() const ENTT_NOEXCEPT {
         return iterator{node};
         return iterator{node};
     }
     }
 
 
-    const_iterator begin() const ENTT_NOEXCEPT {
-        return const_iterator{node};
-    }
-
-    const_iterator cbegin() const ENTT_NOEXCEPT {
-        return begin();
-    }
-
-    iterator end() ENTT_NOEXCEPT {
+    iterator end() const ENTT_NOEXCEPT {
         return iterator{};
         return iterator{};
     }
     }
 
 
-    const_iterator end() const ENTT_NOEXCEPT {
-        return const_iterator{};
-    }
-
-    const_iterator cend() const ENTT_NOEXCEPT {
-        return end();
-    }
-
 private:
 private:
     Node *node{nullptr};
     Node *node{nullptr};
 };
 };

+ 4 - 4
src/entt/meta/meta.hpp

@@ -642,7 +642,7 @@ struct meta_ctor {
      */
      */
     [[nodiscard]] meta_prop prop(meta_any key) const {
     [[nodiscard]] meta_prop prop(meta_any key) const {
         internal::meta_range range{node->prop};
         internal::meta_range range{node->prop};
-        return std::find_if(range.cbegin(), range.cend(), [&key](const auto &curr) { return curr.key() == key; }).operator->();
+        return std::find_if(range.begin(), range.end(), [&key](const auto &curr) { return curr.key() == key; }).operator->();
     }
     }
 
 
     /**
     /**
@@ -789,7 +789,7 @@ struct meta_data {
      */
      */
     [[nodiscard]] meta_prop prop(meta_any key) const {
     [[nodiscard]] meta_prop prop(meta_any key) const {
         internal::meta_range range{node->prop};
         internal::meta_range range{node->prop};
-        return std::find_if(range.cbegin(), range.cend(), [&key](const auto &curr) { return curr.key() == key; }).operator->();
+        return std::find_if(range.begin(), range.end(), [&key](const auto &curr) { return curr.key() == key; }).operator->();
     }
     }
 
 
     /**
     /**
@@ -909,7 +909,7 @@ struct meta_func {
      */
      */
     [[nodiscard]] meta_prop prop(meta_any key) const {
     [[nodiscard]] meta_prop prop(meta_any key) const {
         internal::meta_range range{node->prop};
         internal::meta_range range{node->prop};
-        return std::find_if(range.cbegin(), range.cend(), [&key](const auto &curr) { return curr.key() == key; }).operator->();
+        return std::find_if(range.begin(), range.end(), [&key](const auto &curr) { return curr.key() == key; }).operator->();
     }
     }
 
 
     /**
     /**
@@ -931,7 +931,7 @@ class meta_type {
     [[nodiscard]] auto ctor(std::index_sequence<Indexes...>) const {
     [[nodiscard]] auto ctor(std::index_sequence<Indexes...>) const {
         internal::meta_range range{node->ctor};
         internal::meta_range range{node->ctor};
 
 
-        return std::find_if(range.cbegin(), range.cend(), [](const auto &candidate) {
+        return std::find_if(range.begin(), range.end(), [](const auto &candidate) {
             return candidate.size == sizeof...(Args) && ([](auto *from, auto *to) {
             return candidate.size == sizeof...(Args) && ([](auto *from, auto *to) {
                 return (from->type_id == to->type_id)
                 return (from->type_id == to->type_id)
                         || internal::find_if<&node_type::base>([to](const auto *curr) { return curr->type()->type_id == to->type_id; }, from)
                         || internal::find_if<&node_type::base>([to](const auto *curr) { return curr->type()->type_id == to->type_id; }, from)

+ 42 - 76
src/entt/meta/range.hpp

@@ -8,90 +8,56 @@
 namespace entt {
 namespace entt {
 
 
 
 
-/**
- * @brief Meta iterator to use to iterate all types of meta objects.
- * @tparam Type Type of meta objects returned by the iterator.
- */
-template<typename Type>
-struct meta_iterator {
-    /*! @brief Signed integer type. */
-    using difference_type = std::ptrdiff_t;
-    /*! @brief Type of meta objects returned by the iterator. */
-    using value_type = Type;
-    /*! @brief Pointer type, `void` on purpose. */
-    using pointer = void;
-    /*! @brief Type of proxy object. */
-    using reference = value_type;
-    /*! @brief Iterator category. */
-    using iterator_category = std::input_iterator_tag;
-    /*! @brief Node type. */
-    using node_type = typename Type::node_type;
-
-    /*! @brief Default constructor. */
-    meta_iterator() ENTT_NOEXCEPT = default;
-
-    /**
-     * @brief Constructs a meta iterator from a given node.
-     * @param head The underlying node with which to construct the iterator.
-     */
-    meta_iterator(node_type *head) ENTT_NOEXCEPT
-        : it{head}
-    {}
-
-    /*! @brief Pre-increment operator. @return This iterator. */
-    meta_iterator & operator++() ENTT_NOEXCEPT {
-        return ++it, *this;
-    }
-
-    /*! @brief Post-increment operator. @return A copy of this iterator. */
-    meta_iterator operator++(int) ENTT_NOEXCEPT {
-        meta_iterator orig = *this;
-        return ++it, orig;
-    }
-
-    /**
-     * @brief Indirection operator.
-     * @return A proxy object to the meta item pointed-to by the iterator.
-     */
-    [[nodiscard]] reference operator*() const ENTT_NOEXCEPT {
-        return it.operator->();
-    }
-
-    /**
-     * @brief Checks if two meta iterators refer to the same meta object.
-     * @param other The meta iterator with which to compare.
-     * @return True if the two meta iterators refer to the same meta object,
-     * false otherwise.
-     */
-    [[nodiscard]] bool operator==(const meta_iterator &other) const ENTT_NOEXCEPT {
-        return other.it == it;
-    }
-
-    /**
-     * @brief Checks if two meta iterators refer to the same meta object.
-     * @param other The meta iterator with which to compare.
-     * @return False if the two meta iterators refer to the same meta object,
-     * true otherwise.
-     */
-    [[nodiscard]] bool operator!=(const meta_iterator &other) const ENTT_NOEXCEPT {
-        return !(*this == other);
-    }
-
-private:
-    internal::meta_iterator<node_type> it{};
-};
-
-
 /**
 /**
  * @brief Iterable range to use to iterate all types of meta objects.
  * @brief Iterable range to use to iterate all types of meta objects.
  * @tparam Type Type of meta objects iterated.
  * @tparam Type Type of meta objects iterated.
  */
  */
 template<typename Type>
 template<typename Type>
-struct meta_range {
+class meta_range {
+    struct range_iterator {
+        using difference_type = std::ptrdiff_t;
+        using value_type = Type;
+        using pointer = void;
+        using reference = value_type;
+        using iterator_category = std::input_iterator_tag;
+        using node_type = typename Type::node_type;
+
+        range_iterator() ENTT_NOEXCEPT = default;
+
+        range_iterator(node_type *head) ENTT_NOEXCEPT
+            : it{head}
+        {}
+
+        range_iterator & operator++() ENTT_NOEXCEPT {
+            return ++it, *this;
+        }
+
+        range_iterator operator++(int) ENTT_NOEXCEPT {
+            range_iterator orig = *this;
+            return ++it, orig;
+        }
+
+        [[nodiscard]] reference operator*() const ENTT_NOEXCEPT {
+            return it.operator->();
+        }
+
+        [[nodiscard]] bool operator==(const range_iterator &other) const ENTT_NOEXCEPT {
+            return other.it == it;
+        }
+
+        [[nodiscard]] bool operator!=(const range_iterator &other) const ENTT_NOEXCEPT {
+            return !(*this == other);
+        }
+
+    private:
+        typename internal::meta_range<node_type>::iterator it{};
+    };
+
+public:
     /*! @brief Node type. */
     /*! @brief Node type. */
     using node_type = typename Type::node_type;
     using node_type = typename Type::node_type;
     /*! @brief Input iterator type. */
     /*! @brief Input iterator type. */
-    using iterator = meta_iterator<Type>;
+    using iterator = range_iterator;
 
 
     /*! @brief Default constructor. */
     /*! @brief Default constructor. */
     meta_range() ENTT_NOEXCEPT = default;
     meta_range() ENTT_NOEXCEPT = default;

+ 3 - 3
src/entt/meta/resolve.hpp

@@ -55,7 +55,7 @@ template<typename Func>
 [[deprecated("use std::find_if and entt::meta_range<meta_type> instead")]]
 [[deprecated("use std::find_if and entt::meta_range<meta_type> instead")]]
 [[nodiscard]] meta_type resolve_if(Func func) ENTT_NOEXCEPT {
 [[nodiscard]] meta_type resolve_if(Func func) ENTT_NOEXCEPT {
     internal::meta_range range{*internal::meta_context::global()};
     internal::meta_range range{*internal::meta_context::global()};
-    return std::find_if(range.cbegin(), range.cend(), [&func](const auto &curr) { return func(meta_type{&curr}); }).operator->();
+    return std::find_if(range.begin(), range.end(), [&func](const auto &curr) { return func(meta_type{&curr}); }).operator->();
 }
 }
 
 
 
 
@@ -66,7 +66,7 @@ template<typename Func>
  */
  */
 [[nodiscard]] inline meta_type resolve_id(const id_type id) ENTT_NOEXCEPT {
 [[nodiscard]] inline meta_type resolve_id(const id_type id) ENTT_NOEXCEPT {
     internal::meta_range range{*internal::meta_context::global()};
     internal::meta_range range{*internal::meta_context::global()};
-    return std::find_if(range.cbegin(), range.cend(), [id](const auto &curr) { return curr.id == id; }).operator->();
+    return std::find_if(range.begin(), range.end(), [id](const auto &curr) { return curr.id == id; }).operator->();
 }
 }
 
 
 
 
@@ -77,7 +77,7 @@ template<typename Func>
  */
  */
 [[nodiscard]] inline meta_type resolve_type(const id_type id) ENTT_NOEXCEPT {
 [[nodiscard]] inline meta_type resolve_type(const id_type id) ENTT_NOEXCEPT {
     internal::meta_range range{*internal::meta_context::global()};
     internal::meta_range range{*internal::meta_context::global()};
-    return std::find_if(range.cbegin(), range.cend(), [id](const auto &curr) { return curr.type_id == id; }).operator->();
+    return std::find_if(range.begin(), range.end(), [id](const auto &curr) { return curr.type_id == id; }).operator->();
 }
 }