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>
|
2009-10-23 20:24:59 +00:00
|
|
|
|
|
|
|
class wxCommandEvent;
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------------------
|
|
|
|
// EventListener< typename EvtType >
|
|
|
|
// --------------------------------------------------------------------------------------
|
2009-10-07 19:20:11 +00:00
|
|
|
|
|
|
|
template< typename EvtType >
|
|
|
|
struct EventListener
|
|
|
|
{
|
2009-10-23 20:24:59 +00:00
|
|
|
typedef void __fastcall FuncType( void* object, EvtType& evt );
|
2009-10-07 19:20:11 +00:00
|
|
|
|
|
|
|
void* object;
|
|
|
|
FuncType* OnEvent;
|
2009-10-07 20:50:39 +00:00
|
|
|
|
2009-10-07 19:20:11 +00:00
|
|
|
EventListener( FuncType* fnptr )
|
|
|
|
{
|
|
|
|
object = NULL;
|
|
|
|
OnEvent = fnptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
EventListener( void* objhandle, FuncType* fnptr )
|
|
|
|
{
|
|
|
|
object = objhandle;
|
|
|
|
OnEvent = fnptr;
|
|
|
|
}
|
2009-10-07 20:50:39 +00:00
|
|
|
|
2009-10-07 19:20:11 +00:00
|
|
|
bool operator ==( const EventListener& right ) const
|
|
|
|
{
|
|
|
|
return (object == right.object) && (OnEvent == right.OnEvent);
|
|
|
|
}
|
2009-10-07 20:50:39 +00:00
|
|
|
|
2009-10-07 19:20:11 +00:00
|
|
|
bool operator !=( const EventListener& right ) const
|
|
|
|
{
|
|
|
|
return this->operator ==(right);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2009-10-23 20:24:59 +00:00
|
|
|
// --------------------------------------------------------------------------------------
|
|
|
|
// EventSource< template EvtType >
|
|
|
|
// --------------------------------------------------------------------------------------
|
|
|
|
|
2009-10-07 19:20:11 +00:00
|
|
|
template< typename EvtType >
|
|
|
|
class EventSource
|
|
|
|
{
|
|
|
|
public:
|
2009-10-07 20:50:39 +00:00
|
|
|
typedef EventListener< EvtType > ListenerType;
|
2009-10-07 19:20:11 +00:00
|
|
|
typedef typename std::list< ListenerType > ListenerList;
|
2009-10-07 20:50:39 +00:00
|
|
|
typedef typename ListenerList::iterator Handle;
|
|
|
|
|
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-07 19:20:11 +00:00
|
|
|
ListenerList m_listeners;
|
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;
|
2009-10-07 19:20:11 +00:00
|
|
|
|
|
|
|
public:
|
2009-10-23 20:24:59 +00:00
|
|
|
EventSource() : m_cache_valid( false )
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2009-10-07 19:20:11 +00:00
|
|
|
virtual ~EventSource() throw() {}
|
|
|
|
|
|
|
|
virtual void Remove( const ListenerType& listener )
|
|
|
|
{
|
2009-10-23 20:24:59 +00:00
|
|
|
m_cache_valid = false;
|
2009-10-07 19:20:11 +00:00
|
|
|
m_listeners.remove( listener );
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void Remove( const Handle& listenerHandle )
|
|
|
|
{
|
2009-10-23 20:24:59 +00:00
|
|
|
m_cache_valid = false;
|
2009-10-07 19:20:11 +00:00
|
|
|
m_listeners.erase( listenerHandle );
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual Handle AddFast( const ListenerType& listener )
|
|
|
|
{
|
2009-10-23 20:24:59 +00:00
|
|
|
m_cache_valid = false;
|
2009-10-07 19:20:11 +00:00
|
|
|
m_listeners.push_front( listener );
|
|
|
|
return m_listeners.begin();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Checks for duplicates before adding the event.
|
|
|
|
virtual inline void Add( const ListenerType& listener );
|
|
|
|
virtual inline void RemoveObject( const void* object );
|
|
|
|
|
2009-10-09 15:17:53 +00:00
|
|
|
void Add( void* objhandle, typename ListenerType::FuncType* fnptr )
|
2009-10-07 19:20:11 +00:00
|
|
|
{
|
2009-10-09 15:17:53 +00:00
|
|
|
Add( ListenerType( objhandle, fnptr ) );
|
2009-10-07 19:20:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Remove( void* objhandle, typename ListenerType::FuncType* fnptr )
|
|
|
|
{
|
2009-10-09 15:17:53 +00:00
|
|
|
Remove( ListenerType( objhandle, fnptr ) );
|
2009-10-07 19:20:11 +00:00
|
|
|
}
|
|
|
|
|
2009-10-23 20:24:59 +00:00
|
|
|
void Dispatch( EvtType& evt )
|
2009-10-07 19:20:11 +00:00
|
|
|
{
|
2009-10-23 20:24:59 +00:00
|
|
|
if( !m_cache_valid )
|
|
|
|
{
|
|
|
|
m_cache_copy = m_listeners;
|
|
|
|
m_cache_valid = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
_DispatchRaw( m_cache_copy.begin(), m_cache_copy.end(), evt );
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
__forceinline void _DispatchRaw( ConstIterator& iter, const ConstIterator& iend, EvtType& evt )
|
|
|
|
{
|
|
|
|
while( iter != iend )
|
2009-10-07 19:20:11 +00:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
iter->OnEvent( iter->object, evt );
|
|
|
|
}
|
|
|
|
catch( Exception::RuntimeError& ex )
|
|
|
|
{
|
2009-10-07 20:50:39 +00:00
|
|
|
Console.Error( L"Ignoring runtime error thrown from event listener: " + ex.FormatDiagnosticMessage() );
|
2009-10-07 19:20:11 +00:00
|
|
|
}
|
2009-10-07 20:50:39 +00:00
|
|
|
catch( Exception::BaseException& ex )
|
2009-10-07 19:20:11 +00:00
|
|
|
{
|
2009-10-09 15:17:53 +00:00
|
|
|
if( IsDevBuild ) throw;
|
|
|
|
Console.Error( L"Ignoring non-runtime BaseException thrown from event listener: " + ex.FormatDiagnosticMessage() );
|
2009-10-07 19:20:11 +00:00
|
|
|
}
|
2009-10-09 15:17:53 +00:00
|
|
|
++iter;
|
2009-10-07 19:20:11 +00:00
|
|
|
}
|
|
|
|
}
|
2009-10-23 20:24:59 +00:00
|
|
|
|
2009-10-07 19:20:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------------------
|
2009-10-23 20:24:59 +00:00
|
|
|
// EventListenerBinding< typename EvtType ?
|
2009-10-07 19:20:11 +00:00
|
|
|
// --------------------------------------------------------------------------------------
|
|
|
|
// Encapsulated event listener binding, provides the "benefits" of object unwinding.
|
|
|
|
//
|
2009-10-23 20:24:59 +00:00
|
|
|
template< typename EvtType >
|
2009-10-07 19:20:11 +00:00
|
|
|
class EventListenerBinding
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
typedef typename EventSource<EvtType>::ListenerType ListenerHandle;
|
|
|
|
typedef typename EventSource<EvtType>::Handle ConstIterator;
|
|
|
|
|
|
|
|
protected:
|
2009-10-07 20:50:39 +00:00
|
|
|
EventSource<EvtType>& m_source;
|
|
|
|
const ListenerHandle m_listener;
|
|
|
|
ConstIterator m_iter;
|
|
|
|
bool m_attached;
|
2009-10-07 19:20:11 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
EventListenerBinding( EventSource<EvtType>& source, const ListenerHandle& listener, bool autoAttach=true ) :
|
|
|
|
m_source( source )
|
|
|
|
, m_listener( listener )
|
|
|
|
, m_attached( false )
|
|
|
|
{
|
|
|
|
// If you want to assert on null pointers, you'll need to do the check yourself. There's
|
|
|
|
// too many cases where silently ignoring null pointers is the desired behavior.
|
|
|
|
//if( !pxAssertDev( listener.OnEvent != NULL, "NULL listener callback function." ) ) return;
|
|
|
|
if( autoAttach ) Attach();
|
|
|
|
}
|
2009-10-07 20:50:39 +00:00
|
|
|
|
2009-10-07 19:20:11 +00:00
|
|
|
virtual ~EventListenerBinding() throw()
|
|
|
|
{
|
|
|
|
Detach();
|
|
|
|
}
|
2009-10-07 20:50:39 +00:00
|
|
|
|
2009-10-07 19:20:11 +00:00
|
|
|
void Detach()
|
|
|
|
{
|
|
|
|
if( !m_attached ) return;
|
|
|
|
m_source.Remove( m_iter );
|
|
|
|
m_attached = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Attach()
|
|
|
|
{
|
|
|
|
if( m_attached || (m_listener.OnEvent == NULL) ) return;
|
|
|
|
m_iter = m_source.AddFast( m_listener );
|
|
|
|
m_attached = true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef EventSource<wxCommandEvent> CmdEvt_Source;
|
|
|
|
typedef EventListener<wxCommandEvent> CmdEvt_Listener;
|
|
|
|
typedef EventListenerBinding<wxCommandEvent> CmdEvt_ListenerBinding;
|
|
|
|
|
|
|
|
|
|
|
|
// Checks for duplicates before adding the event.
|
|
|
|
template< typename EvtType >
|
|
|
|
void EventSource<EvtType>::Add( const ListenerType& listener )
|
|
|
|
{
|
|
|
|
if( !pxAssertDev( listener.OnEvent != NULL, "NULL listener callback function." ) ) return;
|
|
|
|
|
|
|
|
Handle iter = m_listeners.begin();
|
|
|
|
while( iter != m_listeners.end() )
|
|
|
|
{
|
|
|
|
if( *iter == listener ) return;
|
|
|
|
++iter;
|
|
|
|
}
|
|
|
|
AddFast( listener );
|
|
|
|
}
|
|
|
|
|
|
|
|
template< typename EvtType >
|
2009-10-07 20:50:39 +00:00
|
|
|
class PredicatesAreTheThingsOfNightmares
|
2009-10-07 19:20:11 +00:00
|
|
|
{
|
2009-10-07 20:50:39 +00:00
|
|
|
typedef EventListener< EvtType > ListenerType;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
const void* const m_object_match;
|
|
|
|
|
|
|
|
public:
|
|
|
|
PredicatesAreTheThingsOfNightmares( const void* objmatch ) : m_object_match( objmatch ) { }
|
|
|
|
|
|
|
|
bool operator()( const ListenerType& src ) const
|
2009-10-07 19:20:11 +00:00
|
|
|
{
|
2009-10-07 20:50:39 +00:00
|
|
|
return src.object == m_object_match;
|
|
|
|
}
|
|
|
|
};
|
2009-10-07 19:20:11 +00:00
|
|
|
|
2009-10-07 20:50:39 +00:00
|
|
|
// removes all listeners which reference the given object. Use for assuring object deletion.
|
|
|
|
template< typename EvtType >
|
|
|
|
void EventSource<EvtType>::RemoveObject( const void* object )
|
|
|
|
{
|
2009-10-23 20:24:59 +00:00
|
|
|
m_cache_valid = false;
|
2009-10-07 20:50:39 +00:00
|
|
|
m_listeners.remove_if( PredicatesAreTheThingsOfNightmares<EvtType>( object ) );
|
2009-10-07 19:20:11 +00:00
|
|
|
}
|
|
|
|
|