1 /*
2  * Copyright (C) 2001-2008 Jacek Sieka, arnetheduck on gmail point com
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17  */
18 
19 #ifndef DCPLUSPLUS_DCPP_SPEAKER_H
20 #define DCPLUSPLUS_DCPP_SPEAKER_H
21 
22 #include "CriticalSection.h"
23 
24 namespace dcpp {
25 
26 template<typename Listener>
27 class Speaker {
28 	typedef vector<Listener*> ListenerList;
29 	typedef typename ListenerList::iterator ListenerIter;
30 
31 public:
Speaker()32 	Speaker() throw() { }
~Speaker()33 	virtual ~Speaker() throw() { }
34 
35 	template<typename T0>
fire(T0 type)36 	void fire(T0 type) throw() {
37 		Lock l(listenerCS);
38 		tmp = listeners;
39 		for(ListenerIter i=tmp.begin(); i != tmp.end(); ++i ) {
40 			(*i)->on(type);
41 		}
42 	}
43 
44 	template<typename T0, class T1>
fire(T0 type,const T1 & p1)45 	void fire(T0 type, const T1& p1) throw() {
46 		Lock l(listenerCS);
47 		tmp = listeners;
48 		for(ListenerIter i=tmp.begin(); i != tmp.end(); ++i ) {
49 			(*i)->on(type, p1);
50 		}
51 	}
52 	template<typename T0, class T1>
fire(T0 type,T1 & p1)53 	void fire(T0 type, T1& p1) throw() {
54 		Lock l(listenerCS);
55 		tmp = listeners;
56 		for(ListenerIter i=tmp.begin(); i != tmp.end(); ++i ) {
57 			(*i)->on(type, p1);
58 		}
59 	}
60 
61 	template<typename T0, class T1, class T2>
fire(T0 type,const T1 & p1,const T2 & p2)62 	void fire(T0 type, const T1& p1, const T2& p2) throw() {
63 		Lock l(listenerCS);
64 		tmp = listeners;
65 		for(ListenerIter i=tmp.begin(); i != tmp.end(); ++i ) {
66 			(*i)->on(type, p1, p2);
67 		}
68 	}
69 
70 	template<typename T0, class T1, class T2, class T3>
fire(T0 type,const T1 & p1,const T2 & p2,const T3 & p3)71 	void fire(T0 type, const T1& p1, const T2& p2, const T3& p3) throw() {
72 		Lock l(listenerCS);
73 		tmp = listeners;
74 		for(ListenerIter i=tmp.begin(); i != tmp.end(); ++i ) {
75 			(*i)->on(type, p1, p2, p3);
76 		}
77 	}
78 
79 	template<typename T0, class T1, class T2, class T3, class T4>
fire(T0 type,const T1 & p1,const T2 & p2,const T3 & p3,const T4 & p4)80 	void fire(T0 type, const T1& p1, const T2& p2, const T3& p3, const T4& p4) throw() {
81 		Lock l(listenerCS);
82 		tmp = listeners;
83 		for(ListenerIter i=tmp.begin(); i != tmp.end(); ++i ) {
84 			(*i)->on(type, p1, p2, p3, p4);
85 		}
86 	}
87 
88 	template<typename T0, class T1, class T2, class T3, class T4, class T5>
fire(T0 type,const T1 & p1,const T2 & p2,const T3 & p3,const T4 & p4,const T5 & p5)89 	void fire(T0 type, const T1& p1, const T2& p2, const T3& p3, const T4& p4, const T5& p5) throw() {
90 		Lock l(listenerCS);
91 		tmp = listeners;
92 		for(ListenerIter i=tmp.begin(); i != tmp.end(); ++i ) {
93 			(*i)->on(type, p1, p2, p3, p4, p5);
94 		}
95 	}
96 
97 	template<typename T0, class T1, class T2, class T3, class T4, class T5, class T6>
fire(T0 type,const T1 & p1,const T2 & p2,const T3 & p3,const T4 & p4,const T5 & p5,const T6 & p6)98 	void fire(T0 type, const T1& p1, const T2& p2, const T3& p3, const T4& p4, const T5& p5, const T6& p6) throw() {
99 		Lock l(listenerCS);
100 		tmp = listeners;
101 		for(ListenerIter i=tmp.begin(); i != tmp.end(); ++i ) {
102 			(*i)->on(type, p1, p2, p3, p4, p5, p6);
103 		}
104 	}
105 
addListener(Listener * aListener)106 	void addListener(Listener* aListener) {
107 		Lock l(listenerCS);
108 		if(find(listeners.begin(), listeners.end(), aListener) == listeners.end())
109 			listeners.push_back(aListener);
110 	}
111 
removeListener(Listener * aListener)112 	void removeListener(Listener* aListener) {
113 		Lock l(listenerCS);
114 		ListenerIter it = find(listeners.begin(), listeners.end(), aListener);
115 		if(it != listeners.end())
116 			listeners.erase(it);
117 	}
118 
removeListeners()119 	void removeListeners() {
120 		Lock l(listenerCS);
121 		listeners.clear();
122 	}
123 
124 protected:
125 	ListenerList listeners;
126 	ListenerList tmp;
127 	CriticalSection listenerCS;
128 };
129 
130 } // namespace dcpp
131 
132 #endif // !defined(SPEAKER_H)
133