\hypertarget{events_8cpp-example}{}\section{events.\+cpp} A simple demonstration of using the event system \begin{DoxyCodeInclude}{0} \DoxyCodeLine{\textcolor{preprocessor}{\#include <\mbox{\hyperlink{event__handler_8hpp}{libfilezilla/event\_handler.hpp}}>}} \DoxyCodeLine{} \DoxyCodeLine{\textcolor{preprocessor}{\#include <iostream>}} \DoxyCodeLine{\textcolor{preprocessor}{\#include <string>}} \DoxyCodeLine{\textcolor{preprocessor}{\#include <vector>}} \DoxyCodeLine{} \DoxyCodeLine{\textcolor{comment}{// A condition that we'll use in this example to signal the main}} \DoxyCodeLine{\textcolor{comment}{// thread that the worker has processed the event.}} \DoxyCodeLine{\mbox{\hyperlink{classfz_1_1condition}{fz::condition}} c;} \DoxyCodeLine{\mbox{\hyperlink{classfz_1_1mutex}{fz::mutex}} m;} \DoxyCodeLine{} \DoxyCodeLine{} \DoxyCodeLine{\textcolor{comment}{// Define a new event.}} \DoxyCodeLine{\textcolor{comment}{// The event is uniquely identified via the incomplete my\_event\_type struct and}} \DoxyCodeLine{\textcolor{comment}{// has two arguments: A string and a vector of ints.}} \DoxyCodeLine{\textcolor{keyword}{struct }my\_event\_type;} \DoxyCodeLine{\textcolor{keyword}{typedef} \mbox{\hyperlink{classfz_1_1simple__event}{fz::simple\_event<my\_event\_type, std::string, std::vector<int>}}> my\_event;} \DoxyCodeLine{} \DoxyCodeLine{\textcolor{comment}{// A simple event handler}} \DoxyCodeLine{\textcolor{keyword}{class }handler final : \textcolor{keyword}{public} \mbox{\hyperlink{classfz_1_1event__handler}{fz::event\_handler}}} \DoxyCodeLine{\{} \DoxyCodeLine{\textcolor{keyword}{public}:} \DoxyCodeLine{ handler(\mbox{\hyperlink{classfz_1_1event__loop}{fz::event\_loop}}\& l)} \DoxyCodeLine{ : \mbox{\hyperlink{namespacefz}{fz}}::event\_handler(l)} \DoxyCodeLine{ \{\}} \DoxyCodeLine{} \DoxyCodeLine{ \textcolor{keyword}{virtual} ~handler()} \DoxyCodeLine{ \{} \DoxyCodeLine{ \textcolor{comment}{// This \_MUST\_ be called to avoid a race so that operator()(fz::event\_base const\&) is not called on a partially destructed object.}} \DoxyCodeLine{ \mbox{\hyperlink{classfz_1_1event__handler_a50653cb04de110c658a44cf9eccfa391}{remove\_handler}}();} \DoxyCodeLine{ \}} \DoxyCodeLine{} \DoxyCodeLine{\textcolor{keyword}{private}:} \DoxyCodeLine{ \textcolor{comment}{// The event loop calls this function for every event sent to this handler.}} \DoxyCodeLine{ \textcolor{keyword}{virtual} \textcolor{keywordtype}{void} \mbox{\hyperlink{classfz_1_1event__handler_a98f41a1071a9ef5318929178dec7c45c}{operator()}}(\mbox{\hyperlink{classfz_1_1event__base}{fz::event\_base}} \textcolor{keyword}{const}\& ev)} \DoxyCodeLine{ \{} \DoxyCodeLine{ \textcolor{comment}{// Dispatch the event to the correct function.}} \DoxyCodeLine{ fz::dispatch<my\_event>(ev, \textcolor{keyword}{this}, \&handler::on\_my\_event);} \DoxyCodeLine{ \}} \DoxyCodeLine{} \DoxyCodeLine{ \textcolor{keywordtype}{void} on\_my\_event(std::string \textcolor{keyword}{const}\& s, std::vector<int> \textcolor{keyword}{const}\& v)} \DoxyCodeLine{ \{} \DoxyCodeLine{ std::cout << \textcolor{stringliteral}{"Received event with text \(\backslash\)""} << s << \textcolor{stringliteral}{"\(\backslash\)" and a vector with "} << v.size() << \textcolor{stringliteral}{" elements"} << std::endl;} \DoxyCodeLine{} \DoxyCodeLine{ \textcolor{comment}{// Signal the condition}} \DoxyCodeLine{ \mbox{\hyperlink{classfz_1_1scoped__lock}{fz::scoped\_lock}} lock(m);} \DoxyCodeLine{ c.\mbox{\hyperlink{classfz_1_1condition_a93f86ef1fc563f47689777cb987b3b2b}{signal}}(lock);} \DoxyCodeLine{ \}} \DoxyCodeLine{\};} \DoxyCodeLine{} \DoxyCodeLine{\textcolor{keywordtype}{int} main()} \DoxyCodeLine{\{} \DoxyCodeLine{ \textcolor{comment}{// Start an event loop}} \DoxyCodeLine{ \mbox{\hyperlink{classfz_1_1event__loop}{fz::event\_loop}} l;} \DoxyCodeLine{} \DoxyCodeLine{ \textcolor{comment}{// Create a handler}} \DoxyCodeLine{ handler h(l);} \DoxyCodeLine{} \DoxyCodeLine{ \textcolor{comment}{// Send an event to the handler}} \DoxyCodeLine{ h.send\_event<my\_event>(\textcolor{stringliteral}{"Hello World!"}, std::vector<int>\{23, 42, 666\});} \DoxyCodeLine{} \DoxyCodeLine{ \textcolor{comment}{// Wait until a signal from the worker thread}} \DoxyCodeLine{ \mbox{\hyperlink{classfz_1_1scoped__lock}{fz::scoped\_lock}} lock(m);} \DoxyCodeLine{ c.\mbox{\hyperlink{classfz_1_1condition_a1d557df085bb788b92afe06ad00423c7}{wait}}(lock);} \DoxyCodeLine{} \DoxyCodeLine{ \textcolor{comment}{// All done.}} \DoxyCodeLine{ \textcolor{keywordflow}{return} 0;} \DoxyCodeLine{\}} \end{DoxyCodeInclude}