昨天讲了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)(序列化之类,无需序列化,直接传)(二)的更多相关文章

  1. 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 ...

  2. Django REST framework 中文教程1:序列化

    建立环境 在我们做任何事情之前,我们将使用virtualenv创建一个新的虚拟环境.这将确保我们的包配置与我们正在开展的任何其他项目保持良好的隔离. virtualenv envsource env/ ...

  3. Django rest framework(6)----序列化

    目录 Django rest framework(1)----认证 Django rest framework(2)----权限 Django rest framework(3)----节流 Djan ...

  4. C#中的二进制序列化和Json序列化

    序列化就是把一个对象变成流的形式,方便传输和还原.小弟不才,总结下对二进制序列化和Json序列化的使用: 1.首先,二进制序列化(BinaryFormatter)要求要序列化的类必须是可序列化的(即在 ...

  5. Django-Rest-Framework的序列化之serializers 序列化组件

    Django-Rest-Framework的序列化之serializers 序列化组件 restful framework 正常的序列化 from django.http import HttpRes ...

  6. [LeetCode] Serialize and Deserialize BST 二叉搜索树的序列化和去序列化

    Serialization is the process of converting a data structure or object into a sequence of bits so tha ...

  7. [LeetCode] Serialize and Deserialize Binary Tree 二叉树的序列化和去序列化

    Serialization is the process of converting a data structure or object into a sequence of bits so tha ...

  8. java编解码技术,json序列化与二进制序列化

    1.何为json序列化与二进制序列化 通常我们在程序中采用的以json为传输,将json转为对象的就是json序列化了.而二进制序列化通常是我们将数据转换为二进制进行传输,然后在进行各类转换操作 2. ...

  9. [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 ...

  10. Effective Java 第三版—— 90.考虑序列化代理替代序列化实例

    Tips 书中的源代码地址:https://github.com/jbloch/effective-java-3e-source-code 注意,书中的有些代码里方法是基于Java 9 API中的,所 ...

随机推荐

  1. HDU 4819 Mosaic 二维线段树

    Mosaic Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/view.action ...

  2. Cache选型的一些思考

    Cache对于减轻DB负载有非常关键的数据.以下对经常使用的memcached和redis做个总结,便于技术选型. 1 memcached  (1) 支持的操作有限,支持经常使用的set,get,de ...

  3. vector<int> v2 = 42; 为何非法

    C++ Primer 第四版,第十三章“复制控制” 习题13.2,为何vector<int> v2 = 42; 不能编译? 百度贴吧里的一位楼主给出了答案,本人认为正确,特此引用: 参考链 ...

  4. 使用IIS 7.0 Smooth Streaming 优化视频服务

    http://www.cnblogs.com/dudu/archive/2013/06/08/iis_webserver_settings.html (支持高并发的IIS Web服务器常用设置) ht ...

  5. 框架使用的技术主要是SpringMVC 在此基础上进行扩展

    框架使用的技术主要是SpringMVC 在此基础上进行扩展 1 Web前端使用 2 前段控制器采用SpringMVC零配置 3 IOC容器Spring 4 ORM使用 Mybites或者hiberna ...

  6. Android中Activity启动模式详解

    在Android中每个界面都是一个Activity,切换界面操作其实是多个不同Activity之间的实例化操作.在Android中Activity的启动模式决定了Activity的启动运行方式. An ...

  7. Agile software architecture design document style..( sketches and no UMLs)

    http://www.infoq.com/articles/agile-software-architecture-sketches-NoUML If you're working in an agi ...

  8. 对x264_macroblock_cache_load的理解

    X264版本: 2004/06/03 函数作用: 将编码该宏块所需的信息加载到mb.pic.mb.cache两个结构体中,记录相邻宏块的存在性. 函数过程: 初始化坐标信息,这些坐标信息将在下面用作下 ...

  9. 生成MD5的方法

    1.需要一个字符串,接受md5值 2.创建一个MD5实例,获得md5.Create()方法. //使用using 3.将字符串.文件流进行计算.返回二进制数组,Encoding.UTF8.GetByt ...

  10. vsftp安装与配置

    配置参考:https://help.aliyun.com/knowledge_detail/5973912.html?spm=5176.776701992.0.0.3X2PB8 553 Could n ...