是否可以在 C++ Actor Framework 中对类型化 actor 使用继承?

Is it possible to use inheritance with typed actors in the C++ Actor Framework?

C++ Actor Framework 允许 actor 是强类型的。该框架是否也支持类型化角色的继承?

是 - typed_actor 实例可以被视为不同的 typed_actor 类型,只要新类型响应实例支持的消息子集。这是一个示例,其中 c_type/C 是 a_type 和 b_type 的超类型:

#include <iostream>
#include "caf/all.hpp"

using namespace caf;
using namespace std;

using a_type = typed_actor<replies_to<int>::with<void>>;
using b_type = typed_actor<replies_to<double>::with<void>>;
using c_type = a_type::extend<replies_to<double>::with<void>>;

class C : public c_type::base
{
protected:
    behavior_type make_behavior() override
    {
        return
        {
            [this](int value)
            {
                aout(this) << "Received integer value: " << value << endl;
            },
            [this](double value)
            {
                aout(this) << "Received double value: " << value << endl;
            },
            after(chrono::seconds(5)) >> [this]
            {
                aout(this) << "Exiting after 5s" << endl;
                this->quit();
            }
        };
    }
};

void testerA(const a_type &spawnedActor)
{
    scoped_actor self;
    self->send(spawnedActor, 5);
}

void testerB(const b_type &spawnedActor)
{
    scoped_actor self;
    self->send(spawnedActor, -5.01);
}

int main()
{
    auto spawnedActor = spawn<C>();
    testerA(spawnedActor);
    testerB(spawnedActor);
    await_all_actors_done();
}

注:详情请见example in the CAF 0.14.0 user manual showing how this works, but CAF 0.14.4 has removed the spawn_typed method that would make an inline creation/spawn of a typed_actor possible. See the corresponding GitHub issue