129 lines
2.5 KiB
C++
129 lines
2.5 KiB
C++
/*
|
|
* threads_actor.hpp
|
|
*
|
|
* Created on: Jun 11, 2021
|
|
* Author: erki
|
|
*/
|
|
|
|
#ifndef SKULLC_THREADS_ACTOR_HPP_
|
|
#define SKULLC_THREADS_ACTOR_HPP_
|
|
|
|
#include <cmsis_os.h>
|
|
#include <freertos_os2.h>
|
|
#include <queue.h>
|
|
|
|
#include <type_traits>
|
|
#include <variant>
|
|
|
|
#include <threads_actor_thread.hpp>
|
|
#include <threads_iactor.hpp>
|
|
#include <threads_signal.hpp>
|
|
|
|
namespace Threads
|
|
{
|
|
|
|
template<typename CRTP, typename... Ts>
|
|
class Actor : public IActor
|
|
{
|
|
public:
|
|
using value_type = std::variant<Ts...>;
|
|
|
|
static_assert(std::is_default_constructible_v<value_type>, "Ts must be default constructible.");
|
|
static_assert(std::is_trivially_copyable_v<value_type>, "Ts must be trivially copyable.");
|
|
|
|
Actor() : IActor(sizeof(value_type)),
|
|
signal_(this)
|
|
{}
|
|
|
|
~Actor() override {}
|
|
|
|
template<typename T>
|
|
struct Signal : Signallable<T>
|
|
{
|
|
using parent = Actor<CRTP, Ts...>;
|
|
|
|
static_assert(std::is_convertible_v<T, parent::value_type>, "value_type cannot be constructed from T.");
|
|
|
|
explicit Signal(parent* q)
|
|
: q_(q)
|
|
{}
|
|
|
|
Signal(const Signal&) = default;
|
|
Signal(Signal&&) = default;
|
|
Signal& operator=(const Signal&) = default;
|
|
Signal& operator=(Signal&&) = default;
|
|
|
|
void emit(const T& data) override
|
|
{
|
|
parent::value_type to_send = data;
|
|
q_->dispatchEvent(to_send);
|
|
}
|
|
|
|
BaseType_t emitFromIsr(const T& data) override
|
|
{
|
|
parent::value_type to_send = data;
|
|
return q_->dispatchEventFromIsr(to_send);
|
|
}
|
|
|
|
private:
|
|
parent* q_;
|
|
};
|
|
|
|
template<typename U>
|
|
Signal<U> getSignal()
|
|
{
|
|
static_assert(std::is_convertible_v<U, value_type>, "value_type cannot be constructed from U.");
|
|
|
|
return Signal<U>(this);
|
|
}
|
|
|
|
template<typename U>
|
|
Signal<U>* getAllocatedSignal()
|
|
{
|
|
static_assert(std::is_convertible_v<U, value_type>, "value_type cannot be constructed from U.");
|
|
|
|
return new Signal<U>(this);
|
|
}
|
|
|
|
Signallable<value_type>* getStaticSignal()
|
|
{
|
|
return &signal_;
|
|
}
|
|
|
|
void dispatchSignal(const char* data) final
|
|
{
|
|
dispatchImpl_(data);
|
|
}
|
|
|
|
private:
|
|
struct Visitor_
|
|
{
|
|
using parent = Actor<CRTP, Ts...>;
|
|
parent* q;
|
|
|
|
Visitor_(parent* q)
|
|
: q(q)
|
|
{}
|
|
|
|
template<typename U>
|
|
void operator()(const U& u)
|
|
{
|
|
CRTP* s = static_cast<CRTP*>(q);
|
|
s->onSignal(u);
|
|
}
|
|
};
|
|
|
|
void dispatchImpl_(const char* data)
|
|
{
|
|
const value_type* signal_data = reinterpret_cast<const value_type*>(data);
|
|
std::visit(Visitor_{this}, *signal_data);
|
|
}
|
|
|
|
Signal<value_type> signal_;
|
|
};
|
|
|
|
}// namespace Threads
|
|
|
|
|
|
#endif /* SKULLC_THREADS_ACTOR_HPP_ */
|