Skip to content

Instantly share code, notes, and snippets.

View JohnMurray's full-sized avatar

John Murray JohnMurray

View GitHub Profile

Nim Learning List

template<std::derived_from<Actor> RefOf>
class ActorRef { /* ...
template<typename RefOf>
class ActorRef {
std::shared_ptr<RefOf> m_actor;
public:
ActorRef(std::shared_ptr<RefOf> actor)
: m_actor(actor) {}
template<typename Message>
void send(Message&& msg) { /* TODO */ }
}
int main() {
std::unique_ptr<Actor> actor = std::make_unique<HelloActor>("Howdy");
actor->enqueue([&actor]() {
// how do I get it to the correct receiver?
dynamic_cast<HelloBehavior*>(actor.get())->receive("neighbor");
});
actor->process_message();
}
class Actor {
std::deque<std::function<void()>> m_messages;
public:
virtual ~Actor() = default;
void enqueue(std::function<void()> const& func) {
m_messages.push_back(func);
}
class HelloActor
: Actor<std::string>
, HelloBehavior { ... }
template<typename ... Messages>
class Actor {
std::vector<std::variant<Messages ...>> m_messages;
};
class HelloBehavior {
public:
virtual void receive(std::string const& msg) = 0;
};
class HelloActor
: public Actor
, public HelloBehavior {
std::string m_greeting;
class Actor {
public:
virtual ~Actor() = default;
};
class Behavior {};
class HelloActor : public Actor {
std::string m_greeting;
public:
HelloActor(std::string const& greeting): m_greeting(greeting){};
void receive(std::string const& msg) {
std::cout << m_greeting << " " << msg << "\n";
}
};