Selaa lähdekoodia

storage: emplace returns the newly created object

Michele Caini 5 vuotta sitten
vanhempi
commit
ddea97bc88
2 muutettua tiedostoa jossa 4 lisäystä ja 3 poistoa
  1. 3 1
      src/entt/entity/storage.hpp
  2. 1 2
      test/entt/entity/storage.cpp

+ 3 - 1
src/entt/entity/storage.hpp

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

+ 1 - 2
test/entt/entity/storage.cpp

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