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

时间:2015-12-10 17:16:33

标签: c++ inheritance c++-actor-framework

C ++ Actor Framework允许演员进行强类型化。框架是否也支持对类型化actor的继承?

1 个答案:

答案 0 :(得分:1)

是 - 只要新类型响应实例支持的消息子集,就可以将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();
}

注意:CAF 0.14.0用户手册中有example显示其工作原理,但CAF 0.14.4已删除了spawn_typed方法,该方法可以使typed_actor的内联创建/生成成为可能。有关详细信息,请参阅相应的GitHub issue