Просмотр исходного кода

process: then/release/peek for child processes

skypjack 9 месяцев назад
Родитель
Сommit
1d163ee113
2 измененных файлов с 43 добавлено и 1 удалено
  1. 27 1
      src/entt/process/process.hpp
  2. 16 0
      test/entt/process/process.cpp

+ 27 - 1
src/entt/process/process.hpp

@@ -83,7 +83,8 @@ public:
 
     /*! @brief Default constructor. */
     constexpr basic_process()
-        : current{state::idle} {}
+        : next{},
+          current{state::idle} {}
 
     /*! @brief Default destructor. */
     virtual ~basic_process() = default;
@@ -179,6 +180,30 @@ public:
         return current == state::rejected;
     }
 
+    /**
+     * @brief Assigns a child process to run in case of success.
+     * @param child A child process to run in case of success.
+     */
+    void then(std::shared_ptr<basic_process> child) {
+        next = std::move(child);
+    }
+
+    /**
+     * @brief Releases ownership of the child process, if any.
+     * @return The child process attached to the object, if any.
+     */
+    std::shared_ptr<basic_process> release() {
+        return std::move(next);
+    }
+
+    /**
+     * @brief Returns the child process without releasing ownership, if any.
+     * @return The child process attached to the object, if any.
+     */
+    std::shared_ptr<basic_process> peek() {
+        return next;
+    }
+
     /**
      * @brief Updates a process and its internal state if required.
      * @param delta Elapsed time.
@@ -217,6 +242,7 @@ public:
     }
 
 private:
+    std::shared_ptr<basic_process> next;
     state current;
 };
 

+ 16 - 0
test/entt/process/process.cpp

@@ -185,6 +185,22 @@ TEST(Process, AbortImmediately) {
     ASSERT_TRUE(process.aborted_invoked);
 }
 
+TEST(ProcessAdaptor, ThenReleasePeek) {
+    test_process<int> process{};
+
+    ASSERT_FALSE(process.peek());
+
+    process.then(std::make_shared<test_process<int>>());
+
+    ASSERT_TRUE(process.peek());
+    // peek does not release ownership
+    ASSERT_TRUE(process.peek());
+
+    auto other = process.release();
+
+    ASSERT_FALSE(process.peek());
+}
+
 TEST(ProcessAdaptor, Resolved) {
     bool updated = false;
     auto lambda = [&updated](std::uint32_t, void *, auto resolve, auto) {