1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
|
#include <libfilezilla/event_handler.hpp>
#include <iostream>
#include <string>
#include <vector>
// A condition that we'll use in this example to signal the main
// thread that the worker has processed the event.
fz::condition c;
fz::mutex m;
// Define a new event.
// The event is uniquely identified via the incomplete my_event_type struct and
// has two arguments: A string and a vector of ints.
struct my_event_type;
typedef fz::simple_event<my_event_type, std::string, std::vector<int>> my_event;
// A simple event handler
class handler final : public fz::event_handler
{
public:
handler(fz::event_loop& l)
: fz::event_handler(l)
{}
virtual ~handler()
{
// This _MUST_ be called to avoid a race so that operator()(fz::event_base const&) is not called on a partially destructed object.
remove_handler();
}
private:
// The event loop calls this function for every event sent to this handler.
virtual void operator()(fz::event_base const& ev)
{
// Dispatch the event to the correct function.
fz::dispatch<my_event>(ev, this, &handler::on_my_event);
}
void on_my_event(std::string const& s, std::vector<int> const& v)
{
std::cout << "Received event with text \"" << s << "\" and a vector with " << v.size() << " elements" << std::endl;
// Signal the condition
fz::scoped_lock lock(m);
c.signal(lock);
}
};
int main()
{
// Start an event loop
fz::event_loop l;
// Create a handler
handler h(l);
// Send an event to the handler
h.send_event<my_event>("Hello World!", std::vector<int>{23, 42, 666});
// Wait until a signal from the worker thread
fz::scoped_lock lock(m);
c.wait(lock);
// All done.
return 0;
}
|