FireBreath  1.4.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Pages
PluginEventSource.cpp
1 /**********************************************************\
2 Original Author: Richard Bateman (taxilian)
3 
4 Created: Nov 24, 2009
5 License: Dual license model; choose one of two:
6  New BSD License
7  http://www.opensource.org/licenses/bsd-license.php
8  - or -
9  GNU Lesser General Public License, version 2.1
10  http://www.gnu.org/licenses/lgpl-2.1.html
11 
12 Copyright 2009 PacketPass, Inc and the Firebreath development team
13 \**********************************************************/
14 
15 #include <algorithm>
16 #include "PluginEvent.h"
17 #include "PluginEventSink.h"
18 #include "PluginEventSource.h"
19 #include "PluginEvents/AttachedEvent.h"
20 #include "precompiled_headers.h" // On windows, everything above this line in PCH
21 
22 using namespace FB;
23 
24 PluginEventSource::PluginEventSource()
25 {
26 }
27 
28 PluginEventSource::~PluginEventSource()
29 {
30 }
31 
33 {
34  AttachObserver(sink->shared_from_this());
35 }
36 
37 void PluginEventSource::AttachObserver( PluginEventSinkPtr sink )
38 {
39  boost::recursive_mutex::scoped_lock _l(m_observerLock);
40  m_observers.push_back(sink);
41  AttachedEvent newEvent;
42  sink->HandleEvent(&newEvent, this);
43 }
44 
46 {
47  DetachObserver(sink->shared_from_this());
48 }
49 
50 void PluginEventSource::DetachObserver( PluginEventSinkPtr sink )
51 {
52  boost::recursive_mutex::scoped_lock _l(m_observerLock);
53 
54  std::list<PluginEventSinkPtr> detachedList;
55  {
56  std::list<PluginEventSinkWeakPtr>::iterator end = m_observers.end();
57  for (std::list<PluginEventSinkWeakPtr>::iterator it = m_observers.begin(); it != end; ) {
58  PluginEventSinkPtr ptr(it->lock());
59  if (!ptr || sink == ptr) {
60  it = m_observers.erase(it);
61  if (ptr)
62  detachedList.push_back(ptr);
63  } else {
64  ++it;
65  }
66  }
67  }
68 
69  std::list<PluginEventSinkPtr>::iterator end = detachedList.end();
70  DetachedEvent evt;
71  for (std::list<PluginEventSinkPtr>::iterator it = detachedList.begin(); it != end; ++it) {
72  (*it)->HandleEvent(&evt, this);
73  }
74 }
75 
77 {
78  boost::recursive_mutex::scoped_lock _l(m_observerLock);
79 
80  // Sometimes the events cause an observer to be removed; we make a copy so that
81  // it doesn't mess with our iterator. Remember that removing an observer will only take
82  // affect on the next SendEvent call
83  ObserverMap copy(m_observers);
84  ObserverMap::iterator it = copy.begin();
85 
86  while (it != copy.end()) {
87  PluginEventSinkPtr tmp = it->lock();
88  if (tmp && tmp->HandleEvent(evt, this)) {
89  return true; // Tell the caller that the event was handled
90  }
91  ++it;
92  }
93  return false;
94 }
95 
virtual void AttachObserver(PluginEventSink *sink)
Attach a PluginEventSink to receive events.
boost::recursive_mutex m_observerLock
List of attached observers.
Plugin event sink; all objects that can receive PluginEvent s should inherit from this class...
std::list< PluginEventSinkWeakPtr > ObserverMap
Defines an alias representing the observer .
virtual void DetachObserver(PluginEventSink *sink)
Detach a PluginEventSink so it no longer receives events from this source.
Fired when a PluginEventSink is attached to a PluginEventSource (such as a PluginCore derived plugin ...
Definition: AttachedEvent.h:29
Plugin event base class.
Definition: PluginEvent.h:36
Fired when a PluginEventSink is detached from a PluginEventSource (such as a PluginCore derived plugi...
Definition: AttachedEvent.h:39
virtual bool SendEvent(PluginEvent *evt)
Sends an event to all attached sinks.