CAF (c + + actor Framework) using essays (projection usage) (i)

Source: Internet
Author: User

Recently work in writing, using the CAF, read the document, found some small pits, their own groping to write some essays. The example folder in the Https://github.com/actor-framework/actor-framework (CAF GitHub site) is a good example.

But the actual use of the feeling is still a lot of not mentioned.

The concept of something can be seen http://www.wfuyu.com/mvc/21278.html

I learn to look at http://www.actor-framework.org/manual/look at the not very good to write an example, the first pit is 3.5 knots of the projection.

Because the option on this example is wrong, it should be optional (I froze for a while)

Personal understanding projection means that an object's callback function expects to get a parameter of type int, but when someone gives it a string type, the projection mechanism catches him first. If it can become an int into an int is entered CASE1, if not, it is OK, then use CASE2 to capture it, after all, people pass in is not necessarily to case1 use. So the projection process, if not successful, is not a mistake, just that it failed to match.

Put your own code underneath.

#include <iostream>#include"caf/all.hpp"#include"caf/io/all.hpp"using namespacestd;using namespaceCaf;auto intproj= [](Const string& str)-optional<int> {    Char* Endptr =nullptr; intresult = static_cast<int> (Strtol (Str.c_str (), &endptr,Ten)); if(Endptr! = nullptr && *endptr = =' /')         returnresult; return {};}; Message_handler Fun (Event_based_actor*Self ) {    return{on (intproj)>> [Self] (inti) {//Case 1:successfully converted a stringAout (self) <<"projection successfully\n"; return; }, [Self] (Const string&str) {        //Case 2:STR isn't an integerAout (self) <<"Not projection \ n"; return; }, after (Std::chrono::seconds (2)) >>[self]{aout (self)<<"After 2 seconds quit\n"; Self-quit (); }    };}voidSend_message (Constactor&Actor1)    {Scoped_actor self; Self->send (Actor1,"1"); Self->send (Actor1,"a");}intMain () {Auto Actor1=spawn (fun);        Send_message (Actor1);    Caf::await_all_actors_done (); Shutdown ();}

Result is

Message_handler and behavior belong to a behavior that can generate an actor.

The CAF supports two methods of generating objects, using the simplest function as a parameter.

Before the test is directly in the main function with Scoped_actor, then the program will always wait, because scoped_actor at the end of the function will be quit, and

Caf::await_all_actors_done (); Wait until all actors created in this CPP file quit to continue executing the following code.

CAF (c + + actor Framework) using essays (projection usage) (i)

Related Article

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.