An example for announce with overloaded getter and setter member functions.The output of this example program is:
foo(1, 2)
#include <utility>
#include <iostream>
#include "caf/all.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_; }
int b() const { return b_; }
void b(
int val) { b_ =
val; }
};
bool operator==(const foo& lhs, const foo& rhs) {
return lhs.a() == rhs.a()
&& lhs.b() == rhs.b();
}
using foo_getter = int (foo::*)() const;
using foo_setter = void (foo::*)(int);
self->become (
on<foo>() >> [=](
const foo&
val) {
aout(self) << "foo("
<< val.a() << ", "
<< val.b() << ")"
<< endl;
self->quit();
}
);
}
int main(int, char**) {
foo_getter g1 = &foo::a;
foo_setter s1 = &foo::a;
foo_getter g2 = &foo::b;
foo_setter s2 = &foo::b;
announce<foo>("foo", make_pair(g1, s1), make_pair(g2, s2));
announce<foo>("foo",
make_pair(static_cast<foo_getter>(&foo::a),
static_cast<foo_setter>(&foo::a)),
make_pair(static_cast<foo_getter>(&foo::b),
static_cast<foo_setter>(&foo::b)));
{
}
return 0;
}