diff --git a/Threads/Inc/threads_actor.hpp b/Threads/Inc/threads_actor.hpp index 0d7bc08..121a09a 100644 --- a/Threads/Inc/threads_actor.hpp +++ b/Threads/Inc/threads_actor.hpp @@ -17,6 +17,7 @@ #include "threads_primitivethread.hpp" #include "threads_signal.hpp" +#include "threads_timer.hpp" namespace Threads { @@ -36,14 +37,53 @@ public: (*dd)(); }, this, name, priority, stack_size), - msg_queue_(xQueueCreate(queue_size, sizeof(value_type))), signal_(*this) + msg_queue_(xQueueCreate(queue_size, sizeof(value_type))), signal_(this) { assert(msg_queue_); } virtual ~Actor() {} - Signallable* getSignal() + template + struct Signal : Signallable + { + using parent = Actor; + + static_assert(std::is_convertible_v, "value_type cannot be constructed from T."); + + explicit Signal(parent* q) + : q_(q) + {} + + bool emit(const T& data) override + { + parent::value_type to_send = data; + const BaseType_t success = xQueueSend(q_->msg_queue_, &to_send, 0); + return success == pdTRUE; + } + + std::pair emitFromIsr(const T& data) override + { + parent::value_type to_send = data; + BaseType_t was_awoken = pdFALSE; + const BaseType_t success = xQueueSendFromISR(q_->msg_queue_, &to_send, &was_awoken); + + return {success == pdTRUE, was_awoken}; + } + + private: + parent* q_; + }; + + template + Signal getSignal() + { + static_assert(std::is_convertible_v, "value_type cannot be constructed from U."); + + return Signal(this); + } + + Signallable* getStaticSignal() { return &signal_; } @@ -89,33 +129,7 @@ private: } } - friend struct Signal_; - - struct Signal_ : Signallable - { - using parent = Actor; - parent& q; - - explicit Signal_(parent& q) - : q(q) - {} - - bool emit(const value_type& data) override - { - const BaseType_t success = xQueueSend(q.msg_queue_, &data, 0); - return success == pdTRUE; - } - - std::pair emitFromIsr(const value_type& data) override - { - BaseType_t was_awoken = pdFALSE; - const BaseType_t success = xQueueSendFromISR(q.msg_queue_, &data, &was_awoken); - - return {success == pdTRUE, was_awoken}; - } - }; - - Signal_ signal_; + Signal signal_; }; template