diff options
author | Alec Thomas <alec@swapoff.org> | 2015-01-15 11:26:09 +1100 |
---|---|---|
committer | Alec Thomas <alec@swapoff.org> | 2015-01-15 11:26:09 +1100 |
commit | 21376ab730cb707f1bb20dfbc3533a2117c80b48 (patch) | |
tree | 6f0d347b4844717be9705a4fa2809b8dd56e67a6 | |
parent | d0c902c6167f965749b720d0c7ab9557187ec422 (diff) | |
parent | 0f642da244f97b009cfa0b310e6914aaf78c1314 (diff) |
Merge pull request #79 from sansumbrella/component_replace
Entity::replace<Component> method.
-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>()); |