Browse Source

process: prepare for a more user-friendly attach/then API

Michele Caini 8 months ago
parent
commit
93dca93616

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

@@ -187,8 +187,7 @@ public:
      * @return The newly assigned child process.
      * @return The newly assigned child process.
      */
      */
     std::shared_ptr<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 this->shared_from_this();
+        return (next = std::move(child));
     }
     }
 
 
     /**
     /**

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

@@ -146,12 +146,11 @@ public:
      * @brief Schedules a process for the next tick.
      * @brief Schedules a process for the next tick.
      * @tparam Proc Type of process to schedule.
      * @tparam Proc Type of process to schedule.
      * @param proc The actual process to schedule.
      * @param proc The actual process to schedule.
-     * @return This process scheduler.
+     * @return The newly assigned child process.
      */
      */
     template<typename Proc>
     template<typename Proc>
-    basic_scheduler &attach(std::shared_ptr<Proc> proc) {
-        handlers.first().emplace_back(std::move(proc));
-        return *this;
+    std::shared_ptr<type> attach(std::shared_ptr<Proc> proc) {
+        return handlers.first().emplace_back(std::move(proc));
     }
     }
 
 
     /**
     /**

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

@@ -190,7 +190,7 @@ TEST(Process, ThenPeek) {
 
 
     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());
     ASSERT_TRUE(process->peek()->peek());
     ASSERT_TRUE(process->peek()->peek());

+ 18 - 9
test/entt/process/scheduler.cpp

@@ -108,13 +108,20 @@ TEST(Scheduler, Then) {
     entt::scheduler scheduler{};
     entt::scheduler scheduler{};
     std::pair<int, int> counter{};
     std::pair<int, int> counter{};
 
 
-    scheduler
-        // failing process with successor
-        .attach(std::make_shared<succeeded_process>()->then(std::make_shared<succeeded_process>()->then(std::make_shared<failed_process>()->then(std::make_shared<succeeded_process>()))))
-        // failing process without successor
-        .attach(std::make_shared<succeeded_process>()->then(std::make_shared<succeeded_process>()->then(std::make_shared<failed_process>())))
-        // non-failing process
-        .attach(std::make_shared<succeeded_process>()->then(std::make_shared<succeeded_process>()));
+    // failing process with successor
+    scheduler.attach(std::make_shared<succeeded_process>())
+        ->then(std::make_shared<succeeded_process>())
+        ->then(std::make_shared<failed_process>())
+        ->then(std::make_shared<succeeded_process>());
+
+    // failing process without successor
+    scheduler.attach(std::make_shared<succeeded_process>())
+        ->then(std::make_shared<succeeded_process>())
+        ->then(std::make_shared<failed_process>());
+
+    // non-failing process
+    scheduler.attach(std::make_shared<succeeded_process>())
+        ->then(std::make_shared<succeeded_process>());
 
 
     while(!scheduler.empty()) {
     while(!scheduler.empty()) {
         scheduler.update(0, &counter);
         scheduler.update(0, &counter);
@@ -142,7 +149,9 @@ TEST(Scheduler, Functor) {
         proc.fail();
         proc.fail();
     };
     };
 
 
-    scheduler.attach(entt::process_from(std::move(attach))->then(entt::process_from(std::move(then))->then(entt::process_from([](entt::process &, std::uint32_t, void *) { FAIL(); }))));
+    scheduler.attach(entt::process_from(std::move(attach)))
+        ->then(entt::process_from(std::move(then)))
+        ->then(entt::process_from([](entt::process &, std::uint32_t, void *) { FAIL(); }));
 
 
     while(!scheduler.empty()) {
     while(!scheduler.empty()) {
         scheduler.update(0);
         scheduler.update(0);
@@ -158,7 +167,7 @@ TEST(Scheduler, SpawningProcess) {
     std::pair<int, int> counter{};
     std::pair<int, int> counter{};
 
 
     scheduler.attach(entt::process_from([&scheduler](entt::process &proc, std::uint32_t, void *) {
     scheduler.attach(entt::process_from([&scheduler](entt::process &proc, std::uint32_t, void *) {
-        scheduler.attach(std::make_shared<succeeded_process>()->then(std::make_shared<failed_process>()));
+        scheduler.attach(std::make_shared<succeeded_process>())->then(std::make_shared<failed_process>());
         proc.succeed();
         proc.succeed();
     }));
     }));