diff options
author | David Wicks <david@sosolimited.com> | 2015-01-14 12:52:27 -0500 |
---|---|---|
committer | David Wicks <david@sosolimited.com> | 2015-01-14 12:58:44 -0500 |
commit | 0f642da244f97b009cfa0b310e6914aaf78c1314 (patch) | |
tree | 6f0d347b4844717be9705a4fa2809b8dd56e67a6 | |
parent | d0c902c6167f965749b720d0c7ab9557187ec422 (diff) |
Entity::replace<Component> 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.
-rw-r--r-- | entityx/Entity.h | 16 |
1 files changed, 16 insertions, 0 deletions
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 <typename C> ComponentHandle<C> assign_from_copy(const C &component); + template <typename C, typename ... Args> + ComponentHandle<C> replace(Args && ... args); + template <typename C> void remove(); @@ -865,6 +868,19 @@ ComponentHandle<C> Entity::assign_from_copy(const C &component) { return manager_->assign<C>(id_, std::forward<const C &>(component)); } +template <typename C, typename ... Args> +ComponentHandle<C> Entity::replace(Args && ... args) { + assert(valid()); + auto handle = component<C>(); + if (handle) { + *(handle.get()) = C(std::forward<Args>(args) ...); + } + else { + handle = manager_->assign<C>(id_, std::forward<Args>(args) ...); + } + return handle; +} + template <typename C> void Entity::remove() { assert(valid() && has_component<C>()); |