Browse Source

process/scheduler: refine API - see #1257

Michele Caini 8 months ago
parent
commit
7942648035

+ 0 - 1
TODO

@@ -42,4 +42,3 @@ TODO:
 * scheduler: remove attach and then
 * process_adaptor: add watchdog to avoid circular calls, write more tests for plain funcs and the like
 * test shared_process_from
-* refine process API for attach/then/whatever, I don't quite like the current one

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

@@ -184,10 +184,10 @@ public:
     /**
      * @brief Assigns a child process to run in case of success.
      * @param child A child process to run in case of success.
-     * @return The newly assigned child process.
+     * @return A reference to the newly assigned child process.
      */
-    std::shared_ptr<basic_process> then(std::shared_ptr<basic_process> child) {
-        return (next = std::move(child));
+    basic_process &then(std::shared_ptr<basic_process> child){
+        return *(next = std::move(child));
     }
 
     /**

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

@@ -146,11 +146,11 @@ public:
      * @brief Schedules a process for the next tick.
      * @tparam Proc Type of process to schedule.
      * @param proc The actual process to schedule.
-     * @return The newly assigned child process.
+     * @return A reference to the newly assigned process.
      */
     template<typename Proc>
-    std::shared_ptr<type> attach(std::shared_ptr<Proc> proc) {
-        return handlers.first().emplace_back(std::move(proc));
+    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());
 
-    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());

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

@@ -110,18 +110,18 @@ TEST(Scheduler, Then) {
 
     // 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>());
+        .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>());
+        .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>());
+        .then(std::make_shared<succeeded_process>());
 
     while(!scheduler.empty()) {
         scheduler.update(0, &counter);
@@ -150,8 +150,8 @@ TEST(Scheduler, Functor) {
     };
 
     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(); }));
+        .then(entt::process_from(std::move(then)))
+        .then(entt::process_from([](entt::process &, std::uint32_t, void *) { FAIL(); }));
 
     while(!scheduler.empty()) {
         scheduler.update(0);
@@ -167,7 +167,7 @@ TEST(Scheduler, SpawningProcess) {
     std::pair<int, int> counter{};
 
     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();
     }));