2009-10-07 19:20:11 +00:00
|
|
|
/* PCSX2 - PS2 Emulator for PCs
|
|
|
|
* Copyright (C) 2002-2009 PCSX2 Dev Team
|
|
|
|
*
|
|
|
|
* PCSX2 is free software: you can redistribute it and/or modify it under the terms
|
|
|
|
* of the GNU Lesser General Public License as published by the Free Software Found-
|
|
|
|
* ation, either version 3 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* PCSX2 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
|
|
|
|
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE. See the GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along with PCSX2.
|
|
|
|
* If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2009-10-07 20:50:39 +00:00
|
|
|
#include <list>
|
2010-01-08 23:33:15 +00:00
|
|
|
#include "Threading.h"
|
2009-10-23 20:24:59 +00:00
|
|
|
|
|
|
|
// --------------------------------------------------------------------------------------
|
|
|
|
// EventSource< template EvtType >
|
|
|
|
// --------------------------------------------------------------------------------------
|
|
|
|
|
2010-01-22 15:22:01 +00:00
|
|
|
template< typename ListenerType >
|
2009-10-07 19:20:11 +00:00
|
|
|
class EventSource
|
|
|
|
{
|
|
|
|
public:
|
2010-01-22 15:22:01 +00:00
|
|
|
typedef typename ListenerType::EvtParams EvtParams;
|
|
|
|
typedef typename std::list< ListenerType* > ListenerList;
|
|
|
|
typedef typename ListenerList::iterator ListenerIterator;
|
2009-10-07 20:50:39 +00:00
|
|
|
|
2009-10-07 19:20:11 +00:00
|
|
|
protected:
|
2009-10-23 20:24:59 +00:00
|
|
|
typedef typename ListenerList::const_iterator ConstIterator;
|
|
|
|
|
2009-10-25 22:33:23 +00:00
|
|
|
ListenerList m_listeners;
|
2009-10-23 21:05:32 +00:00
|
|
|
|
2009-10-23 20:24:59 +00:00
|
|
|
// This is a cached copy of the listener list used to handle standard dispatching, which
|
|
|
|
// allows for self-modification of the EventSource's listener list by the listeners.
|
|
|
|
// Translation: The dispatcher uses this copy instead, to avoid iterator invalidation.
|
|
|
|
ListenerList m_cache_copy;
|
|
|
|
bool m_cache_valid;
|
2010-01-08 23:33:15 +00:00
|
|
|
|
|
|
|
Threading::Mutex m_listeners_lock;
|
2009-10-07 19:20:11 +00:00
|
|
|
|
|
|
|
public:
|
2010-01-22 15:22:01 +00:00
|
|
|
EventSource()
|
2009-10-23 20:24:59 +00:00
|
|
|
{
|
2010-01-22 15:22:01 +00:00
|
|
|
m_cache_valid = false;
|
2009-10-23 20:24:59 +00:00
|
|
|
}
|
|
|
|
|
2009-10-07 19:20:11 +00:00
|
|
|
virtual ~EventSource() throw() {}
|
|
|
|
|
2010-01-22 15:22:01 +00:00
|
|
|
virtual ListenerIterator Add( ListenerType& listener );
|
|
|
|
virtual void Remove( ListenerType& listener );
|
|
|
|
virtual void Remove( const ListenerIterator& listenerHandle );
|
|
|
|
|
|
|
|
void Add( ListenerType* listener )
|
|
|
|
{
|
|
|
|
if( listener == NULL ) return;
|
|
|
|
Add( *listener );
|
|
|
|
}
|
|
|
|
|
|
|
|
void Remove( ListenerType* listener )
|
|
|
|
{
|
|
|
|
if( listener == NULL ) return;
|
|
|
|
Remove( *listener );
|
|
|
|
}
|
2009-10-07 19:20:11 +00:00
|
|
|
|
2010-01-22 15:22:01 +00:00
|
|
|
void Dispatch( const EvtParams& params );
|
2009-10-23 20:24:59 +00:00
|
|
|
|
|
|
|
protected:
|
2010-01-22 15:22:01 +00:00
|
|
|
virtual ListenerIterator _AddFast_without_lock( ListenerType& listener );
|
|
|
|
virtual void _DispatchRaw( ListenerIterator iter, const ListenerIterator& iend, const EvtParams& params );
|
2009-10-07 19:20:11 +00:00
|
|
|
};
|
|
|
|
|
2010-01-22 15:22:01 +00:00
|
|
|
// --------------------------------------------------------------------------------------
|
|
|
|
// IEventDispatcher
|
|
|
|
// --------------------------------------------------------------------------------------
|
|
|
|
// This class is used as a base interface for EventListeners. It allows the listeners to do
|
|
|
|
// customized dispatching of several event types into "user friendly" function overrides.
|
|
|
|
//
|
|
|
|
template< typename EvtParams >
|
|
|
|
class IEventDispatcher
|
|
|
|
{
|
|
|
|
protected:
|
|
|
|
IEventDispatcher() {}
|
2009-10-07 19:20:11 +00:00
|
|
|
|
2010-01-22 15:22:01 +00:00
|
|
|
public:
|
|
|
|
virtual ~IEventDispatcher() throw() {}
|
|
|
|
virtual void DispatchEvent( const EvtParams& params )=0;
|
|
|
|
};
|