classtk_1_1_actor.md 5.7 KB


title: tk::Actor summary: メッセージ駆動型タスク (アクター) の基底クラス


tk::Actor

メッセージ駆動型タスク (アクター) の基底クラス More...

#include <actor.hpp>

Public Functions

Protected Functions

Name
Actor() =default
virtual ~Actor()
bool start(uint32_t prio =PRIO_NORMAL)
Actor をタスクとして生成し、EventHub へ自動登録します
bool post(const MsgType & msg)
この Actor のメッセージキューへ直接メッセージを送信します (コピー渡し)
bool post(MsgType && msg)
この Actor のメッセージキューへ直接メッセージを送信します (ムーブ渡し)
template
std::enable_if< std::is_same< U, Message >::value, bool >::type
post(uint32_t id, uintptr_t arg =0)
[tk::Message]() 型の場合のコンビニエンス送信関数
TaskHandle handle() const
Name
virtual void on_start()
タスク起動時、イベントループ突入直前に1度だけ呼ばれる初期化関数
virtual void on_message(const MsgType & msg) =0
メッセージ受信時に呼ばれるハンドラ関数

Detailed Description

template <typename MsgType  =Message,
size_t QueueSize =8>
class tk::Actor;

メッセージ駆動型タスク (アクター) の基底クラス

Template Parameters:

  • MsgType 受信するメッセージの型 (デフォルト: tk::Message)
  • QueueSize メッセージキューの最大保持件数 (デフォルト: 8)

固定長リングバッファ付きのメッセージキューを持ち、メッセージ到着時に自動起床して [on_message()](Classes/classtk_1_1_actor.md#function-on-message) を実行します。メッセージが無い時は CPU を消費せずブロック待機します。

struct SensorMsg { float temp; };

class SensorActor : public tk::Actor<SensorMsg, 8> {
protected:
    void on_start() override {
        // デバイスの初期化
    }
    void on_message(const SensorMsg& msg) override {
        printf("Temp: %.1f\n", msg.temp);
    }
};

Public Functions Documentation

function Actor

Actor() =default

function ~Actor

inline virtual ~Actor()

function start

inline bool start(
    uint32_t prio =PRIO_NORMAL
)

Actor をタスクとして生成し、EventHub へ自動登録します

Parameters:

Return: true 登録成功, false 登録失敗 (タスク枠上限など)

[tk::start()](Namespaces/namespacetk.md#function-start) を呼び出す前に登録する必要があります。

function post

inline bool post(
    const MsgType & msg
)

この Actor のメッセージキューへ直接メッセージを送信します (コピー渡し)

Parameters:

  • msg 送信するメッセージ

Return: true 送信成功, false キューが満杯

スレッドセーフ・割り込みセーフ・マルチコアセーフです。

function post

inline bool post(
    MsgType && msg
)

この Actor のメッセージキューへ直接メッセージを送信します (ムーブ渡し)

Parameters:

  • msg 送信するメッセージ (ムーブ)

Return: true 送信成功, false キューが満杯

function post

template <typename U  =MsgType>
inline std::enable_if< std::is_same< U, Message >::value, bool >::type post(
    uint32_t id,
    uintptr_t arg =0
)

[tk::Message]() 型の場合のコンビニエンス送信関数

Parameters:

  • id メッセージID
  • arg 引数 (省略時: 0)

Return: true 送信成功, false キューが満杯

function handle

inline TaskHandle handle() const

この Actor のタスクハンドルを取得

Protected Functions Documentation

function on_start

inline virtual void on_start()

タスク起動時、イベントループ突入直前に1度だけ呼ばれる初期化関数

タスクコンテキスト内で実行されるため、[tk::sleep_ms()](Namespaces/namespacetk.md#function-sleep-ms) を使った初期化ウェイトも安全に行えます。 必要に応じて派生クラスでオーバーライドします。

function on_message

virtual void on_message(
    const MsgType & msg
) =0

メッセージ受信時に呼ばれるハンドラ関数

Parameters:

  • msg 受信したメッセージ

キューから取り出されたメッセージの実体への参照が渡されます。 派生クラスで必ず実装します。


Updated on 2026-10-02 at 14:01:32 +0900