/[pcsx2_0.9.7]/trunk/common/include/Utilities/EventSource.inl
ViewVC logotype

Annotation of /trunk/common/include/Utilities/EventSource.inl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 31 - (hide annotations) (download)
Tue Sep 7 03:24:11 2010 UTC (9 years, 11 months ago) by william
File size: 3123 byte(s)
committing r3113 initial commit again...
1 william 31 /* PCSX2 - PS2 Emulator for PCs
2     * Copyright (C) 2002-2010 PCSX2 Dev Team
3     *
4     * PCSX2 is free software: you can redistribute it and/or modify it under the terms
5     * of the GNU Lesser General Public License as published by the Free Software Found-
6     * ation, either version 3 of the License, or (at your option) any later version.
7     *
8     * PCSX2 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
9     * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
10     * PURPOSE. See the GNU General Public License for more details.
11     *
12     * You should have received a copy of the GNU General Public License along with PCSX2.
13     * If not, see <http://www.gnu.org/licenses/>.
14     */
15    
16     #pragma once
17    
18     #include "Threading.h"
19    
20     using Threading::ScopedLock;
21    
22     template< typename ListenerType >
23     typename EventSource<ListenerType>::ListenerIterator EventSource<ListenerType>::Add( ListenerType& listener )
24     {
25     ScopedLock locker( m_listeners_lock );
26    
27     // Check for duplicates before adding the event.
28     if( IsDebugBuild )
29     {
30     ListenerIterator iter = m_listeners.begin();
31     while( iter != m_listeners.end() )
32     {
33     if( (*iter) == &listener ) return iter;
34     ++iter;
35     }
36     }
37     return _AddFast_without_lock( listener );
38     }
39    
40     template< typename ListenerType >
41     void EventSource<ListenerType>::Remove( ListenerType& listener )
42     {
43     ScopedLock locker( m_listeners_lock );
44     m_cache_valid = false;
45     m_listeners.remove( &listener );
46     }
47    
48     template< typename ListenerType >
49     void EventSource<ListenerType>::Remove( const ListenerIterator& listenerHandle )
50     {
51     ScopedLock locker( m_listeners_lock );
52     m_cache_valid = false;
53     m_listeners.erase( listenerHandle );
54     }
55    
56     template< typename ListenerType >
57     typename EventSource<ListenerType>::ListenerIterator EventSource<ListenerType>::_AddFast_without_lock( ListenerType& listener )
58     {
59     m_cache_valid = false;
60     m_listeners.push_front( &listener );
61     return m_listeners.begin();
62     }
63    
64    
65     template< typename ListenerType >
66     __forceinline void EventSource<ListenerType>::_DispatchRaw( ListenerIterator iter, const ListenerIterator& iend, const EvtParams& evtparams )
67     {
68     while( iter != iend )
69     {
70     try {
71     (*iter)->DispatchEvent( evtparams );
72     }
73     catch( Exception::RuntimeError& ex )
74     {
75     if( IsDevBuild ) {
76     pxFailDev( L"Ignoring runtime error thrown from event listener (event listeners should not throw exceptions!): " + ex.FormatDiagnosticMessage() );
77     }
78     else {
79     Console.Error( L"Ignoring runtime error thrown from event listener: " + ex.FormatDiagnosticMessage() );
80     }
81     }
82     catch( BaseException& ex )
83     {
84     if( IsDevBuild )
85     {
86     ex.DiagMsg() = L"Non-runtime BaseException thrown from event listener .. " + ex.DiagMsg();
87     throw;
88     }
89     Console.Error( L"Ignoring non-runtime BaseException thrown from event listener: " + ex.FormatDiagnosticMessage() );
90     }
91     ++iter;
92     }
93     }
94    
95     template< typename ListenerType >
96     void EventSource<ListenerType>::Dispatch( const EvtParams& evtparams )
97     {
98     if( !m_cache_valid )
99     {
100     m_cache_copy = m_listeners;
101     m_cache_valid = true;
102     }
103    
104     if( m_cache_copy.empty() ) return;
105     _DispatchRaw( m_cache_copy.begin(), m_cache_copy.end(), evtparams );
106     }

  ViewVC Help
Powered by ViewVC 1.1.22