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

This commit is contained in:
Michele Caini
2025-07-21 12:51:38 +02:00
parent 9f10d32b73
commit 93dca93616
4 changed files with 23 additions and 16 deletions
+1 -2
View File
@@ -187,8 +187,7 @@ public:
* @return The newly assigned child process.
*/
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
View File
@@ -146,12 +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 This process scheduler.
* @return The newly assigned child process.
*/
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
View File
@@ -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());
+18 -9
View File
@@ -108,13 +108,20 @@ TEST(Scheduler, Then) {
entt::scheduler scheduler{};
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()) {
scheduler.update(0, &counter);
@@ -142,7 +149,9 @@ TEST(Scheduler, Functor) {
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()) {
scheduler.update(0);
@@ -158,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();
}));