process: prepare for a more user-friendly attach/then API
This commit is contained in:
@@ -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));
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -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));
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -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());
|
||||
|
||||
@@ -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();
|
||||
}));
|
||||
|
||||
|
||||
Reference in New Issue
Block a user