faster accommodate

This commit is contained in:
Michele Caini
2018-04-22 13:32:38 +02:00
parent 7157e7e77d
commit 67858bf300
2 changed files with 10 additions and 5 deletions

View File

@@ -462,8 +462,8 @@ velocity.dx = 0.;
velocity.dy = 0.;
```
Note that `accommodate` is mostly syntactic sugar for the following `if`/`else`
statement and nothing more:
Note that `accommodate` is a slightly faster alternative for the following
`if/else` statement and nothing more:
```cpp
if(registry.has<Comp>(entity)) {

View File

@@ -808,6 +808,8 @@ public:
* }
* @endcode
*
* Prefer this function anyway because it has slightly better performance.
*
* @warning
* Attempting to use an invalid entity results in undefined behavior.<br/>
* An assertion will abort the execution at runtime in debug mode in case of
@@ -821,9 +823,12 @@ public:
*/
template<typename Component, typename... Args>
Component & accommodate(entity_type entity, Args &&... args) {
return (has<Component>(entity)
? replace<Component>(entity, std::forward<Args>(args)...)
: assign<Component>(entity, std::forward<Args>(args)...));
assure<Component>();
auto &cpool = pool<Component>();
return (cpool.has(entity)
? cpool.get(entity) = Component{std::forward<Args>(args)...}
: cpool.construct(entity, std::forward<Args>(args)...));
}
/**