From: David Wicks Date: Wed, 14 Jan 2015 17:52:27 +0000 (-0500) Subject: Entity::replace method. X-Git-Url: https://code.bitgloo.com/?a=commitdiff_plain;h=0f642da244f97b009cfa0b310e6914aaf78c1314;p=clyne%2Fentityx.git 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. --- 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());