An example for announce with non-primitive members.The output of this example program is:
#include <iostream>
#include "caf/all.hpp"
#include "caf/to_string.hpp"
using std::cout;
using std::endl;
using std::make_pair;
class foo {
int a_;
int b_;
public:
foo() : a_(0), b_(0) { }
foo(int a0, int b0) : 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; }
int b() const { return b_; }
void set_b(
int val) { b_ =
val; }
};
bool operator==(const foo& lhs, const foo& rhs) {
return lhs.a() == rhs.a()
&& lhs.b() == rhs.b();
}
struct bar {
foo f;
int i;
};
bool operator==(const bar& lhs, const bar& rhs) {
return lhs.f == rhs.f
&& lhs.i == rhs.i;
}
class baz {
foo f_;
public:
bar b;
baz() = default;
inline baz(const foo& mf, const bar& mb) : f_(mf), b(mb) { }
const foo& f() const { return f_; }
void set_f(
const foo&
val) { f_ =
val; }
};
bool operator==(const baz& lhs, const baz& rhs) {
return lhs.f() == rhs.f()
&& lhs.b == rhs.b;
}
auto set_next_behavior = [=] {
if (remaining > 1) testee(self, remaining - 1);
else self->quit();
};
self->become (
on<bar>() >> [=](
const bar&
val) {
aout(self) << "bar(foo("
<< val.f.a() << ", "
<< val.f.b() << "), "
<< val.i << ")"
<< endl;
set_next_behavior();
},
on<baz>() >> [=](const baz& val) {
aout(self) << to_string(make_message(val)) << endl;
set_next_behavior();
}
);
}
int main(int, char**) {
auto meta_bar_f = [] {
make_pair(&foo::a, &foo::set_a),
make_pair(&foo::b, &foo::set_b));
};
announce<bar>("bar", meta_bar_f(), &bar::i);
make_pair(&foo::a, &foo::set_a),
make_pair(&foo::b, &foo::set_b)),
auto t =
spawn(testee, 2);
{
self->
send(t, bar{foo{1, 2}, 3});
self->send(t, baz{foo{1, 2}, bar{foo{3, 4}, 5}});
}
return 0;
}