是否可以在C++Actor框架中对类型化的Actor使用继承

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

本文关键字:类型化 Actor 继承 C++Actor 框架 是否      更新时间:2023-10-16

C++Actor框架允许参与者是强类型的。该框架是否也支持类型化参与者的继承?

是-只要新类型响应实例支持的消息子集,typed_actor实例就可以被视为不同的typed_actors类型。这里有一个例子,其中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();
}

注意:CAF 0.14.0用户手册中有一个例子显示了这是如何工作的,但CAF 0.14.4删除了spawn_typed方法,该方法可以内联创建/派生typed_actor。有关详细信息,请参阅相应的GitHub问题。