CAF(C++ actor framework)(序列化之结构体,任意嵌套STL)(一)
User-Defined Data Types in Messages(用户自定义类型)
All user-defined types must be explicitly “announced” so that CAF can (de)serialize them correctly.
之前干活,一开始不知道CAF自带序列化,都用boost库来做序列化,就是变string 类型发送,发现很多STL有些搞搞比较麻烦,发现诶?CAF居然比boost库好使!
那么就来搞一下看看.
先看一个例子(也是usermanual 里唯一的一个例子,呵呵呵~)其他的例子在github官网里https://github.com/actor-framework/actor-framework/tree/master/examples/type_system (就五个收益很大)

没看错就是那么简单的使用,announce函数。第一个参数是一个string那么之后就是他的所有成员。怎么实现我也不是很懂,上图

大致就是TS 就是参数的类型,可以是可变长度,然后检查他们的类型,我第一看到Is_pod 查了一下(pod类型 是plain old data)就是完全兼容C语言的编程的。(涨姿势了~)
还有uniform_type_info是CAF自己的一个关于类型什么的(没深究,只知道与RTTI有关)。还有一个重要的地方就是你必须写明你要发送的结构体的比较函数 ==(下面代码上有)
进入正题。(announce1.cpp)代码有一点点小长但是信息量很大。
// POD struct
struct foo {
std::vector<int> a;
int b;
}; // announce requires foo to have the equal operator implemented
bool operator==(const foo& lhs, const foo& rhs) {
return lhs.a == rhs.a && lhs.b == rhs.b;
} // a pair of two ints
using foo_pair = std::pair<int, int>; // another pair of two ints
using foo_pair2 = std::pair<int, int>; // a struct with member vector<vector<...>>
struct foo2 {
int a;
vector<vector<double>> b;
}; bool operator==(const foo2& lhs, const foo2& rhs) {
return lhs.a == rhs.a && lhs.b == rhs.b;
} // receives `remaining` messages
void testee(event_based_actor* self, size_t remaining) {
auto set_next_behavior = [=] {
if (remaining > ) testee(self, remaining - );
else self->quit();
};
self->become (
// note: we sent a foo_pair2, but match on foo_pair
// that's safe because both are aliases for std::pair<int, int>
[=](const foo_pair& val) {
cout << "foo_pair("
<< val.first << ", "
<< val.second << ")"
<< endl;
set_next_behavior();
},
[=](const foo& val) {
cout << "foo({";
auto i = val.a.begin();
auto end = val.a.end();
if (i != end) {
cout << *i;
while (++i != end) {
cout << ", " << *i;
}
}
cout << "}, " << val.b << ")" << endl;
set_next_behavior();
}
);
} int main(int, char**) {
// announces foo to the libcaf type system;
// the function expects member pointers to all elements of foo
announce<foo>("foo", &foo::a, &foo::b);
// announce foo2 to the libcaf type system,
// note that recursive containers are managed automatically by libcaf
announce<foo2>("foo2", &foo2::a, &foo2::b);
// serialization can throw if types are not announced properly
try {
// init some test data
foo2 vd;
vd.a = ;
vd.b.resize();
vd.b.back().push_back();
// serialize test data
vector<char> buf;
binary_serializer bs(std::back_inserter(buf));
bs << vd;
// deserialize written test data from buffer
binary_deserializer bd(buf.data(), buf.size());
foo2 vd2;
uniform_typeid<foo2>()->deserialize(&vd2, &bd);
// deserialized data must be equal to original input
assert(vd == vd2);
// announce std::pair<int, int> to the type system
announce<foo_pair>("foo_pair", &foo_pair::first, &foo_pair::second);
// libcaf returns the same uniform_type_info
// instance for the type aliases foo_pair and foo_pair2
assert(uniform_typeid<foo_pair>() == uniform_typeid<foo_pair2>());
}
catch (std::exception& e) {
cerr << "error during type (de)serialization: " << e.what() << endl;
return -;
}
// spawn a testee that receives two messages of user-defined type
auto t = spawn(testee, size_t{});
{ // lifetime scope of self
scoped_actor self;
// send t a foo
self->send(t, foo{std::vector<int>{, , , }, });
// send t a foo_pair2
self->send(t, foo_pair2{, });
}
await_all_actors_done();
shutdown();
}
一开始看,就是声明了两种结构体。foo 和foo2,foo2里面有vector<vector<double>> b(其实这里就告诉我们,它不但支持STL,还支持嵌套,而且我亲测pair,map都是可以的。其他应该也没问题吧。)
然后testee里定义了接受两种类型的消息一种是<int,int>(不管别名),一种是结构体foo 是的没看错,都不用序列化了,直接传(// note that recursive containers are managed automatically by libcaf)。
真心方便,然后是main函数里,使用了二进制去序列化类,再使用反序列化,整个过程就像用读文件非常的方便(注意捕获异常)。那么在最后的scoped_actor send也直接把类传过去非常的方便。
为了证明好用,支持remote actor我写了一个很难看的代码。
#include <vector>
#include <iostream>
#include "caf/all.hpp"
#include "caf/io/all.hpp"
using std::cout;
using std::endl;
using std::vector;
using std::map;
using std::pair;
using namespace caf; struct foo {
std::vector<vector<map<int,pair<int,int>>>> a;
int b;
};
bool operator==(const foo& lhs, const foo& rhs) {
return lhs.a == rhs.a && lhs.b == rhs.b;
}
// receives `remaining` messages
void testee(event_based_actor* self) {
self->become (
[=](const foo& val) {
aout(self)<<"get it"<<endl;
}
);
}
int main(int, char**) {
// announce<foo2>("foo2", &foo2::a, &foo2::b);
announce<foo>("foo", &foo::a, &foo::b);
auto actor = spawn(testee);
caf::io::publish(actor,);
{ // lifetime scope of self
scoped_actor self;
auto remoter = caf::io::remote_actor("localhost", );
self->send(remoter, foo{std::vector<vector<map<int,pair<int,int>>>>{},,});
}
await_all_actors_done();
shutdown();
}
结果为

不得不服还是很方便的!
码字不容易,求粉丝~互粉呀~
CAF(C++ actor framework)(序列化之结构体,任意嵌套STL)(一)的更多相关文章
- c、c++ 结构体的嵌套
c.c++ 结构体的嵌套 /************************************************************************/ /* 嵌套结构体 * C ...
- (60) 结构体指针、结构体变量嵌套、结构体指针嵌套、函数指针、数组指针、指针数组、typedef 综合运用
#include<stdio.h> #include<iostream> #include<malloc.h> /* author : 吴永聪 program: 结 ...
- C++结构体与Delphi结构体相互传参,结构体中包含结构体的嵌套,数组指针
//结构体的声明 typedef struct Mwinddirectbaseline { char* p; int s; int i; }Mwinddirectbaseline; typedef s ...
- C语言 结构体(嵌套结构体--结构体数组)
//结构体--嵌套结构体和结构体数组 #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <stdlib.h> ...
- golang中结构体的嵌套、方法的继承、方法的重写
package main import "fmt" type human struct { name, phone string age int8 } type student s ...
- Go语言基础之结构体
Go语言基础之结构体 Go语言中没有“类”的概念,也不支持“类”的继承等面向对象的概念.Go语言中通过结构体的内嵌再配合接口比面向对象具有更高的扩展性和灵活性. 类型别名和自定义类型 自定义类型 在G ...
- Go语言 - 结构体 | 方法
自定义类型和类型别名 自定义类型 在Go语言中有一些基本的数据类型,如string.整型.浮点型.布尔等数据类型, Go语言中可以使用type关键字来定义自定义类型. 自定义类型是定义了一个全新的类型 ...
- Go part 5 结构体,方法与接收器
结构体 结构体定义 结构体的定义只是一种内存布局的描述(相当于是一个模板),只有当结构体实例化时,才会真正分配内存空间 结构体是一种复合的基本类型,通过关键字 type 定义为 自定义 类型后,使结构 ...
- Golang的面向对象编程【结构体、方法、继承、接口】
Golang也支持面向对象编程.但与以前学过传统的面向对象编程语言有区别.1)Golang没有类class,Go语言的结构体struct和类class有相似的特性.2)Golang中不存在继承,方法重 ...
随机推荐
- [Javascript] Other functor
EventStream: You can use RxJS, BaconJS or any reactive programming lib you want: var id_s = map(func ...
- jquery datatable隐藏字段获取
如下,假Xpath为隐藏列,单击某一行时获取 $('#MessPropGrid tbody').on('click', 'tr', function () { tXpath=$("#Mess ...
- 《赢在用户:Web人物角色创建和应用实践指南》阅读总结
本书针对创建人物角色的每一个步骤,包括进行定性.定量的用户研究,生成人物角色分类,使人物角色真实可信等进行了十分详细的介绍.而且,在人物角色如何指导总体商业策略.确定信息架构.内容和设计 ...
- 第2章 数字之魅——寻找最大的K个数
寻找最大的K个数 问题描述 在面试中,有下面的问答: 问:有很多个无序的数,我们姑且假定它们各不相等,怎么选出其中最大的若干个数呢? 答:可以这样写:int array[100] …… 问:好,如果有 ...
- 实例源码--Android自定义Gallery动画效果
相关文档与源码: 下载源码 技术要点: 1.自定义控件的使用 2.Gallery控件的使用实例 3.详细的源码注释 ...... 详细介绍: 1.自定义控件的使用 本套源码通过自定义控件的方式,继 ...
- java_类承继其他类的内部类例子
package ming; class Outer { class In { public In(String msg) { System.out.println(msg); } } } public ...
- mysql自动备份策略
目标:每7天做一个完整备份,每天做一份binlog日志,第二周将之前的备份删除并产生新的完整备份和binlog日志,备份要求每天2:00自动完成 mysql 版本:mysql5.5 1.开启binlo ...
- Helpers\Number
Helpers\Number This helper has 2 methods for converting a number format and to get a percentage. Num ...
- CSS拾遗+技巧集合
1.实现尖角符号. 这是内联inline-block标签独有的特性. <!DOCTYPE html> <html lang="en"> <head&g ...
- U盘安装Debian 7
网上看到好多使用U盘安装的教程,齐说不一.实践是检验真理的标准,Try it ! 下载系统镜像:http://cdimage.debian.org/cdimage/release/current/am ...