registry: stable assign (close #386)

This commit is contained in:
Michele Caini
2020-01-09 22:06:33 +01:00
parent b352815cf8
commit 5904941361
2 changed files with 18 additions and 6 deletions

View File

@@ -68,15 +68,14 @@ class basic_registry {
template<typename... Args>
decltype(auto) assign(basic_registry &owner, const Entity entt, Args &&... args) {
decltype(auto) component = storage<entity_type, Component>::construct(entt, std::forward<Args>(args)...);
construction.publish(entt, owner, component);
return component;
construction.publish(entt, owner, this->construct(entt, std::forward<Args>(args)...));
return this->get(entt);
}
template<typename It, typename... Args>
std::enable_if_t<std::is_same_v<typename std::iterator_traits<It>::value_type, Entity>, typename storage<Entity, Component>::reverse_iterator_type>
assign(basic_registry &owner, It first, It last, Args &&... args) {
auto it = storage<entity_type, Component>::construct(first, last, std::forward<Args>(args)...);
auto it = this->construct(first, last, std::forward<Args>(args)...);
const auto end = it + (!construction.empty() * std::distance(first, last));
std::for_each(it, end, [this, &owner, &first](decltype(*it) component) {
@@ -88,14 +87,14 @@ class basic_registry {
void remove(basic_registry &owner, const Entity entt) {
destruction.publish(entt, owner);
storage<entity_type, Component>::destroy(entt);
this->destroy(entt);
}
template<typename... Args>
decltype(auto) replace(basic_registry &owner, const Entity entt, Args &&... args) {
Component component{std::forward<Args>(args)...};
update.publish(entt, owner, component);
return (storage<entity_type, Component>::get(entt) = std::move(component));
return (this->get(entt) = std::move(component));
}
private:

View File

@@ -13,6 +13,11 @@
struct empty_type {};
struct listener {
template<typename Component>
static void sort(entt::entity, entt::registry &registry) {
registry.sort<Component>([](auto lhs, auto rhs) { return lhs < rhs; });
}
template<typename Component>
void incr(entt::entity entity, entt::registry &registry, const Component &) {
ASSERT_TRUE(registry.valid(entity));
@@ -1588,3 +1593,11 @@ TEST(Registry, Dependencies) {
ASSERT_TRUE(registry.has<int>(entity));
ASSERT_FALSE(registry.has<double>(entity));
}
TEST(Registry, StableAssign) {
entt::registry registry;
registry.on_construct<int>().connect<&listener::sort<int>>();
registry.assign<int>(registry.create(), 0);
ASSERT_EQ(registry.assign<int>(registry.create(), 1), 1);
}