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 ...
随机推荐
- iOS动画详解(二)
UIImage常用的绘图操作 一个UIImage对象提供了向当前上下文绘制自身的方法.我们现在已经知道如何获取一个图片类型的上下文并将它转变成当前上下文. 平移操作:下面的代码展示了如何将UI ...
- eclipse项目导入到Android Studio Plugin with id 'android-library' not found
在主项目的build.gradle 中加入以下代码buildscript { repositories { mavenCentral() } dependencies { classpath 'com ...
- cocos2d-x CCSpriteBatchNode
转自:http://www.cnblogs.com/jiackyan/archive/2013/04/14/3019880.html 1.先说下渲染批次:这是游戏引擎中一个比较重要的优化指标,指的是一 ...
- cocos2d-x 基本数学
转自:http://cjhworld.blog.163.com/blog/static/207078036201331510141222/ 数学函数: ccp(x, y); // 以坐标x,y创建一个 ...
- python爬虫之採集——360联想词W2版本号
http://blog.csdn.net/recsysml/article/details/30541197,我的这个博文介绍了对应的简单的方法做一个联想词的爬虫,并且还承诺了下面优化: 下一版本号的 ...
- [013]函数重载--int*和void*的匹配优先级
同事去面试的时候的问题: 测试一下发现:在同时存在int*和void*的重载函数时,vs2010的环境下,优先匹配void* #include<iostream> using namesp ...
- Moving From Objective-C to C++
1. virtual method or not: It's better to declare all cpp member methods without "virtual" ...
- iOS 并发概念浅析
在进行iOS开发过程中,我们常会遇到网络请求.复杂计算.数据存取等比较耗时的操作,如果处理不合理,将对APP的流畅度产生较大影响.除了优化APP架构,并发(concurrency)是一个常用且较好的解 ...
- 16% off MPPS V16 ECU tuning tool for EDC15 EDC16 EDC17
EOBD2.FR is offering 16% discount off the latest MPPS V16 ECU chip tuning tool. The device is now so ...
- vim高亮显示
在当期用户的主目录下创建文件.vimrc,打开编辑内容(~/.vimrc): filetype on syntax on