Skip to content

Commit

Permalink
Merge branch 'master' into snapshot
Browse files Browse the repository at this point in the history
  • Loading branch information
skypjack committed Mar 2, 2018
2 parents 566e98c + b3df46d commit e7cfd6e
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
40 changes: 37 additions & 3 deletions src/entt/entity/sparse_set.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,40 @@ class SparseSet<Entity, Type>: public SparseSet<Entity> {
/**
* @brief Assigns an entity to a sparse set and constructs its object.
*
* @note
* _Sfinae'd_ function.<br/>
* This version is used for types that can be constructed in place directly.
* It doesn't work well with aggregates because of the placement new usually
* performed under the hood during an _emplace back_.
*
* @warning
* Attempting to use an entity that already belongs to the sparse set
* results in undefined behavior.<br/>
* An assertion will abort the execution at runtime in debug mode if the
* sparse set already contains the given entity.
*
* @tparam Args Types of arguments to use to construct the object.
* @param entity A valid entity identifier.
* @param args Parameters to use to construct an object for the entity.
* @return The object associated to the entity.
*/
template<typename... Args>
std::enable_if_t<std::is_constructible<Type, Args...>::value, object_type &>
construct(entity_type entity, Args&&... args) {
underlying_type::construct(entity);
instances.emplace_back(std::forward<Args>(args)...);
return instances.back();
}

/**
* @brief Assigns an entity to a sparse set and constructs its object.
*
* @note
* _Sfinae'd_ function.<br/>
* Fallback for aggregates and types in general that do not work well with a
* placement new as performed usually under the hood during an
* _emplace back_.
*
* @warning
* Attempting to use an entity that already belongs to the sparse set
* results in undefined behavior.<br/>
Expand All @@ -513,10 +547,10 @@ class SparseSet<Entity, Type>: public SparseSet<Entity> {
* @return The object associated to the entity.
*/
template<typename... Args>
object_type & construct(entity_type entity, Args&&... args) {
std::enable_if_t<!std::is_constructible<Type, Args...>::value, object_type &>
construct(entity_type entity, Args&&... args) {
underlying_type::construct(entity);
// emplace_back doesn't work well with PODs because of its placement new
instances.push_back(object_type{ std::forward<Args>(args)... });
instances.emplace_back(Type{ std::forward<Args>(args)... });
return instances.back();
}

Expand Down
1 change: 1 addition & 0 deletions test/entt/entity/registry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ TEST(DefaultRegistry, Orphans) {
registry.create();
registry.create<int>();
registry.create();
registry.attach<double>(registry.create());

registry.orphans([&](auto) { ++tot; });
ASSERT_EQ(tot, 2u);
Expand Down

0 comments on commit e7cfd6e

Please sign in to comment.