CAF (c + + actor Framework) uses essays (using classes to build actors and some ideas for using them)

Source: Internet
Author: User

class-based actors
A class-based actor is a subtype of event_based_actor and must implement the pure virtual member function Make_behavior re Turning the initial behavior.

The exact words tell us two points: 1. "Event_based_actor" must be inherited.

2. The function of overloading Make_behavior is actually the constructor of this class, which defines the initial behavior.

About the 1th, that this class can use a lot of basic send, read a little bit of source code discovery Event_based_actor indirectly inherited a class called Localactor, about this class-based actor's usage can go inside to find, or very convenient.

On the 2nd, it is relatively simple to note that actors constructed with functions will pass in parameters, but this main_behavior is not a parameter.

The 2nd problem is that the actor who constructs the function sends itself a message that only needs self to be himself, as

  

Self-->send (self,"hello");

Now, I think one way is to declare a scoped_actor within a class and send it to the this pointer (which should be sent locally by pointers).

#include <string>#include<iostream>#include"caf/io/all.hpp"#include"caf/all.hpp"using namespacestd;using namespaceCAF;classSlavenodeactor: PublicEvent_based_actor { Public: Behavior Make_behavior ()Override{ scoped_actor A; A ->send (This,"hello"); return {        [=](Const string&str) {cout<< Str <<Endl;        Quit (); }, Caf::others>> [=] () {cout<<"unkown Message"<<Endl;  }     }; }};intMain () {Auto Slave_actor= caf::spawn<slavenodeactor>();  Caf::await_all_actors_done (); return 0;}

Then, the recent write code found such a usage, that is, the information that class A holds this actor, such as IP, port number, and the actor's own information, as a person needs a name, and Class B inherits Event_based_actor, as a friend of Class A to call a information, The constructor of B needs to pass in a pointer to Class A, and a is responsible for providing an external interface to control the actor's behavior.

Paste a picture

For the convenience of control, Class A (Slavenode best to the actor as their own member variable, otherwise it will be inconvenient to use, you know, you understand. )

CAF (c + + actor Framework) uses essays (using classes to build actors and some ideas for using them)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.