process: basic_process vs process

This commit is contained in:
skypjack
2025-06-18 12:22:41 +02:00
parent db6c6573af
commit c80b634497
6 changed files with 45 additions and 45 deletions

3
TODO
View File

@@ -37,6 +37,5 @@ TODO:
* use any for meta_custom_node
* avoid copying meta_type/data/func nodes
* paged vector as a standalone class
* basic_process vs process with default delta type
* basic_process_adaptor vs process_adaptor with default delta type
* update process/scheduler natvis and doc after rework
* process test with allocator_arg

View File

@@ -7,13 +7,16 @@
namespace entt {
template<typename>
class process;
class basic_process;
template<typename = std::uint32_t, typename = std::allocator<void>>
/*! @brief Alias declaration for the most common use case. */
using process = basic_process<std::uint32_t>;
template<typename, typename = std::allocator<void>>
class basic_scheduler;
/*! @brief Alias declaration for the most common use case. */
using scheduler = basic_scheduler<>;
using scheduler = basic_scheduler<std::uint32_t>;
} // namespace entt

View File

@@ -57,9 +57,9 @@ namespace entt {
* @tparam Delta Type to use to provide elapsed time.
*/
template<typename Delta>
class process: private std::enable_shared_from_this<process<Delta>> {
class basic_process: private std::enable_shared_from_this<basic_process<Delta>> {
class process_arg_t {
friend class process<Delta>;
friend class basic_process<Delta>;
explicit process_arg_t() = default;
};
@@ -89,29 +89,29 @@ public:
using delta_type = Delta;
/*! @brief Default constructor. */
constexpr process(token_type)
constexpr basic_process(token_type)
: current{state::idle} {}
/*! @brief Default destructor. */
virtual ~process() = default;
virtual ~basic_process() = default;
/*! @brief Default copy constructor. */
process(const process &) = default;
basic_process(const basic_process &) = default;
/*! @brief Default move constructor. */
process(process &&) noexcept = default;
basic_process(basic_process &&) noexcept = default;
/**
* @brief Default copy assignment operator.
* @return This process.
*/
process &operator=(const process &) = default;
basic_process &operator=(const basic_process &) = default;
/**
* @brief Default move assignment operator.
* @return This process.
*/
process &operator=(process &&) noexcept = default;
basic_process &operator=(basic_process &&) noexcept = default;
/**
* @brief Factory method.
@@ -288,11 +288,11 @@ private:
* @tparam Delta Type to use to provide elapsed time.
*/
template<typename Func, typename Delta>
struct process_adaptor: public process<Delta>, private Func {
struct process_adaptor: public basic_process<Delta>, private Func {
/*! @brief Process constructor token. */
using token_type = typename process<Delta>::token_type;
using token_type = typename basic_process<Delta>::token_type;
/*! @brief Type used to provide elapsed time. */
using delta_type = typename process<Delta>::delta_type;
using delta_type = typename basic_process<Delta>::delta_type;
/**
* @brief Constructs a process adaptor from a lambda or a functor.
@@ -302,7 +302,7 @@ struct process_adaptor: public process<Delta>, private Func {
*/
template<typename... Args>
process_adaptor(const token_type token, Args &&...args)
: process<Delta>{token},
: basic_process<Delta>{token},
Func{std::forward<Args>(args)...} {}
/**

View File

@@ -16,11 +16,11 @@ namespace entt {
/*! @cond TURN_OFF_DOXYGEN */
namespace internal {
template<typename Delta>
template<typename Type>
struct process_handler {
// std::shared_ptr because of its type erased allocator which is useful here
std::shared_ptr<process_handler> next{};
std::shared_ptr<process<Delta>> task{};
std::shared_ptr<Type> task{};
};
} // namespace internal
@@ -55,8 +55,9 @@ struct process_handler {
*/
template<typename Delta, typename Allocator>
class basic_scheduler {
using handler_type = internal::process_handler<Delta>;
using process_type = basic_process<Delta>;
using alloc_traits = std::allocator_traits<Allocator>;
using handler_type = internal::process_handler<process_type>;
using container_allocator = typename alloc_traits::template rebind_alloc<handler_type>;
using container_type = std::vector<handler_type, container_allocator>;
@@ -201,8 +202,7 @@ public:
*/
template<typename Proc, typename... Args>
basic_scheduler &attach(Args &&...args) {
static_assert(std::is_base_of_v<process<Delta>, Proc>, "Invalid process type");
handlers.first().emplace_back().task = process<Delta>::allocate<Proc>(handlers.second(), std::forward<Args>(args)...);
handlers.first().emplace_back().task = process_type::template allocate<Proc>(handlers.second(), std::forward<Args>(args)...);
return *this;
}
@@ -272,12 +272,11 @@ public:
*/
template<typename Proc, typename... Args>
basic_scheduler &then(Args &&...args) {
static_assert(std::is_base_of_v<process<Delta>, Proc>, "Invalid process type");
ENTT_ASSERT(!handlers.first().empty(), "Process not available");
auto *curr = &handlers.first().back();
for(; curr->next; curr = curr->next.get()) {}
curr->next = std::allocate_shared<handler_type>(handlers.second());
curr->next->task = process<Delta>::allocate<Proc>(handlers.second(), std::forward<Args>(args)...);
curr->next->task = process_type::template allocate<Proc>(handlers.second(), std::forward<Args>(args)...);
return *this;
}

View File

@@ -5,8 +5,8 @@
#include "../../common/empty.h"
template<typename Delta>
class test_process: public entt::process<Delta> {
using entt::process<Delta>::process;
class test_process: public entt::basic_process<Delta> {
using entt::basic_process<Delta>::basic_process;
void succeeded() override {
succeeded_invoked = true;
@@ -36,7 +36,7 @@ public:
};
TEST(Process, Basics) {
auto process = entt::process<int>::allocate<test_process<int>>(std::allocator<void>{});
auto process = entt::basic_process<int>::allocate<test_process<int>>(std::allocator<void>{});
ASSERT_FALSE(process->alive());
ASSERT_FALSE(process->finished());
@@ -91,7 +91,7 @@ TEST(Process, Basics) {
}
TEST(Process, Succeeded) {
auto process = entt::process<test::empty>::allocate<test_process<test::empty>>(std::allocator<void>{});
auto process = entt::basic_process<test::empty>::allocate<test_process<test::empty>>(std::allocator<void>{});
process->tick({});
process->tick({});
@@ -110,7 +110,7 @@ TEST(Process, Succeeded) {
}
TEST(Process, Fail) {
auto process = entt::process<int>::allocate<test_process<int>>(std::allocator<void>{});
auto process = entt::basic_process<int>::allocate<test_process<int>>(std::allocator<void>{});
process->tick(0);
process->tick(0);
@@ -129,7 +129,7 @@ TEST(Process, Fail) {
}
TEST(Process, Data) {
auto process = entt::process<test::empty>::allocate<test_process<test::empty>>(std::allocator<void>{});
auto process = entt::basic_process<test::empty>::allocate<test_process<test::empty>>(std::allocator<void>{});
int value = 0;
process->tick({});
@@ -150,7 +150,7 @@ TEST(Process, Data) {
}
TEST(Process, AbortNextTick) {
auto process = entt::process<int>::allocate<test_process<int>>(std::allocator<void>{});
auto process = entt::basic_process<int>::allocate<test_process<int>>(std::allocator<void>{});
process->tick(0);
process->abort();
@@ -168,7 +168,7 @@ TEST(Process, AbortNextTick) {
}
TEST(Process, AbortImmediately) {
auto process = entt::process<test::empty>::allocate<test_process<test::empty>>(std::allocator<void>{});
auto process = entt::basic_process<test::empty>::allocate<test_process<test::empty>>(std::allocator<void>{});
process->tick({});
process->abort(true);
@@ -192,7 +192,7 @@ TEST(ProcessAdaptor, Resolved) {
resolve();
};
auto process = entt::process<std::uint64_t>::allocate<entt::process_adaptor<decltype(lambda), std::uint64_t>>(std::allocator<void>{}, lambda);
auto process = entt::basic_process<std::uint64_t>::allocate<entt::process_adaptor<decltype(lambda), std::uint64_t>>(std::allocator<void>{}, lambda);
process->tick(0);
process->tick(0);
@@ -209,7 +209,7 @@ TEST(ProcessAdaptor, Rejected) {
rejected();
};
auto process = entt::process<std::uint64_t>::allocate<entt::process_adaptor<decltype(lambda), std::uint64_t>>(std::allocator<void>{}, lambda);
auto process = entt::basic_process<std::uint64_t>::allocate<entt::process_adaptor<decltype(lambda), std::uint64_t>>(std::allocator<void>{}, lambda);
process->tick(0);
process->tick(0);
@@ -225,7 +225,7 @@ TEST(ProcessAdaptor, Data) {
resolve();
};
auto process = entt::process<std::uint64_t>::allocate<entt::process_adaptor<decltype(lambda), std::uint64_t>>(std::allocator<void>{}, lambda);
auto process = entt::basic_process<std::uint64_t>::allocate<entt::process_adaptor<decltype(lambda), std::uint64_t>>(std::allocator<void>{}, lambda);
process->tick(0, &value);

View File

@@ -5,9 +5,8 @@
#include <entt/process/process.hpp>
#include <entt/process/scheduler.hpp>
class foo_process: public entt::process<entt::scheduler::delta_type> {
using base_type = entt::process<entt::scheduler::delta_type>;
using token_type = typename base_type::token_type;
class foo_process: public entt::process {
using token_type = typename entt::process::token_type;
void update(const delta_type, void *) override {
on_update();
@@ -19,7 +18,7 @@ class foo_process: public entt::process<entt::scheduler::delta_type> {
public:
foo_process(const token_type token, std::function<void()> upd, std::function<void()> abort)
: base_type{token},
: entt::process{token},
on_update{std::move(upd)},
on_aborted{std::move(abort)} {}
@@ -28,24 +27,24 @@ private:
std::function<void()> on_aborted;
};
class succeeded_process: public entt::process<entt::scheduler::delta_type> {
void update(const entt::scheduler::delta_type, void *data) override {
class succeeded_process: public entt::process {
void update(const delta_type, void *data) override {
++static_cast<std::pair<int, int> *>(data)->first;
succeed();
}
public:
using entt::process<entt::scheduler::delta_type>::process;
using entt::process::process;
};
class failed_process: public entt::process<entt::scheduler::delta_type> {
void update(const entt::scheduler::delta_type, void *data) override {
class failed_process: public entt::process {
void update(const delta_type, void *data) override {
++static_cast<std::pair<int, int> *>(data)->second;
fail();
}
public:
using entt::process<entt::scheduler::delta_type>::process;
using entt::process::process;
};
TEST(Scheduler, Functionalities) {