We first include the channel header file
#include <boost/channel/channel.hpp>
As a first step, instantiate a concrete channel type according to application's requirement. We could consider what data type to use as ids, and what kind of namespace and dispatcher will be best fit the application. Here we use strings as ids and use default linear_namespace and broadcast dispatcher:
typedef channel<string> evt_chan;
Next we
define a few event ids to simulate some possible events inside a GUI
window:string down_id = "_window_button_down_";
string up_id = "_window_button_up_";
string close_id = "_window_close_";
struct evt_data {
string data_;
evt_data(const char *d) : data_(d) {}
};
Then we define the event source of this sample: a simplest GUI window without any features except those for sending events: bound channel and named_outs (also typedefed inside channel: evt_chan::out). Inside gui_window constructor, we bind named_outs to the above mentioned event ids in channel to send events:
class gui_window {
evt_chan & chan_;
//a few event src
evt_chan::out down_out_;
evt_chan::out close_out_;
public:
gui_window(evt_chan & c): chan_(c),
down_out_(chan_, down_id),
up_out_(chan_, up_id),
close_out_(chan_, close_id) {}
...and define some utility methods to send out events thru named_outs ...
};
class window_handler {
evt_chan & ch_;
//a few event sinks
evt_chan::in *down_in_;
evt_chan::in *up_in_;
evt_chan::in *close_in_;
public:
window_handler(evt_chan & c): ch_(c) {
down_in_ = ch_.bind_name_in(down_id, boost::bind(&window_handler::down, this, _1, _2));
up_in_ = ch_.bind_name_in(up_id, boost::bind(&window_handler::up, this, _1, _2));
close_in_ = ch_.bind_name_in(close_id, boost::bind(&window_handler::close, this, _1, _2));
}
.... here define methods as event callbacks ...
};
int main(int, char **) {
The
first thing is to create the event channelevt_chan chan;
create the event source (gui_window) and bind it to channelgui_window window(chan);
create a handler object and bind it to channel window_handler hdlr(chan);
Next we create more event sinks to bind the global function to event
ids.evt_chan::in window_down_in(chan, down_id, evt_handler)
evt_chan::in window_up_in(chan, up_id, evt_handler);
evt_chan::in shut_down_in(chan, close_id, evt_handler);
Last, we fire some eventswindow.up("..Hi there [mouse-left-up] ..");
window.down(".. a simple window test [mouse-right-down] ..");
window.close(".. simple window wiered [window-closed] ..");
Then exitcout << "... exit ...\n";
return 0;
In this simple sample, we bind event handlers/callbacks to event
sources indirectly by binding to names in channel namespace. After
namespace binding, event dispatching happens directly between named_out
(senders) and named_in (receivers).