From 682f6b23925bf0c7e184ee4d83da918e07abf78e Mon Sep 17 00:00:00 2001 From: skypjack Date: Mon, 19 Jan 2026 13:56:39 +0100 Subject: [PATCH] entity: constrain iterator types --- src/entt/entity/group.hpp | 3 ++- src/entt/entity/mixin.hpp | 7 ++++--- src/entt/entity/registry.hpp | 15 ++++++++------- src/entt/entity/snapshot.hpp | 3 ++- src/entt/entity/sparse_set.hpp | 9 +++++---- src/entt/entity/storage.hpp | 9 +++++---- 6 files changed, 26 insertions(+), 20 deletions(-) diff --git a/src/entt/entity/group.hpp b/src/entt/entity/group.hpp index 9be090188..319000f67 100644 --- a/src/entt/entity/group.hpp +++ b/src/entt/entity/group.hpp @@ -2,6 +2,7 @@ #define ENTT_ENTITY_GROUP_HPP #include +#include #include #include #include @@ -642,7 +643,7 @@ public: * @param first An iterator to the first element of the range of entities. * @param last An iterator past the last element of the range of entities. */ - template + template void sort_as(It first, It last) const { if(*this) { descriptor->handle().sort_as(first, last); diff --git a/src/entt/entity/mixin.hpp b/src/entt/entity/mixin.hpp index 80cf20242..baa0a40a9 100644 --- a/src/entt/entity/mixin.hpp +++ b/src/entt/entity/mixin.hpp @@ -1,6 +1,7 @@ #ifndef ENTT_ENTITY_MIXIN_HPP #define ENTT_ENTITY_MIXIN_HPP +#include #include #include #include "../config/config.h" @@ -309,11 +310,11 @@ public: /** * @brief Assigns each element in a range an identifier. - * @tparam It Type of mutable forward iterator. + * @tparam It Type of output iterator. * @param first An iterator to the first element of the range to generate. * @param last An iterator past the last element of the range to generate. */ - template + template It> void generate(It first, It last) { underlying_type::generate(first, last); @@ -361,7 +362,7 @@ public: * @param last An iterator past the last element of the range of entities. * @param args Parameters to use to forward to the underlying storage. */ - template + template void insert(It first, It last, Args &&...args) { auto from = underlying_type::size(); underlying_type::insert(first, last, std::forward(args)...); diff --git a/src/entt/entity/registry.hpp b/src/entt/entity/registry.hpp index 364c3f1a5..a015479d9 100644 --- a/src/entt/entity/registry.hpp +++ b/src/entt/entity/registry.hpp @@ -4,6 +4,7 @@ #include #include #include +#include #include #include #include @@ -485,11 +486,11 @@ public: * * @sa create * - * @tparam It Type of forward iterator. + * @tparam It Type of output iterator. * @param first An iterator to the first element of the range to generate. * @param last An iterator past the last element of the range to generate. */ - template + template It> void create(It first, It last) { entities.generate(std::move(first), std::move(last)); } @@ -540,7 +541,7 @@ public: * @param first An iterator to the first element of the range of entities. * @param last An iterator past the last element of the range of entities. */ - template + template void destroy(It first, It last) { const auto to = entities.sort_as(first, last); const auto from = entities.cend() - static_cast(entities.free_list()); @@ -583,7 +584,7 @@ public: * @param first An iterator to the first element of the range of entities. * @param last An iterator past the last element of the range of entities. */ - template + template void insert(It first, It last) { ENTT_ASSERT(std::all_of(first, last, [this](const auto entt) { return valid(entt); }), "Invalid entity"); assure().insert(std::move(first), std::move(last)); @@ -600,7 +601,7 @@ public: * @param last An iterator past the last element of the range of entities. * @param value An instance of the element to assign. */ - template + template void insert(It first, It last, const Type &value) { ENTT_ASSERT(std::all_of(first, last, [this](const auto entt) { return valid(entt); }), "Invalid entity"); assure().insert(std::move(first), std::move(last), value); @@ -711,7 +712,7 @@ public: * @param last An iterator past the last element of the range of entities. * @return The number of elements actually removed. */ - template + template size_type remove(It first, It last) { size_type count{}; @@ -764,7 +765,7 @@ public: * @param first An iterator to the first element of the range of entities. * @param last An iterator past the last element of the range of entities. */ - template + template void erase(It first, It last) { if constexpr(std::is_same_v) { std::array cpools{static_cast(&assure()), static_cast(&assure())...}; diff --git a/src/entt/entity/snapshot.hpp b/src/entt/entity/snapshot.hpp index 35f350edd..f50e12c67 100644 --- a/src/entt/entity/snapshot.hpp +++ b/src/entt/entity/snapshot.hpp @@ -1,6 +1,7 @@ #ifndef ENTT_ENTITY_SNAPSHOT_HPP #define ENTT_ENTITY_SNAPSHOT_HPP +#include #include #include #include @@ -136,7 +137,7 @@ public: * @param id Optional name used to map the storage within the registry. * @return An object of this type to continue creating the snapshot. */ - template + template const basic_snapshot &get(Archive &archive, It first, It last, const id_type id = type_hash::value()) const { static_assert(!std::is_same_v, "Entity types not supported"); diff --git a/src/entt/entity/sparse_set.hpp b/src/entt/entity/sparse_set.hpp index aed858e91..d2b01ef5e 100644 --- a/src/entt/entity/sparse_set.hpp +++ b/src/entt/entity/sparse_set.hpp @@ -2,6 +2,7 @@ #define ENTT_ENTITY_SPARSE_SET_HPP #include +#include #include #include #include @@ -791,7 +792,7 @@ public: * @return Iterator pointing to the first element inserted in case of * success, the `end()` iterator otherwise. */ - template + template iterator push(It first, It last) { auto curr = end(); @@ -843,7 +844,7 @@ public: * @param first An iterator to the first element of the range of entities. * @param last An iterator past the last element of the range of entities. */ - template + template void erase(It first, It last) { if constexpr(std::is_same_v) { pop(first, last); @@ -870,7 +871,7 @@ public: * @param last An iterator past the last element of the range of entities. * @return The number of entities actually removed. */ - template + template size_type remove(It first, It last) { size_type count{}; @@ -1029,7 +1030,7 @@ public: * @param last An iterator past the last element of the range of entities. * @return An iterator past the last of the elements actually shared. */ - template + template iterator sort_as(It first, It last) { ENTT_ASSERT((mode != deletion_policy::in_place) || (head == max_size), "Sorting with tombstones not allowed"); const size_type len = (mode == deletion_policy::swap_only) ? head : packed.size(); diff --git a/src/entt/entity/storage.hpp b/src/entt/entity/storage.hpp index 0bfdd21de..e34dd9dca 100644 --- a/src/entt/entity/storage.hpp +++ b/src/entt/entity/storage.hpp @@ -2,6 +2,7 @@ #define ENTT_ENTITY_STORAGE_HPP #include +#include #include #include #include @@ -693,7 +694,7 @@ public: * @param value An instance of the object to construct. * @return Iterator pointing to the first element inserted, if any. */ - template + template iterator insert(It first, It last, const value_type &value = {}) { for(; first != last; ++first) { emplace_element(*first, true, value); @@ -901,7 +902,7 @@ public: * @param first An iterator to the first element of the range of entities. * @param last An iterator past the last element of the range of entities. */ - template + template void insert(It first, It last) { for(; first != last; ++first) { base_type::try_emplace(*first, true); @@ -1131,11 +1132,11 @@ public: /** * @brief Assigns each element in a range an identifier. - * @tparam It Type of mutable forward iterator. + * @tparam It Type of output iterator. * @param first An iterator to the first element of the range to generate. * @param last An iterator past the last element of the range to generate. */ - template + template It> void generate(It first, It last) { for(const auto sz = base_type::size(); first != last && base_type::free_list() != sz; ++first) { *first = *base_type::try_emplace(base_type::data()[base_type::free_list()], true);