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 键盘框架IQKeyboardManager使用
框架地址:https://github.com/hackiftekhar/IQKeyboardManager AppDelegate.m文件中 #import <IQKeyboardMana ...
- windows7下硬盘安装ubuntu14.04
windows7 ubuntu1404双系统 准备软件 安装步骤 step 1 step 2 step 3 step 4 windows7 + ubuntu14.04双系统 准备软件 1)grub4d ...
- kvm上安装xp
主要为了看看图像显示是否有问题,跑起来系能如何,网络连接.文件共享是怎样的. 用的是雨林木风xp sp3的iso.为了提高性能,决定使用qcow2格式,预分配metadata,cache=none(查 ...
- Android在MediaMuxer和MediaCodec用例 - audio+video
在Android多媒体类,MediaMuxer和MediaCodec这是一个相对年轻,他们是JB 4.1和JB 4.3据介绍. 前者被用来产生一个混合的音频和视频的多媒体文件.的缺点是,现在可以只支持 ...
- WebView的应用 持续积累
在我的项目中载入网页时我们会用到WebView这个控件,关于这个控件的相关的比較有用的API在这里记录一下. 第一 webview 设置javascript可用, mWebView = (WebVi ...
- Supervised Learning-Regression
假设我们有一张房子属性及其价格之间的关系表(如下图所示) ,根据这些数据如何估计其他房子的价格?我们的第一个反应肯定是参考属性相似的房子的价格.在属性较少时这个方法还行得通,属性太复杂时就不那么简单了 ...
- c语言实例
#include <stdio.h> int main() { ; ; i=max(j,k); printf("i=%d\n",i); ; } int max(int ...
- iOS之设置状态栏颜色
状态栏的字体为黑色:UIStatusBarStyleDefault 状态栏的字体为白色:UIStatusBarStyleLightContent 一.在info.plist中,将View contro ...
- logstash-input-file 参数说明
参数 close_older close_older: # This has different implications depending on if a file is being tailed ...
- Leetcode 102. Binary Tree Level Order Traversal(二叉树的层序遍历)
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...