diff --git a/lib/include/srslte/common/inplace_task.h b/lib/include/srslte/common/move_callback.h similarity index 84% rename from lib/include/srslte/common/inplace_task.h rename to lib/include/srslte/common/move_callback.h index d7bf563dc..03f3faa1b 100644 --- a/lib/include/srslte/common/inplace_task.h +++ b/lib/include/srslte/common/move_callback.h @@ -19,8 +19,8 @@ * */ -#ifndef SRSLTE_INPLACE_TASK_H -#define SRSLTE_INPLACE_TASK_H +#ifndef SRSLTE_MOVE_CALLBACK_H +#define SRSLTE_MOVE_CALLBACK_H #include #include @@ -31,16 +31,18 @@ namespace srslte { constexpr size_t default_buffer_size = 32; template -class inplace_task; +class move_callback; namespace task_details { +//! Class used to type-erase the functor/lambda capture move/call/dtor operators template struct oper_table_t { using call_oper_t = R (*)(void* src, Args&&... args); using move_oper_t = void (*)(void* src, void* dest); using dtor_oper_t = void (*)(void* src); + //! Returns a operator table for when the move_function object is empty const static oper_table_t* get_empty() noexcept { const static oper_table_t t{true, @@ -50,6 +52,7 @@ struct oper_table_t { return &t; } + //! Returns a operator table for when the move_function fits the small buffer template const static oper_table_t* get_small() noexcept { @@ -64,6 +67,7 @@ struct oper_table_t { return &t; } + //! Returns a operator table for when the move_function fits the big buffer template const static oper_table_t* get_big() noexcept { @@ -101,7 +105,7 @@ private: template struct is_inplace_task : std::false_type {}; template -struct is_inplace_task > : std::true_type {}; +struct is_inplace_task > : std::true_type {}; template ::type> using enable_small_capture = @@ -112,17 +116,17 @@ using enable_big_capture = typename std::enable_if < Cap -class inplace_task +class move_callback { static constexpr size_t capacity = Capacity >= sizeof(void*) ? Capacity : sizeof(void*); using storage_t = typename std::aligned_storage::type; using oper_table_t = task_details::oper_table_t; public: - inplace_task() noexcept { oper_ptr = oper_table_t::get_empty(); } + move_callback() noexcept { oper_ptr = oper_table_t::get_empty(); } template = true> - inplace_task(T&& function) noexcept + move_callback(T&& function) noexcept { using FunT = typename std::decay::type; oper_ptr = oper_table_t::template get_small(); @@ -130,23 +134,23 @@ public: } template = true> - inplace_task(T&& function) + move_callback(T&& function) { using FunT = typename std::decay::type; oper_ptr = oper_table_t::template get_big(); ptr = static_cast(new FunT{std::forward(function)}); } - inplace_task(inplace_task&& other) noexcept + move_callback(move_callback&& other) noexcept { oper_ptr = other.oper_ptr; other.oper_ptr = oper_table_t::get_empty(); oper_ptr->move(&other.buffer, &buffer); } - ~inplace_task() { oper_ptr->dtor(&buffer); } + ~move_callback() { oper_ptr->dtor(&buffer); } - inplace_task& operator=(inplace_task&& other) noexcept + move_callback& operator=(move_callback&& other) noexcept { oper_ptr->dtor(&buffer); oper_ptr = other.oper_ptr; @@ -155,12 +159,12 @@ public: return *this; } - R operator()(Args&&... args) const { return oper_ptr->call(&buffer, std::forward(args)...); } + R operator()(Args&&... args) const noexcept { return oper_ptr->call(&buffer, std::forward(args)...); } bool is_empty() const { return oper_ptr == oper_table_t::get_empty(); } bool is_in_small_buffer() const { return oper_ptr->is_in_buffer; } - void swap(inplace_task& other) noexcept + void swap(move_callback& other) noexcept { if (this == &other) return; @@ -184,7 +188,7 @@ public: std::swap(oper_ptr, other.oper_ptr); } - friend void swap(inplace_task& lhs, inplace_task& rhs) noexcept { lhs.swap(rhs); } + friend void swap(move_callback& lhs, move_callback& rhs) noexcept { lhs.swap(rhs); } private: union { @@ -196,4 +200,4 @@ private: } // namespace srslte -#endif // SRSLTE_INPLACE_TASK_H +#endif // SRSLTE_MOVE_CALLBACK_H diff --git a/lib/include/srslte/common/multiqueue.h b/lib/include/srslte/common/multiqueue.h index edad196ad..47ab2869a 100644 --- a/lib/include/srslte/common/multiqueue.h +++ b/lib/include/srslte/common/multiqueue.h @@ -28,7 +28,7 @@ #ifndef SRSLTE_MULTIQUEUE_H #define SRSLTE_MULTIQUEUE_H -#include "inplace_task.h" +#include "move_callback.h" #include #include #include @@ -333,8 +333,7 @@ private: std::unique_ptr task_ptr; }; -// using move_task_t = move_function<>; -using move_task_t = inplace_task; +using move_task_t = move_callback; using task_multiqueue = multiqueue_handler; } // namespace srslte diff --git a/lib/test/common/queue_test.cc b/lib/test/common/queue_test.cc index 5f0f562a8..b126ad8e1 100644 --- a/lib/test/common/queue_test.cc +++ b/lib/test/common/queue_test.cc @@ -19,7 +19,7 @@ * */ -#include "srslte/common/inplace_task.h" +#include "srslte/common/move_callback.h" #include "srslte/common/multiqueue.h" #include "srslte/common/thread_pool.h" #include @@ -336,8 +336,8 @@ int test_inplace_task() auto l0 = [&v]() { v = 1; }; - srslte::inplace_task t{l0}; - srslte::inplace_task t2{[v]() mutable { v = 2; }}; + srslte::move_callback t{l0}; + srslte::move_callback t2{[v]() mutable { v = 2; }}; // sanity static checks static_assert(task_details::is_inplace_task::type>::value, "failed check\n"); static_assert(std::is_base_of::type> >::value, @@ -351,8 +351,8 @@ int test_inplace_task() t3(); TESTASSERT(v == 1); - C c; - srslte::inplace_task t4{std::bind([&v](C& c) { v = *c.val; }, std::move(c))}; + C c; + srslte::move_callback t4{std::bind([&v](C& c) { v = *c.val; }, std::move(c))}; { decltype(t4) t5; t5 = std::move(t4); @@ -360,10 +360,10 @@ int test_inplace_task() TESTASSERT(v == 5); } - D d; - srslte::inplace_task t6 = [&v, d]() { v = d.big_val[0]; }; + D d; + srslte::move_callback t6 = [&v, d]() { v = d.big_val[0]; }; { - srslte::inplace_task t7; + srslte::move_callback t7; t6(); TESTASSERT(v == 6); v = 0; @@ -392,7 +392,7 @@ int test_inplace_task() // TEST: task works in const contexts t = l2; - auto l3 = [](const srslte::inplace_task& task) { task(); }; + auto l3 = [](const srslte::move_callback& task) { task(); }; v = 0; l3(t); TESTASSERT(v == 6);