<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/> <meta http-equiv="X-UA-Compatible" content="IE=9"/> <meta name="generator" content="Doxygen 1.8.15"/> <meta name="viewport" content="width=device-width, initial-scale=1"/> <title>libfilezilla: events.cpp</title> <link href="tabs.css" rel="stylesheet" type="text/css"/> <script type="text/javascript" src="jquery.js"></script> <script type="text/javascript" src="dynsections.js"></script> <link href="search/search.css" rel="stylesheet" type="text/css"/> <script type="text/javascript" src="search/searchdata.js"></script> <script type="text/javascript" src="search/search.js"></script> <link href="doxygen.css" rel="stylesheet" type="text/css" /> </head> <body> <div id="top"><!-- do not remove this div, it is closed by doxygen! --> <div id="titlearea"> <table cellspacing="0" cellpadding="0"> <tbody> <tr style="height: 56px;"> <td id="projectlogo"><img alt="Logo" src="libfilezilla.png"/></td> <td id="projectalign" style="padding-left: 0.5em;"> <div id="projectname">libfilezilla </div> </td> </tr> </tbody> </table> </div> <!-- end header part --> <!-- Generated by Doxygen 1.8.15 --> <script type="text/javascript"> /* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&dn=gpl-2.0.txt GPL-v2 */ var searchBox = new SearchBox("searchBox", "search",false,'Search'); /* @license-end */ </script> <script type="text/javascript" src="menudata.js"></script> <script type="text/javascript" src="menu.js"></script> <script type="text/javascript"> /* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&dn=gpl-2.0.txt GPL-v2 */ $(function() { initMenu('',true,false,'search.php','Search'); $(document).ready(function() { init_search(); }); }); /* @license-end */</script> <div id="main-nav"></div> </div><!-- top --> <!-- window showing the filter options --> <div id="MSearchSelectWindow" onmouseover="return searchBox.OnSearchSelectShow()" onmouseout="return searchBox.OnSearchSelectHide()" onkeydown="return searchBox.OnSearchSelectKey(event)"> </div> <!-- iframe showing the search results (closed by default) --> <div id="MSearchResultsWindow"> <iframe src="javascript:void(0)" frameborder="0" name="MSearchResults" id="MSearchResults"> </iframe> </div> <div class="header"> <div class="headertitle"> <div class="title">events.cpp</div> </div> </div><!--header--> <div class="contents"> <p>A simple demonstration of using the event system</p> <div class="fragment"><div class="line"><span class="preprocessor">#include <<a class="code" href="event__handler_8hpp.html">libfilezilla/event_handler.hpp</a>></span></div><div class="line"></div><div class="line"><span class="preprocessor">#include <iostream></span></div><div class="line"><span class="preprocessor">#include <string></span></div><div class="line"><span class="preprocessor">#include <vector></span></div><div class="line"></div><div class="line"><span class="comment">// A condition that we'll use in this example to signal the main</span></div><div class="line"><span class="comment">// thread that the worker has processed the event.</span></div><div class="line"><a name="_a0"></a><a class="code" href="classfz_1_1condition.html">fz::condition</a> c;</div><div class="line"><a name="_a1"></a><a class="code" href="classfz_1_1mutex.html">fz::mutex</a> m;</div><div class="line"></div><div class="line"></div><div class="line"><span class="comment">// Define a new event.</span></div><div class="line"><span class="comment">// The event is uniquely identified via the incomplete my_event_type struct and</span></div><div class="line"><span class="comment">// has two arguments: A string and a vector of ints.</span></div><div class="line"><span class="keyword">struct </span>my_event_type;</div><div class="line"><span class="keyword">typedef</span> <a name="_a2"></a><a class="code" href="classfz_1_1simple__event.html">fz::simple_event<my_event_type, std::string, std::vector<int></a>> my_event;</div><div class="line"></div><div class="line"><span class="comment">// A simple event handler</span></div><div class="line"><span class="keyword">class </span>handler final : <span class="keyword">public</span> <a name="_a3"></a><a class="code" href="classfz_1_1event__handler.html">fz::event_handler</a></div><div class="line">{</div><div class="line"><span class="keyword">public</span>:</div><div class="line"> handler(<a name="_a4"></a><a class="code" href="classfz_1_1event__loop.html">fz::event_loop</a>& l)</div><div class="line"> : <a class="code" href="namespacefz.html">fz</a>::event_handler(l)</div><div class="line"> {}</div><div class="line"></div><div class="line"> <span class="keyword">virtual</span> ~handler()</div><div class="line"> {</div><div class="line"> <span class="comment">// This _MUST_ be called to avoid a race so that operator()(fz::event_base const&) is not called on a partially destructed object.</span></div><div class="line"> <a name="a5"></a><a class="code" href="classfz_1_1event__handler.html#a50653cb04de110c658a44cf9eccfa391">remove_handler</a>();</div><div class="line"> }</div><div class="line"></div><div class="line"><span class="keyword">private</span>:</div><div class="line"> <span class="comment">// The event loop calls this function for every event sent to this handler.</span></div><div class="line"> <span class="keyword">virtual</span> <span class="keywordtype">void</span> <a name="a6"></a><a class="code" href="classfz_1_1event__handler.html#a98f41a1071a9ef5318929178dec7c45c">operator()</a>(<a name="_a7"></a><a class="code" href="classfz_1_1event__base.html">fz::event_base</a> <span class="keyword">const</span>& ev)</div><div class="line"> {</div><div class="line"> <span class="comment">// Dispatch the event to the correct function.</span></div><div class="line"> fz::dispatch<my_event>(ev, <span class="keyword">this</span>, &handler::on_my_event);</div><div class="line"> }</div><div class="line"></div><div class="line"> <span class="keywordtype">void</span> on_my_event(std::string <span class="keyword">const</span>& s, std::vector<int> <span class="keyword">const</span>& v)</div><div class="line"> {</div><div class="line"> std::cout << <span class="stringliteral">"Received event with text \""</span> << s << <span class="stringliteral">"\" and a vector with "</span> << v.size() << <span class="stringliteral">" elements"</span> << std::endl;</div><div class="line"></div><div class="line"> <span class="comment">// Signal the condition</span></div><div class="line"> <a name="_a8"></a><a class="code" href="classfz_1_1scoped__lock.html">fz::scoped_lock</a> lock(m);</div><div class="line"> c.<a name="a9"></a><a class="code" href="classfz_1_1condition.html#a93f86ef1fc563f47689777cb987b3b2b">signal</a>(lock);</div><div class="line"> }</div><div class="line">};</div><div class="line"></div><div class="line"><span class="keywordtype">int</span> main()</div><div class="line">{</div><div class="line"> <span class="comment">// Start an event loop</span></div><div class="line"> <a class="code" href="classfz_1_1event__loop.html">fz::event_loop</a> l;</div><div class="line"></div><div class="line"> <span class="comment">// Create a handler</span></div><div class="line"> handler h(l);</div><div class="line"></div><div class="line"> <span class="comment">// Send an event to the handler</span></div><div class="line"> h.send_event<my_event>(<span class="stringliteral">"Hello World!"</span>, std::vector<int>{23, 42, 666});</div><div class="line"></div><div class="line"> <span class="comment">// Wait until a signal from the worker thread</span></div><div class="line"> <a class="code" href="classfz_1_1scoped__lock.html">fz::scoped_lock</a> lock(m);</div><div class="line"> c.<a name="a10"></a><a class="code" href="classfz_1_1condition.html#a1d557df085bb788b92afe06ad00423c7">wait</a>(lock);</div><div class="line"></div><div class="line"> <span class="comment">// All done.</span></div><div class="line"> <span class="keywordflow">return</span> 0;</div><div class="line">}</div></div><!-- fragment --> </div><!-- contents --> <!-- start footer part --> <hr class="footer"/><address class="footer"><small> Generated by  <a href="http://www.doxygen.org/index.html"> <img class="footer" src="doxygen.png" alt="doxygen"/> </a> 1.8.15 </small></address> </body> </html>