2009-10-07 19:20:11 +00:00
|
|
|
/* PCSX2 - PS2 Emulator for PCs
|
2010-05-03 14:08:02 +00:00
|
|
|
* Copyright (C) 2002-2010 PCSX2 Dev Team
|
2009-10-07 19:20:11 +00:00
|
|
|
*
|
|
|
|
* 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 >
|
|
|
|
// --------------------------------------------------------------------------------------
|
|
|
|
|
2016-11-12 15:28:37 +00:00
|
|
|
template <typename ListenerType>
|
2009-10-07 19:20:11 +00:00
|
|
|
class EventSource
|
|
|
|
{
|
|
|
|
public:
|
2016-11-12 15:28:37 +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:
|
2016-11-12 15:28:37 +00:00
|
|
|
typedef typename ListenerList::const_iterator ConstIterator;
|
2009-10-23 20:24:59 +00:00
|
|
|
|
2016-11-12 15:28:37 +00:00
|
|
|
ListenerList m_listeners;
|
2009-10-23 21:05:32 +00:00
|
|
|
|
2016-11-12 15:28:37 +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-04-25 00:31:27 +00:00
|
|
|
|
2016-11-12 15:28:37 +00:00
|
|
|
Threading::Mutex m_listeners_lock;
|
2009-10-07 19:20:11 +00:00
|
|
|
|
|
|
|
public:
|
2016-11-12 15:28:37 +00:00
|
|
|
EventSource()
|
|
|
|
{
|
|
|
|
m_cache_valid = false;
|
|
|
|
}
|
2009-10-23 20:24:59 +00:00
|
|
|
|
2016-11-12 15:28:37 +00:00
|
|
|
virtual ~EventSource() throw() {}
|
2009-10-07 19:20:11 +00:00
|
|
|
|
2016-11-12 15:28:37 +00:00
|
|
|
virtual ListenerIterator Add(ListenerType &listener);
|
|
|
|
virtual void Remove(ListenerType &listener);
|
|
|
|
virtual void Remove(const ListenerIterator &listenerHandle);
|
2010-04-25 00:31:27 +00:00
|
|
|
|
2016-11-12 15:28:37 +00:00
|
|
|
void Add(ListenerType *listener)
|
|
|
|
{
|
|
|
|
if (listener == NULL)
|
|
|
|
return;
|
|
|
|
Add(*listener);
|
|
|
|
}
|
2010-04-25 00:31:27 +00:00
|
|
|
|
2016-11-12 15:28:37 +00:00
|
|
|
void Remove(ListenerType *listener)
|
|
|
|
{
|
|
|
|
if (listener == NULL)
|
|
|
|
return;
|
|
|
|
Remove(*listener);
|
|
|
|
}
|
2009-10-07 19:20:11 +00:00
|
|
|
|
2016-11-12 15:28:37 +00:00
|
|
|
void Dispatch(const EvtParams ¶ms);
|
2009-10-23 20:24:59 +00:00
|
|
|
|
|
|
|
protected:
|
2016-11-12 15:28:37 +00:00
|
|
|
virtual ListenerIterator _AddFast_without_lock(ListenerType &listener);
|
|
|
|
virtual void _DispatchRaw(ListenerIterator iter, const ListenerIterator &iend, const EvtParams ¶ms);
|
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.
|
|
|
|
//
|
2016-11-12 15:28:37 +00:00
|
|
|
template <typename EvtParams>
|
2010-01-22 15:22:01 +00:00
|
|
|
class IEventDispatcher
|
|
|
|
{
|
|
|
|
protected:
|
2016-11-12 15:28:37 +00:00
|
|
|
IEventDispatcher() {}
|
2009-10-07 19:20:11 +00:00
|
|
|
|
2010-01-22 15:22:01 +00:00
|
|
|
public:
|
2016-11-12 15:28:37 +00:00
|
|
|
virtual ~IEventDispatcher() throw() {}
|
|
|
|
virtual void DispatchEvent(const EvtParams ¶ms) = 0;
|
2010-01-22 15:22:01 +00:00
|
|
|
};
|