CAF(C++ actor framework)使用随笔(projection 用法)(一)
最近干活在写毕设,用到了CAF,看了文档,发现了一些小坑,自己摸索写点随笔。(CAF的github网站 https://github.com/actor-framework/actor-framework)里面的example文件夹例子不错。
但是感觉其实实际使用还是会有很多没讲到。
概念的东西可以看http://www.wfuyu.com/mvc/21278.html
我学习就看着http://www.actor-framework.org/manual/看下去看着不太会的就写例子,第一个坑就是3.5节的projection。

因为这个例子上的option是不对的,应该是optional(害我愣了一会~)
个人理解projection的意思就是 比如一个对象的回调函数期望获得一个int类型的参数传进来,但是别人给了它一个string类型的,那么projection机制就会先把他捕获,若它能够变为int就变成int被进入case1,如果不行,没关系,再用case2来捕获它就好了,毕竟人家传进来就不一定是要给case1用的。所以这个投影过程如果没成功也不是一个错误,只是说它匹配失败了。
下面贴上自己的代码
#include <iostream>
#include "caf/all.hpp"
#include "caf/io/all.hpp" using namespace std;
using namespace caf; auto intproj = [](const string& str)-> optional<int> {
char* endptr = nullptr;
int result = static_cast<int>(strtol(str.c_str(), &endptr, ));
if (endptr != nullptr && *endptr == '\0')
return result;
return {};
}; message_handler fun(event_based_actor* self){
return {
on(intproj) >> [self](int i) {
// case 1: successfully converted a string
aout(self)<<"projection successfully\n";
return;
},
[self](const string& str) {
// case 2: str is not an integer
aout(self)<<"not projection \n";
return;
},
after(std::chrono::seconds())>>[self]{
aout(self)<<"after 2 seconds fun self is quit\n";
self->quit();
}
};
} void send_message(const actor& actor1){
scoped_actor self;
self->send(actor1,"");
self->send(actor1,"a");
}
int main(){
auto actor1 = spawn(fun);
send_message(actor1); caf::await_all_actors_done();
shutdown();
return 0;
}
结果为

message_handler 和behavior 一样属于一种行为可以来生成一个actor。
CAF支持两种生成对象的方法,这里使用了最简单的以函数作为参数。
之前测试直接在main函数里用scoped_actor,那么程序就会永远等待,因为scoped_actor在函数结尾才会被quit,而
caf::await_all_actors_done();要等到所有此cpp文件中创建的actor都quit了才能继续执行下面的代码。
后来我又发现其实没必要把scoped_actor 另外搞一个函数 只要用{}把scoped_actor 包括起来就好。
就像
{
scoped_actor self;
self->send(actor1,"1");
self->send(actor1,"a");
}
在括号结束后self也会自动调用quit函数.
自己后来完了一下CAF序列,感觉功能非常的强大,直接传指针,C++中的类都不需要序列化。下次记录一下!
CAF(C++ actor framework)使用随笔(projection 用法)(一)的更多相关文章
- CAF(C++ actor framework)使用随笔(unbecome与keep_behavior用法)
看usermanual(使用随笔一里面有)看到差不多一半的时候,这个keep_behavior与unbeacome的结合引起了我的注意.(这是为什么呢?) 因为它的示例代码写的太简单了!我真的没看太懂 ...
- CAF(C++ actor framework)使用随笔(使用类去构建actor和使用的一些思路)
Class-based actorsA class-based actor is a subtype of event_based_actor and must implement the pure ...
- CAF(C++ actor framework)使用随笔(延迟发送,消息转发,消息优先级)(四)
e). 消息延迟发送(和前面没太大区别直接上代码) #include <iostream> #include "caf/all.hpp" #include " ...
- CAF(C++ actor framework)使用随笔(同步发送 异步与同步等待)(三)
c). 同步发送, 等待响应, 超时后收到1个系统消息. 贴上代码 #include <iostream> #include "caf/all.hpp" #includ ...
- CAF(C++ actor framework)使用随笔(send sync_send)(二)
a). 发完就忘, 就像上面anon_send 以及send #include <iostream> #include "caf/all.hpp" #include & ...
- CAF(C++ actor framework)(序列化之类,无需序列化,直接传)(二)
昨天讲了Struct,还是不够满意,毕竟C++里面类用的比较多嘛,那就先上个类, 这段代码是我稍微改编了一下的结果.都是最基本的用法. #include <utility> #includ ...
- CAF(C++ actor framework)(序列化之结构体,任意嵌套STL)(一)
User-Defined Data Types in Messages(用户自定义类型)All user-defined types must be explicitly “announced” so ...
- CAF(C++ actor framework)(序列化之复杂类,分析 还有自己不懂的细思恐极函数实现)(三)
这里应该是序列化的最后一篇.感觉自己写的不是很好,也一点点在学习.这次就不贴上代码了.代码在github上的announce5.cpp.代码简单,但是分析下去会有细思恐极的感觉! 先看一下几个函数是干 ...
- Robot Framework 自动化测试 Selenium2Library 库 用法
Robot Framework自动化测试Selenium2Library库详细用法 一.浏览器驱动 通过不同的浏览器执行脚本. Open Browser Htpp://www.xxx.com ...
随机推荐
- 让asp.net web api同时支持[AcceptVerbs("GET","POST")]
在使用第三方接口时,有时候会看到接口同时支持GET和POST,当时想想webapi有AcceptVerbs特性,没有细想便想当然肯定会支持,后来项目中需要用到,当时在没有参数传入下确实支持,直到早几天 ...
- Oracle DECODE函数的语法介绍
Oracle DECODE函数功能很强,下面就为您详细介绍Oracle DECODE函数的用法,希望可以让您对Oracle DECODE函数有更多的了解. Oracle DECODE函数 Oracle ...
- mysql slow log分析工具的比较
mysql 中的 slow log 是用来记录执行时间较长(超过 long_query_time 秒)的 sql 的一种日志工具. 启用 slow log 在 my.cnf 中设置 [mysqld] ...
- 教你50招提升ASP.NET性能(十一):避免在调试模式下运行网站
(17)Avoid running sites in debug mode 招数17: 避免在调试模式下运行网站 When it comes to ASP.NET, one of the most c ...
- OpenResty
- 金蝶KIS 13.0专业版破解方法破解安装流程 金蝶KIS 13.0专业版安装流程
金蝶KIS 13.0安装 1.先安装操作系统Windows server 2008 R2. 2.再安装SQL2008 R2. 3.再安装金蝶KIS 13.0专业版. 在安装时记住须要将系列号设置成为1 ...
- 高级Bash脚本编程指南
http://www.cnblogs.com/rollenholt/archive/2012/04/20/2458763.html
- java_Cookies_1_商品浏览历史记录servlet2
public class CookiesServlet2 extends HttpServlet { // 显示商品详细信息 public void doGet(HttpServletRequest ...
- 使用TortoiseGit对Git版本进行分支操作
版本克隆分支问题 TortoiseGit在克隆分支的时候,默认克隆master分支,克隆后本地工作目录为中心器的Master分支. 克隆后本地分支 中心库分支 Push分支到中心服务器(Pushing ...
- C# 工厂模式示例
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace 工厂模式 ...