CAF(C++ actor framework)(序列化之类,无需序列化,直接传)(二)
昨天讲了Struct,还是不够满意,毕竟C++里面类用的比较多嘛,那就先上个类,
这段代码是我稍微改编了一下的结果。都是最基本的用法。
#include <utility>
#include <iostream>
#include <vector>
#include "caf/all.hpp" using std::cout;
using std::endl;
using std::make_pair;
using std::vector;
using namespace caf;
class foo { private:
int a_;
vector<int> b_;
public: foo(int a0 = , vector<int> b0 = vector<int>()) : a_(a0), b_(b0) { } foo(const foo&) = default; foo& operator=(const foo&) = default; int a() const { return a_; } void set_a(int val) { a_ = val; } vector<int> b() const { return (vector<int>)b_; } void set_b(const vector<int> val){ b_ = val; }
}; bool operator==(const foo& lhs, const foo& rhs) {
return lhs.a() == rhs.a()
&& lhs.b() == rhs.b();
} void testee(event_based_actor* self) {
self->become (
[=](const foo& val) {
aout(self)<< val.a()<<endl;
auto b = val.b();
for (auto it = b.begin(); it != b.end(); ++it)
{
aout(self)<<*(it)<<endl;
}
self->quit();
}
);
} int main(int, char**) { //###################First method####################
announce<foo>("foo", make_pair(&foo::a, &foo::set_a),
make_pair(&foo::b, &foo::set_b)); //####################Second method####################
//a member function pointer to get an attribute of foo
using foo_getter = int (foo::*)() const;
using foo_getter1 = vector<int> (foo::*)() const;
// a member function pointer to set an attribute of foo
using foo_setter = void (foo::*)(int);
using foo_setter1 = void (foo::*)(const vector<int>); foo_getter g1 = &foo::a;
foo_setter s1 = &foo::set_a;
// same is true for b
foo_getter1 g2 = &foo::b;
foo_setter1 s2 = &foo::set_b;
announce<foo>("foo", make_pair(g1, s1), make_pair(g2, s2)); //####################Third method######################
// alternative syntax that uses casts instead of variables
// (returns false since foo is already announced)
announce<foo>("foo",
make_pair(static_cast<foo_getter>(&foo::a),
static_cast<foo_setter>(&foo::set_a)),
make_pair(static_cast<foo_getter1>(&foo::b),
static_cast<foo_setter1>(&foo::set_b))); {
scoped_actor self;
auto t = spawn(testee);
self->send(t, foo{,{,}});
}
await_all_actors_done();
shutdown();
return ;
}
贴上结果

CAF 告诉我们,你想在消息中传送一个类,你只要告诉它你所有成员的getter 和setter函数,然后呢它提供了三种方式,(一般就选第一种 没有任何区别的 = = )。
稍微再深入一些的是类里面再套类对象,其实也是很方便。只是多了一个嵌套。可以看github上announce4.cpp
https://github.com/actor-framework/actor-framework/blob/master/examples/type_system/announce_4.cpp
还有一个小东西值得一讲,using的用法
using foo_setter1 = void (foo::*)(const vector<int>);
就是声明一个函数指针,指向了foo类里的一个成员函数,参数为一个const 向量,返回值为void。
举一反三,map,pair 都是一样的。只要是C++ 标准的STL都是可以的。
最后还有一篇想讲一下自己对announce5的代码理解和改编吧。
---------------------------2016.4.4-------------------------------
试了一下枚举类型是不需要announce的 但是编译通过了,运行,接受不到后来把枚举再转化为int就好了。
CAF(C++ actor framework)(序列化之类,无需序列化,直接传)(二)的更多相关文章
- day71:drf:API接口&Restful API规范&Django Rest Framework&drf中的序列化和反序列化功能
目录 1.web应用模式 2.API接口 3.Restful API规范 4.序列化 5.Django Rest Framework 1.drf的简单介绍 2.drf的特点 3.如何安装drf 4.d ...
- Django REST framework 中文教程1:序列化
建立环境 在我们做任何事情之前,我们将使用virtualenv创建一个新的虚拟环境.这将确保我们的包配置与我们正在开展的任何其他项目保持良好的隔离. virtualenv envsource env/ ...
- Django rest framework(6)----序列化
目录 Django rest framework(1)----认证 Django rest framework(2)----权限 Django rest framework(3)----节流 Djan ...
- C#中的二进制序列化和Json序列化
序列化就是把一个对象变成流的形式,方便传输和还原.小弟不才,总结下对二进制序列化和Json序列化的使用: 1.首先,二进制序列化(BinaryFormatter)要求要序列化的类必须是可序列化的(即在 ...
- Django-Rest-Framework的序列化之serializers 序列化组件
Django-Rest-Framework的序列化之serializers 序列化组件 restful framework 正常的序列化 from django.http import HttpRes ...
- [LeetCode] Serialize and Deserialize BST 二叉搜索树的序列化和去序列化
Serialization is the process of converting a data structure or object into a sequence of bits so tha ...
- [LeetCode] Serialize and Deserialize Binary Tree 二叉树的序列化和去序列化
Serialization is the process of converting a data structure or object into a sequence of bits so tha ...
- java编解码技术,json序列化与二进制序列化
1.何为json序列化与二进制序列化 通常我们在程序中采用的以json为传输,将json转为对象的就是json序列化了.而二进制序列化通常是我们将数据转换为二进制进行传输,然后在进行各类转换操作 2. ...
- [LeetCode] Serialize and Deserialize N-ary Tree N叉搜索树的序列化和去序列化
Serialization is the process of converting a data structure or object into a sequence of bits so tha ...
- Effective Java 第三版—— 90.考虑序列化代理替代序列化实例
Tips 书中的源代码地址:https://github.com/jbloch/effective-java-3e-source-code 注意,书中的有些代码里方法是基于Java 9 API中的,所 ...
随机推荐
- setuptools的使用
1.什么是setuptools setuptoolssetuptools是 Python Enterprise Application Kit(PEAK)的一个副项目,是Python distutil ...
- delphi 设置超链接
的属性 的事件 的方法 //1设置链接类型//2获取样式 链接和提示信息 //title是提示信息//HTTPS https://<a href="https://www.baidu ...
- GoldenGate配置(一)之单向复制配置
GoldenGate配置(一)之单向复制配置 环境: Item Source System Target System Platform Red Hat Enterprise Linux Server ...
- SQL用法总结
1.创建数据库语句 create table persons( 'id' INT NOT NULL AUTO_INCREMENT, ) NOT NULL, ) NOT NULL, PRIMARY KE ...
- 网络IPC:套接字
网络进程间通信(network IPC):不同计算机(通过网络相连)上运行的进程相互通信的机制. 套接字网络IPC接口:进程能够使用该接口和其他进程通信.通过该接口,其他进程运行位置是透明的,它们可以 ...
- hdu 3849 (双联通求桥)
一道简单的双联通求桥的题目,,数据时字符串,,map用的不熟练啊,,,,,,,,,,,,, #include <iostream> #include <cstring> #in ...
- Fliptile
开关题 尺度法 Fliptile Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 3394 Accepted: ...
- 杂乱无章之javascript(二)
1.浏览器与事件事件通常是由浏览器所产生,不同的浏览器会产生的事件也有所不同,即使同一浏览器不同版本所产生的事件也有不同.以下为HTML4.01中的事件 2.error事件:它可以调用一个错误处理函数 ...
- Android 高级UI设计笔记03:使用ListView实现左右滑动删除Item
1. 这里就是实现一个很简单的功能,使用ListView实现左右滑动删除Item: (1)当我们在ListView的某个Item,向左滑动显示一个删除按钮,用户点击按钮,即可以删除该项item,并且有 ...
- jquery 页面跳转 表单提交
$("#button").click(function () { $("#form").first().attr("action ...