From 0f642da244f97b009cfa0b310e6914aaf78c1314 Mon Sep 17 00:00:00 2001 From: David Wicks Date: Wed, 14 Jan 2015 12:52:27 -0500 Subject: Entity::replace method. Avoid the client-side song and dance around assignment in cases where a component of the same type may already be assigned to the entity. --- entityx/Entity.h | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/entityx/Entity.h b/entityx/Entity.h index fa88726..85033c9 100644 --- a/entityx/Entity.h +++ b/entityx/Entity.h @@ -135,6 +135,9 @@ public: template ComponentHandle assign_from_copy(const C &component); + template + ComponentHandle replace(Args && ... args); + template void remove(); @@ -865,6 +868,19 @@ ComponentHandle Entity::assign_from_copy(const C &component) { return manager_->assign(id_, std::forward(component)); } +template +ComponentHandle Entity::replace(Args && ... args) { + assert(valid()); + auto handle = component(); + if (handle) { + *(handle.get()) = C(std::forward(args) ...); + } + else { + handle = manager_->assign(id_, std::forward(args) ...); + } + return handle; +} + template void Entity::remove() { assert(valid() && has_component()); -- cgit v1.2.3