The C++ Actor Framework allows actors to be strongly typed. Does the framework also support inheritance with typed actors?
Yes - a typed_actor instance can be treated as a different typed_actor type as long as the new type responds to a subset of the messages the instance supports. Here's an example where c_type/C is a super type of both a_type and 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();
}
Note: there is an 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 for details.