← Back to team overview

linuxdcpp-team team mailing list archive

[Branch ~dcplusplus-team/dcplusplus/trunk] Rev 2181: use perfect forwarding in the Speaker class

 

------------------------------------------------------------
revno: 2181
committer: poy <poy@xxxxxxxxxx>
branch nick: repo
timestamp: Sat 2010-07-10 18:37:01 +0200
message:
  use perfect forwarding in the Speaker class
modified:
  dcpp/Speaker.h


--
lp:dcplusplus
https://code.launchpad.net/~dcplusplus-team/dcplusplus/trunk

Your team Dcplusplus-team is subscribed to branch lp:dcplusplus.
To unsubscribe from this branch go to https://code.launchpad.net/~dcplusplus-team/dcplusplus/trunk/+edit-subscription
=== modified file 'dcpp/Speaker.h'
--- dcpp/Speaker.h	2010-02-11 21:44:13 +0000
+++ dcpp/Speaker.h	2010-07-10 16:37:01 +0000
@@ -26,80 +26,73 @@
 template<typename Listener>
 class Speaker {
 	typedef vector<Listener*> ListenerList;
-	typedef typename ListenerList::iterator ListenerIter;
 
 public:
 	Speaker() throw() { }
 	virtual ~Speaker() throw() { }
 
+	/// @todo simplify when we have variadic templates
+
 	template<typename T0>
-	void fire(T0 type) throw() {
-		Lock l(listenerCS);
-		tmp = listeners;
-		for(ListenerIter i=tmp.begin(); i != tmp.end(); ++i ) {
-			(*i)->on(type);
-		}
-	}
-
-	template<typename T0, class T1>
-	void fire(T0 type, const T1& p1) throw() {
-		Lock l(listenerCS);
-		tmp = listeners;
-		for(ListenerIter i=tmp.begin(); i != tmp.end(); ++i ) {
-			(*i)->on(type, p1);
-		}
-	}
-	template<typename T0, class T1>
-	void fire(T0 type, T1& p1) throw() {
-		Lock l(listenerCS);
-		tmp = listeners;
-		for(ListenerIter i=tmp.begin(); i != tmp.end(); ++i ) {
-			(*i)->on(type, p1);
-		}
-	}
-
-	template<typename T0, class T1, class T2>
-	void fire(T0 type, const T1& p1, const T2& p2) throw() {
-		Lock l(listenerCS);
-		tmp = listeners;
-		for(ListenerIter i=tmp.begin(); i != tmp.end(); ++i ) {
-			(*i)->on(type, p1, p2);
-		}
-	}
-
-	template<typename T0, class T1, class T2, class T3>
-	void fire(T0 type, const T1& p1, const T2& p2, const T3& p3) throw() {
-		Lock l(listenerCS);
-		tmp = listeners;
-		for(ListenerIter i=tmp.begin(); i != tmp.end(); ++i ) {
-			(*i)->on(type, p1, p2, p3);
-		}
-	}
-
-	template<typename T0, class T1, class T2, class T3, class T4>
-	void fire(T0 type, const T1& p1, const T2& p2, const T3& p3, const T4& p4) throw() {
-		Lock l(listenerCS);
-		tmp = listeners;
-		for(ListenerIter i=tmp.begin(); i != tmp.end(); ++i ) {
-			(*i)->on(type, p1, p2, p3, p4);
-		}
-	}
-
-	template<typename T0, class T1, class T2, class T3, class T4, class T5>
-	void fire(T0 type, const T1& p1, const T2& p2, const T3& p3, const T4& p4, const T5& p5) throw() {
-		Lock l(listenerCS);
-		tmp = listeners;
-		for(ListenerIter i=tmp.begin(); i != tmp.end(); ++i ) {
-			(*i)->on(type, p1, p2, p3, p4, p5);
-		}
-	}
-
-	template<typename T0, class T1, class T2, class T3, class T4, class T5, class T6>
-	void fire(T0 type, const T1& p1, const T2& p2, const T3& p3, const T4& p4, const T5& p5, const T6& p6) throw() {
-		Lock l(listenerCS);
-		tmp = listeners;
-		for(ListenerIter i=tmp.begin(); i != tmp.end(); ++i ) {
-			(*i)->on(type, p1, p2, p3, p4, p5, p6);
+	void fire(T0&& type) throw() {
+		Lock l(listenerCS);
+		tmp = listeners;
+		for(auto i = tmp.begin(); i != tmp.end(); ++i) {
+			(*i)->on(forward<T0>(type));
+		}
+	}
+
+	template<typename T0, typename T1>
+	void fire(T0&& type, T1&& p1) throw() {
+		Lock l(listenerCS);
+		tmp = listeners;
+		for(auto i = tmp.begin(); i != tmp.end(); ++i) {
+			(*i)->on(forward<T0>(type), forward<T1>(p1));
+		}
+	}
+
+	template<typename T0, typename T1, typename T2>
+	void fire(T0&& type, T1&& p1, T2&& p2) throw() {
+		Lock l(listenerCS);
+		tmp = listeners;
+		for(auto i = tmp.begin(); i != tmp.end(); ++i) {
+			(*i)->on(forward<T0>(type), forward<T1>(p1), forward<T2>(p2));
+		}
+	}
+
+	template<typename T0, typename T1, typename T2, typename T3>
+	void fire(T0&& type, T1&& p1, T2&& p2, T3&& p3) throw() {
+		Lock l(listenerCS);
+		tmp = listeners;
+		for(auto i = tmp.begin(); i != tmp.end(); ++i) {
+			(*i)->on(forward<T0>(type), forward<T1>(p1), forward<T2>(p2), forward<T3>(p3));
+		}
+	}
+
+	template<typename T0, typename T1, typename T2, typename T3, typename T4>
+	void fire(T0&& type, T1&& p1, T2&& p2, T3&& p3, T4&& p4) throw() {
+		Lock l(listenerCS);
+		tmp = listeners;
+		for(auto i = tmp.begin(); i != tmp.end(); ++i) {
+			(*i)->on(forward<T0>(type), forward<T1>(p1), forward<T2>(p2), forward<T3>(p3), forward<T4>(p4));
+		}
+	}
+
+	template<typename T0, typename T1, typename T2, typename T3, typename T4, typename T5>
+	void fire(T0&& type, T1&& p1, T2&& p2, T3&& p3, T4&& p4, T5&& p5) throw() {
+		Lock l(listenerCS);
+		tmp = listeners;
+		for(auto i = tmp.begin(); i != tmp.end(); ++i) {
+			(*i)->on(forward<T0>(type), forward<T1>(p1), forward<T2>(p2), forward<T3>(p3), forward<T4>(p4), forward<T5>(p5));
+		}
+	}
+
+	template<typename T0, typename T1, typename T2, typename T3, typename T4, typename T5, typename T6>
+	void fire(T0&& type, T1&& p1, T2&& p2, T3&& p3, T4&& p4, T5&& p5, T6&& p6) throw() {
+		Lock l(listenerCS);
+		tmp = listeners;
+		for(auto i = tmp.begin(); i != tmp.end(); ++i) {
+			(*i)->on(forward<T0>(type), forward<T1>(p1), forward<T2>(p2), forward<T3>(p3), forward<T4>(p4), forward<T5>(p5), forward<T6>(p6));
 		}
 	}
 
@@ -111,7 +104,7 @@
 
 	void removeListener(Listener* aListener) {
 		Lock l(listenerCS);
-		ListenerIter it = find(listeners.begin(), listeners.end(), aListener);
+		auto it = find(listeners.begin(), listeners.end(), aListener);
 		if(it != listeners.end())
 			listeners.erase(it);
 	}