CAF(C++ actor framework)(序列化之类,无需序列化,直接传)
Posted 浙江小麦
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 = 0, vector<int> b0 = vector<int>(0)) : 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{1,{2,3}}); } await_all_actors_done(); shutdown(); return 0; }
贴上结果
CAF 告诉我们,你想在消息中传送一个类,你只要告诉它你所有成员的getter 和setter函数,然后呢它提供了三种方式,(一般就选第一种 没有任何区别的 = = )。
稍微再深入一些的是类里面再套类对象,其实也是很方便知识多了一个嵌套。可以看github上announce4.cpp
https://github.com/actor-framework/actor-framework/blob/master/examples/type_system/announce_4.cpp
最后还有一篇想讲一下自己对announce5的代码理解和改编吧。
求粉丝,呵呵呵~
以上是关于CAF(C++ actor framework)(序列化之类,无需序列化,直接传)的主要内容,如果未能解决你的问题,请参考以下文章
CAF(C++ actor framework)使用随笔(各种send通信用法)
C++ Actor并发模型框架 Actor Framework (CAF)
CAF(C++ actor framework)使用随笔(延迟发送,消息转发,消息优先级)
CAF(C++ actor framework)(序列化之类,无需序列化,直接传)