Browse Source

process: enable_shared_from_this was actually useful here

skypjack 8 months ago
parent
commit
716c67d3a5
2 changed files with 10 additions and 10 deletions
  1. 3 3
      src/entt/process/process.hpp
  2. 7 7
      test/entt/process/process.cpp

+ 3 - 3
src/entt/process/process.hpp

@@ -57,7 +57,7 @@ namespace entt {
  * @tparam Delta Type to use to provide elapsed time.
  */
 template<typename Delta>
-class basic_process {
+class basic_process: public std::enable_shared_from_this<basic_process<Delta>> {
     enum class state : std::uint8_t {
         idle = 0,
         running,
@@ -185,9 +185,9 @@ public:
      * @param child A child process to run in case of success.
      * @return The newly assigned child process.
      */
-    basic_process& then(std::shared_ptr<basic_process> child) {
+    std::shared_ptr<basic_process> then(std::shared_ptr<basic_process> child) {
         next = std::move(child);
-        return *next;
+        return shared_from_this();
     }
 
     /**

+ 7 - 7
test/entt/process/process.cpp

@@ -186,17 +186,17 @@ TEST(Process, AbortImmediately) {
 }
 
 TEST(ProcessAdaptor, ThenPeek) {
-    test_process<int> process{};
+    auto process = std::make_shared<test_process<int>>();
 
-    ASSERT_FALSE(process.peek());
+    ASSERT_FALSE(process->peek());
 
-    process.then(std::make_shared<test_process<int>>()).then(std::make_shared<test_process<int>>());
+    process->then(std::make_shared<test_process<int>>()->then(std::make_shared<test_process<int>>()));
 
-    ASSERT_TRUE(process.peek());
-    ASSERT_TRUE(process.peek()->peek());
-    ASSERT_FALSE(process.peek()->peek()->peek());
+    ASSERT_TRUE(process->peek());
+    ASSERT_TRUE(process->peek()->peek());
+    ASSERT_FALSE(process->peek()->peek()->peek());
     // peek does not release ownership
-    ASSERT_TRUE(process.peek());
+    ASSERT_TRUE(process->peek());
 }
 
 TEST(ProcessAdaptor, Resolved) {