<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Implementing flash.events.EventDispatcher in C++</title>
	<atom:link href="http://www.liranuna.com/implementing-eventdispatcher-in-cpp/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.liranuna.com/implementing-eventdispatcher-in-cpp/</link>
	<description>Just another coder</description>
	<lastBuildDate>Wed, 16 Nov 2011 19:05:53 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
	<item>
		<title>By: Patrick</title>
		<link>http://www.liranuna.com/implementing-eventdispatcher-in-cpp/comment-page-1/#comment-6882</link>
		<dc:creator>Patrick</dc:creator>
		<pubDate>Sat, 10 Sep 2011 03:44:29 +0000</pubDate>
		<guid isPermaLink="false">http://www.liranuna.com/?p=791#comment-6882</guid>
		<description>Tyler, you could use Event as a base class and create MouseEvent and KeyboardEvent classes that inherit from the base class.</description>
		<content:encoded><![CDATA[<p>Tyler, you could use Event as a base class and create MouseEvent and KeyboardEvent classes that inherit from the base class.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Tyler</title>
		<link>http://www.liranuna.com/implementing-eventdispatcher-in-cpp/comment-page-1/#comment-6874</link>
		<dc:creator>Tyler</dc:creator>
		<pubDate>Thu, 17 Mar 2011 04:22:21 +0000</pubDate>
		<guid isPermaLink="false">http://www.liranuna.com/?p=791#comment-6874</guid>
		<description>Very nice work!

I have been trying to modify these classes so that I can create custom event listeners, i.e. derived classes from the base Event class like KeyboardEvent or MouseEvent. But so far I am unable to get the addEventListener function to work since it takes in Event as a parameter.

Can anyone shed some light on how to do this?</description>
		<content:encoded><![CDATA[<p>Very nice work!</p>
<p>I have been trying to modify these classes so that I can create custom event listeners, i.e. derived classes from the base Event class like KeyboardEvent or MouseEvent. But so far I am unable to get the addEventListener function to work since it takes in Event as a parameter.</p>
<p>Can anyone shed some light on how to do this?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Shannon</title>
		<link>http://www.liranuna.com/implementing-eventdispatcher-in-cpp/comment-page-1/#comment-6856</link>
		<dc:creator>Shannon</dc:creator>
		<pubDate>Sun, 14 Nov 2010 05:48:41 +0000</pubDate>
		<guid isPermaLink="false">http://www.liranuna.com/?p=791#comment-6856</guid>
		<description>The complete code

Event

	// Event class
#include 
class Event
{
	public:
		Event(const std::string &amp;type, bool bubbles = false, bool cancelable = false):
			type(type), bubbles(bubbles), cancelable(cancelable)
		{
 
		}
 
		const std::string type;
 
		const bool bubbles;
		const bool cancelable;
	/*
		const void* target;
		const unsigned int eventPhase;
		const void* currentTarget;
	*/
};
 
	// Event function callback pointer type
typedef void (*eventFunctionPtr)(const Event &amp;);

EventDispatcher.hpp -----------------------------------------------------------------


#include 
#include 
#include &quot;event.hpp&quot;
#include 
class EventDispatcher
{

	private:
		std::map&lt;const std::string, std::map&lt;int, std::list &gt; &gt; eventHandlerList;;

	public:
		void addEventListener(const std::string &amp;type, eventFunctionPtr listener, bool useCapture = false, int priority = 0, bool useWeakReference = false)
		{
				// Simply add the event listener to the list of listeners for the selected priority
			eventHandlerList[type][priority].push_back(listener);
		}

		bool hasEventListener(const std::string &amp;type)
		{
			return (eventHandlerList.find(type) != eventHandlerList.end());
		}

		void dispatchEvent(const Event &amp;event)
		{
				// Leave if no event registered
			if(!hasEventListener(event.type))
				return;
 
				// A reference to keep code clean
			std::map&lt;int, std::list &gt; &amp;allFunctions = eventHandlerList[event.type];
 
				// Iterate through all functions in the event, from high proproty to low
			for(std::map&lt;int, std::list &gt;::reverse_iterator i=allFunctions.rbegin(); i!=allFunctions.rend(); ++i) {
				std::list &amp;funcList = i-&gt;second;
					// Execute callbacks
				for(std::list::iterator f=funcList.begin(); f!=funcList.end(); ++f)
					(*f)(event);
			}
		}

		void removeEventListener(const std::string &amp;type, eventFunctionPtr listener, bool useCapture = false)
		{
				// Leave if no event registered
			if(!hasEventListener(type))
				return;
 
				// Reference to keep the code clean
			std::map&lt;int, std::list &gt; &amp;allFunctions = eventHandlerList[type];
 
				// Since we don&#039;t know the function&#039;s priority, we&#039;ll search for it
			for(std::map&lt;int, std::list &gt;::iterator i=allFunctions.begin(); i!=allFunctions.end(); ++i) {
					// Saving a branch here: instead of checking if the callback exists let remove() do it for us
				i-&gt;second.remove(listener);
 
					// Remove object from the map if list gone empty to eliminate false positives
				if(i-&gt;second.empty())
					allFunctions.erase(i);
			}
 
				// Remove map to eliminate false positives
			if(allFunctions.empty())
				eventHandlerList.erase(type);
		}
 
};



// Obviously put the implementation into a cpp file</description>
		<content:encoded><![CDATA[<p>The complete code</p>
<p>Event</p>
<p>	// Event class<br />
#include<br />
class Event<br />
{<br />
	public:<br />
		Event(const std::string &amp;type, bool bubbles = false, bool cancelable = false):<br />
			type(type), bubbles(bubbles), cancelable(cancelable)<br />
		{</p>
<p>		}</p>
<p>		const std::string type;</p>
<p>		const bool bubbles;<br />
		const bool cancelable;<br />
	/*<br />
		const void* target;<br />
		const unsigned int eventPhase;<br />
		const void* currentTarget;<br />
	*/<br />
};</p>
<p>	// Event function callback pointer type<br />
typedef void (*eventFunctionPtr)(const Event &amp;);</p>
<p>EventDispatcher.hpp &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;</p>
<p>#include<br />
#include<br />
#include &#8220;event.hpp&#8221;<br />
#include<br />
class EventDispatcher<br />
{</p>
<p>	private:<br />
		std::map&lt;const std::string, std::map&lt;int, std::list &gt; &gt; eventHandlerList;;</p>
<p>	public:<br />
		void addEventListener(const std::string &amp;type, eventFunctionPtr listener, bool useCapture = false, int priority = 0, bool useWeakReference = false)<br />
		{<br />
				// Simply add the event listener to the list of listeners for the selected priority<br />
			eventHandlerList[type][priority].push_back(listener);<br />
		}</p>
<p>		bool hasEventListener(const std::string &amp;type)<br />
		{<br />
			return (eventHandlerList.find(type) != eventHandlerList.end());<br />
		}</p>
<p>		void dispatchEvent(const Event &amp;event)<br />
		{<br />
				// Leave if no event registered<br />
			if(!hasEventListener(event.type))<br />
				return;</p>
<p>				// A reference to keep code clean<br />
			std::map&lt;int, std::list &gt; &amp;allFunctions = eventHandlerList[event.type];</p>
<p>				// Iterate through all functions in the event, from high proproty to low<br />
			for(std::map&lt;int, std::list &gt;::reverse_iterator i=allFunctions.rbegin(); i!=allFunctions.rend(); ++i) {<br />
				std::list &amp;funcList = i-&gt;second;<br />
					// Execute callbacks<br />
				for(std::list::iterator f=funcList.begin(); f!=funcList.end(); ++f)<br />
					(*f)(event);<br />
			}<br />
		}</p>
<p>		void removeEventListener(const std::string &amp;type, eventFunctionPtr listener, bool useCapture = false)<br />
		{<br />
				// Leave if no event registered<br />
			if(!hasEventListener(type))<br />
				return;</p>
<p>				// Reference to keep the code clean<br />
			std::map&lt;int, std::list &gt; &amp;allFunctions = eventHandlerList[type];</p>
<p>				// Since we don&#8217;t know the function&#8217;s priority, we&#8217;ll search for it<br />
			for(std::map&lt;int, std::list &gt;::iterator i=allFunctions.begin(); i!=allFunctions.end(); ++i) {<br />
					// Saving a branch here: instead of checking if the callback exists let remove() do it for us<br />
				i-&gt;second.remove(listener);</p>
<p>					// Remove object from the map if list gone empty to eliminate false positives<br />
				if(i-&gt;second.empty())<br />
					allFunctions.erase(i);<br />
			}</p>
<p>				// Remove map to eliminate false positives<br />
			if(allFunctions.empty())<br />
				eventHandlerList.erase(type);<br />
		}</p>
<p>};</p>
<p>// Obviously put the implementation into a cpp file</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Paul</title>
		<link>http://www.liranuna.com/implementing-eventdispatcher-in-cpp/comment-page-1/#comment-6788</link>
		<dc:creator>Paul</dc:creator>
		<pubDate>Sun, 19 Sep 2010 04:56:01 +0000</pubDate>
		<guid isPermaLink="false">http://www.liranuna.com/?p=791#comment-6788</guid>
		<description>You might consider using std::multimap to store the priorities and callbacks instead of std::map&lt; int, std::list&lt;...

Cheers.</description>
		<content:encoded><![CDATA[<p>You might consider using std::multimap to store the priorities and callbacks instead of std::map&lt; int, std::list&lt;&#8230;</p>
<p>Cheers.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: AStorm Devblog</title>
		<link>http://www.liranuna.com/implementing-eventdispatcher-in-cpp/comment-page-1/#comment-6717</link>
		<dc:creator>AStorm Devblog</dc:creator>
		<pubDate>Mon, 23 Aug 2010 19:28:29 +0000</pubDate>
		<guid isPermaLink="false">http://www.liranuna.com/?p=791#comment-6717</guid>
		<description>&lt;strong&gt;Template et héritage...&lt;/strong&gt;

Afin de pouvoir faire de la programmation événementielle en C++ j&#039;ai commencé par (ré)implémenter une classe EventDispatcher. Et comme je la voulais capable de propager n&#039;importe quels types d&#039;événement, je me suis heurté à quelques menus soucis......</description>
		<content:encoded><![CDATA[<p><strong>Template et héritage&#8230;</strong></p>
<p>Afin de pouvoir faire de la programmation événementielle en C++ j&#8217;ai commencé par (ré)implémenter une classe EventDispatcher. Et comme je la voulais capable de propager n&#8217;importe quels types d&#8217;événement, je me suis heurté à quelques menus soucis&#8230;&#8230;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Shannon</title>
		<link>http://www.liranuna.com/implementing-eventdispatcher-in-cpp/comment-page-1/#comment-6693</link>
		<dc:creator>Shannon</dc:creator>
		<pubDate>Sun, 14 Feb 2010 09:43:15 +0000</pubDate>
		<guid isPermaLink="false">http://www.liranuna.com/?p=791#comment-6693</guid>
		<description>Perhaps you could have a crack at a C++ version of binding and a C++ version of MXML!</description>
		<content:encoded><![CDATA[<p>Perhaps you could have a crack at a C++ version of binding and a C++ version of MXML!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Shannon</title>
		<link>http://www.liranuna.com/implementing-eventdispatcher-in-cpp/comment-page-1/#comment-6692</link>
		<dc:creator>Shannon</dc:creator>
		<pubDate>Tue, 09 Feb 2010 12:44:45 +0000</pubDate>
		<guid isPermaLink="false">http://www.liranuna.com/?p=791#comment-6692</guid>
		<description>Thank you very much!  This is awesome.  As someone who programs in both Flex/AS3 and C++, and who likes the EventDispatcher way of doing things too, this saved me a lot of time.  Keep up the good work!!!</description>
		<content:encoded><![CDATA[<p>Thank you very much!  This is awesome.  As someone who programs in both Flex/AS3 and C++, and who likes the EventDispatcher way of doing things too, this saved me a lot of time.  Keep up the good work!!!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Guus Geurkink</title>
		<link>http://www.liranuna.com/implementing-eventdispatcher-in-cpp/comment-page-1/#comment-6690</link>
		<dc:creator>Guus Geurkink</dc:creator>
		<pubDate>Mon, 08 Feb 2010 14:45:43 +0000</pubDate>
		<guid isPermaLink="false">http://www.liranuna.com/?p=791#comment-6690</guid>
		<description>Wow, this is fantastic work!
I wrote a whole GUI class with Event listeners, and this helped me alot!

Keep it up ~</description>
		<content:encoded><![CDATA[<p>Wow, this is fantastic work!<br />
I wrote a whole GUI class with Event listeners, and this helped me alot!</p>
<p>Keep it up ~</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: NinjaCoder</title>
		<link>http://www.liranuna.com/implementing-eventdispatcher-in-cpp/comment-page-1/#comment-6571</link>
		<dc:creator>NinjaCoder</dc:creator>
		<pubDate>Thu, 12 Mar 2009 11:27:23 +0000</pubDate>
		<guid isPermaLink="false">http://www.liranuna.com/?p=791#comment-6571</guid>
		<description>haha nice! I&#039;m a professional flash coder at work, but developing nds stuff on my free time. Thinking of implementing a lite version of the &quot;Event structure&quot; in my ndslib... anyway nice artical</description>
		<content:encoded><![CDATA[<p>haha nice! I&#8217;m a professional flash coder at work, but developing nds stuff on my free time. Thinking of implementing a lite version of the &#8220;Event structure&#8221; in my ndslib&#8230; anyway nice artical</p>
]]></content:encoded>
	</item>
</channel>
</rss>

