Sophie

Sophie

distrib > Mageia > 7 > armv7hl > media > core-updates > by-pkgid > 94774a05d4f99367afd97b8b4adf565d > files > 1117

libfilezilla-devel-0.19.3-1.mga7.armv7hl.rpm

\hypertarget{timer_fizzbuzz_8cpp-example}{}\section{timer\+\_\+fizzbuzz.\+cpp}
A simple demonstration of using timers.\+This example creates and event loop and starts two timers to print fizz and buzz every 3 respective 5 seconds.

The user can also configure a third timer via stdin to print woof.

This example assumes you already understand the \mbox{\hyperlink{events_8cpp-example}{events.\+cpp}} example.


\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{\textcolor{preprocessor}{\#include <\mbox{\hyperlink{event__handler_8hpp}{libfilezilla/event\_handler.hpp}}>}}
\DoxyCodeLine{\textcolor{preprocessor}{\#include <\mbox{\hyperlink{time_8hpp}{libfilezilla/time.hpp}}>}}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{preprocessor}{\#include <iostream>}}
\DoxyCodeLine{\textcolor{preprocessor}{\#include <string>}}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{keyword}{struct }interval\_change\_type;}
\DoxyCodeLine{\textcolor{keyword}{typedef} \mbox{\hyperlink{classfz_1_1simple__event}{fz::simple\_event<interval\_change\_type, fz::duration>}} interval\_change;}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{keyword}{class }fizzbuzz final : \textcolor{keyword}{public} \mbox{\hyperlink{classfz_1_1event__handler}{fz::event\_handler}}}
\DoxyCodeLine{\{}
\DoxyCodeLine{\textcolor{keyword}{public}:}
\DoxyCodeLine{    fizzbuzz(\mbox{\hyperlink{classfz_1_1event__loop}{fz::event\_loop}}\& loop)}
\DoxyCodeLine{        : \mbox{\hyperlink{namespacefz}{fz}}::event\_handler(loop)}
\DoxyCodeLine{    \{}
\DoxyCodeLine{        \textcolor{comment}{// Start two periodic timers}}
\DoxyCodeLine{        fizz\_ = \mbox{\hyperlink{classfz_1_1event__handler_a38510d3665308dfbfd507401f36e65a2}{add\_timer}}(fz::duration::from\_seconds(3), \textcolor{keyword}{false});}
\DoxyCodeLine{        buzz\_ = \mbox{\hyperlink{classfz_1_1event__handler_a38510d3665308dfbfd507401f36e65a2}{add\_timer}}(fz::duration::from\_seconds(5), \textcolor{keyword}{false});}
\DoxyCodeLine{    \}}
\DoxyCodeLine{}
\DoxyCodeLine{    \textcolor{keyword}{virtual} ~fizzbuzz()}
\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{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<fz::timer\_event, interval\_change>(ev, \textcolor{keyword}{this}, \&fizzbuzz::on\_timer, \&fizzbuzz::on\_interval\_change);}
\DoxyCodeLine{    \}}
\DoxyCodeLine{}
\DoxyCodeLine{    \textcolor{keywordtype}{void} on\_interval\_change(\mbox{\hyperlink{classfz_1_1duration}{fz::duration}} interval)}
\DoxyCodeLine{    \{}
\DoxyCodeLine{        \textcolor{comment}{// We got told to change the woof interval.}}
\DoxyCodeLine{        \mbox{\hyperlink{classfz_1_1event__handler_a4f10ff19ab5af7814e7ba3ac6ae71bc8}{stop\_timer}}(woof\_);}
\DoxyCodeLine{        woof\_ = \mbox{\hyperlink{classfz_1_1event__handler_a38510d3665308dfbfd507401f36e65a2}{add\_timer}}(interval, \textcolor{keyword}{false});}
\DoxyCodeLine{    \}}
\DoxyCodeLine{}
\DoxyCodeLine{    \textcolor{keywordtype}{void} on\_timer(fz::timer\_id \textcolor{keyword}{const}\& \textcolor{keywordtype}{id})}
\DoxyCodeLine{    \{}
\DoxyCodeLine{        \textcolor{keywordflow}{if} (\textcolor{keywordtype}{id} == fizz\_) \{}
\DoxyCodeLine{            std::cout << \textcolor{stringliteral}{"fizz"} << std::endl;}
\DoxyCodeLine{        \}}
\DoxyCodeLine{        \textcolor{keywordflow}{else} \textcolor{keywordflow}{if} (\textcolor{keywordtype}{id} == buzz\_) \{}
\DoxyCodeLine{            std::cout << \textcolor{stringliteral}{"buzz"} << std::endl;}
\DoxyCodeLine{        \}}
\DoxyCodeLine{        \textcolor{keywordflow}{else} \textcolor{keywordflow}{if} (\textcolor{keywordtype}{id} == woof\_) \{}
\DoxyCodeLine{            std::cout << \textcolor{stringliteral}{"woof"} << std::endl;}
\DoxyCodeLine{        \}}
\DoxyCodeLine{    \}}
\DoxyCodeLine{}
\DoxyCodeLine{    fz::timer\_id fizz\_\{\};}
\DoxyCodeLine{    fz::timer\_id buzz\_\{\};}
\DoxyCodeLine{    fz::timer\_id woof\_\{\};}
\DoxyCodeLine{\};}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{keywordtype}{int} main()}
\DoxyCodeLine{\{}
\DoxyCodeLine{    \textcolor{comment}{// Start an event loop and add a handler.}}
\DoxyCodeLine{    \mbox{\hyperlink{classfz_1_1event__loop}{fz::event\_loop}} loop;}
\DoxyCodeLine{    fizzbuzz fb(loop);}
\DoxyCodeLine{}
\DoxyCodeLine{    std::cout << \textcolor{stringliteral}{"Enter interval in seconds for the woof timer or 0 to stop program."} << std::endl;}
\DoxyCodeLine{}
\DoxyCodeLine{    \textcolor{comment}{// Read numbers from stdin until reading fails or a 0 is entered}}
\DoxyCodeLine{    \textcolor{keywordtype}{int} seconds;}
\DoxyCodeLine{    \textcolor{keywordflow}{do} \{}
\DoxyCodeLine{        std::cin >> seconds;}
\DoxyCodeLine{        \textcolor{keywordflow}{if} (std::cin.good() \&\& seconds > 0) \{}
\DoxyCodeLine{            \textcolor{comment}{// Got a new interval, send to handler}}
\DoxyCodeLine{            fb.send\_event<interval\_change>(fz::duration::from\_seconds(seconds));}
\DoxyCodeLine{        \}}
\DoxyCodeLine{    \}}
\DoxyCodeLine{    \textcolor{keywordflow}{while} (std::cin.good() \&\& seconds != 0);}
\DoxyCodeLine{}
\DoxyCodeLine{    \textcolor{keywordflow}{return} 0;}
\DoxyCodeLine{\}}
\end{DoxyCodeInclude}