A simple example for announce with public accessible members.The output of this example program is:
#include <vector>
#include <cassert>
#include <utility>
#include <iostream>
#include "caf/all.hpp"
#include "caf/binary_serializer.hpp"
#include "caf/binary_deserializer.hpp"
using std::cout;
using std::endl;
using std::vector;
struct foo {
std::vector<int> a;
int b;
};
bool operator==(const foo& lhs, const foo& rhs) {
return lhs.a == rhs.a
&& lhs.b == rhs.b;
}
using foo_pair = std::pair<int, int>;
using foo_pair2 = std::pair<int, int>;
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;
}
auto set_next_behavior = [=] {
if (remaining > 1) testee(self, remaining - 1);
else self->quit();
};
self->become (
on<foo_pair>() >> [=](
const foo_pair&
val) {
cout << "foo_pair("
<< val.first << ", "
<< val.second << ")"
<< endl;
set_next_behavior();
},
on<foo>() >> [=](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**) {
announce<foo>("foo", &foo::a, &foo::b);
announce<foo2>("foo2", &foo2::a, &foo2::b);
foo2 vd;
vd.a = 5;
vd.b.resize(1);
vd.b.back().push_back(42);
vector<char> buf;
bs << vd;
foo2 vd2;
uniform_typeid<foo2>()->deserialize(&vd2, &bd);
assert(vd == vd2);
announce<foo_pair>("foo_pair", &foo_pair::first, &foo_pair::second);
assert(uniform_typeid<foo_pair>() == uniform_typeid<foo_pair2>());
auto t =
spawn(testee, 2);
{
self->
send(t, foo{std::vector<int>{1, 2, 3, 4}, 5});
self->send(t, foo_pair2{3, 4});
}
return 0;
}