storage: emplace returns the newly created object

This commit is contained in:
Michele Caini
2020-10-14 19:44:17 +02:00
parent 102c9891a0
commit ddea97bc88
2 changed files with 4 additions and 3 deletions

View File

@@ -358,9 +358,10 @@ public:
* @tparam Args Types of arguments to use to construct the object.
* @param entt A valid entity identifier.
* @param args Parameters to use to construct an object for the entity.
* @return A reference to the newly created object.
*/
template<typename... Args>
void emplace(const entity_type entt, Args &&... args) {
object_type & emplace(const entity_type entt, Args &&... args) {
if constexpr(std::is_aggregate_v<object_type>) {
instances.push_back(Type{std::forward<Args>(args)...});
} else {
@@ -369,6 +370,7 @@ public:
// entity goes after component in case constructor throws
underlying_type::emplace(entt);
return instances.back();
}
/**

View File

@@ -159,8 +159,7 @@ TEST(Storage, AggregatesMustWork) {
TEST(Storage, TypesFromStandardTemplateLibraryMustWork) {
// see #37 - this test shouldn't crash, that's all
entt::storage<std::unordered_set<int>> pool;
pool.emplace(entt::entity{0});
pool.get(entt::entity{0}).insert(42);
pool.emplace(entt::entity{0}).insert(42);
pool.erase(entt::entity{0});
}