11/* ******************************************************************************
2- * Copyright 2014 See AUTHORS file.
2+ * Copyright 2014, 2015 See AUTHORS file.
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
1414 * limitations under the License.
1515 ******************************************************************************/
1616
17- #ifndef ENTITY_HPP_
18- #define ENTITY_HPP_
17+ #ifndef ACPP_ENTITY_HPP_
18+ #define ACPP_ENTITY_HPP_
1919
2020#include < bitset>
2121#include < cstdint>
2222#include < memory>
2323#include < typeindex>
2424#include < unordered_map>
2525#include < vector>
26+ #include < type_traits>
2627
2728#include " Ashley/AshleyConstants.hpp"
2829#include " Ashley/core/Component.hpp"
2930#include " Ashley/core/ComponentType.hpp"
30- #include " Ashley/internal/ComponentOperations.hpp"
3131#include " Ashley/signals/Signal.hpp"
32+ #include " Ashley/internal/ComponentOperations.hpp"
33+ #include " Ashley/internal/Helper.hpp"
3234
3335namespace ashley {
3436
@@ -77,7 +79,19 @@ class Entity {
7779 * @param component the component to add.
7880 * @return this {@link Entity} for chaining.
7981 */
80- Entity &add (std::unique_ptr<Component> &&component);
82+ template <typename C> Entity &add (std::unique_ptr<C> &&component) {
83+ internal::verify_component_type<C>();
84+
85+ auto typeIndex = std::type_index (typeid (C));
86+
87+ if (operationHandler != nullptr ) {
88+ operationHandler->add (this , std::move (component), typeIndex);
89+ } else {
90+ addInternal (std::move (component), typeIndex);
91+ }
92+
93+ return *this ;
94+ }
8195
8296 /* *
8397 * <p>Constructs a new object of type C (subclassing ashley::Component) using <em>args...</em> for construction.</p>
@@ -87,15 +101,20 @@ class Entity {
87101 * @return This {@link Entity} for easy chaining
88102 */
89103 template <typename C, typename ...Args> Entity &add (Args&&... args) {
90- auto mapPtr = std::unique_ptr<Component>( new C (args...) );
104+ internal::verify_component_type<C>( );
91105
92- if (operationHandler != nullptr ) {
93- operationHandler->add (this , mapPtr);
106+ const auto typeIndex = std::type_index (typeid (C));
107+ auto component = std::unique_ptr<Component>(new C (args...));
108+
109+ if (operationHandler != nullptr ) {
110+ operationHandler->add (this , std::move (component), typeIndex);
94111 } else {
95- addInternal (mapPtr );
112+ addInternal (std::move (component), typeIndex );
96113 }
97114
98115 return *this ;
116+
117+ // return add<C>(std::unique_ptr<C>(new C(args...)));
99118 }
100119
101120 /* *
@@ -106,14 +125,6 @@ class Entity {
106125 */
107126 std::unique_ptr<Component> remove (const std::type_index typeIndex);
108127
109- /* *
110- * <p>Removes the given {@link Component}, if it's found attached to this {@link Entity}.
111- * @param component the {@link Component} to remove.
112- * @return the removed {@link Component}'s unique_ptr if the component was removed straight away; or nullptr if not
113- * found or if the removal has been delayed (e.g. when we're already in update() and need to wait to the end)
114- */
115- std::unique_ptr<Component> remove (Component * const component);
116-
117128 /* *
118129 * <p>Removes the {@link Component} of the specified type. Since there is only ever one component of one type, we
119130 * don't need an instance, just the type.</p>
@@ -160,6 +171,8 @@ class Entity {
160171 * no such component.
161172 */
162173 template <typename C> C* getComponent () {
174+ internal::verify_component_type<C>();
175+
163176 auto type = std::type_index (typeid (C));
164177 auto id = ashley::ComponentType::getIndexFor (type);
165178
@@ -255,7 +268,7 @@ class Entity {
255268
256269 ComponentOperationHandler *operationHandler = nullptr , *operationHandlerTemp = nullptr ;
257270
258- void addInternal (std::unique_ptr<Component> &component);
271+ void addInternal (std::unique_ptr<Component> && component, std::type_index type );
259272
260273 std::unique_ptr<Component> removeImpl (std::type_index typeIndex);
261274
0 commit comments