FireBreath  1.4.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Pages
PluginEventSource.h
1 /**********************************************************\
2 Original Author: Richard Bateman (taxilian)
3 
4 Created: Oct 19, 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 #pragma once
16 #ifndef H_FB_PLUGINEVENTSOURCE
17 #define H_FB_PLUGINEVENTSOURCE
18 
19 #include <list>
20 #include <typeinfo>
21 #include "APITypes.h"
22 #include <boost/enable_shared_from_this.hpp>
23 #include <boost/thread/recursive_mutex.hpp>
24 #include <boost/noncopyable.hpp>
25 
26 namespace FB {
27 
28  FB_FORWARD_PTR(PluginEventSink);
29  FB_FORWARD_PTR(PluginEventSource);
30  class PluginEvent;
31 
38  class PluginEventSource : public boost::enable_shared_from_this<PluginEventSource>, boost::noncopyable
39  {
40  public:
42  virtual ~PluginEventSource();
43 
51  virtual void AttachObserver(PluginEventSink* sink);
52  virtual void AttachObserver(PluginEventSinkPtr sink);
53 
61  virtual void DetachObserver(PluginEventSink* sink);
62  virtual void DetachObserver(PluginEventSinkPtr sink);
63 
73  virtual bool SendEvent(PluginEvent* evt);
74 
88  template <class T>
89  T* get_as()
90  {
91  T* tmp = dynamic_cast<T*>(this);
92  if (!tmp)
93  throw std::bad_cast();
94  return tmp;
95  }
96 
104  template<class T>
105  bool validType()
106  {
107  T* out(dynamic_cast<T*>(this));
108  return out != NULL;
109  }
110 
111  private:
112  // Helper function used by DetachObserver
113  bool _deleteObserver( PluginEventSinkPtr sink, PluginEventSinkWeakPtr wptr );
114 
115  protected:
116 
122  typedef std::list<PluginEventSinkWeakPtr> ObserverMap;
123  ObserverMap m_observers;
124  boost::recursive_mutex m_observerLock;
125  };
126 };
127 
128 #endif
129 
virtual void AttachObserver(PluginEventSink *sink)
Attach a PluginEventSink to receive events.
bool validType()
Returns true if the object can be cast to the specified type.
boost::recursive_mutex m_observerLock
List of attached observers.
T * get_as()
Templated convenience function for performing a dynamic cast of this object.
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.
Plugin event base class.
Definition: PluginEvent.h:36
virtual bool SendEvent(PluginEvent *evt)
Sends an event to all attached sinks.
Base class for any object, such as BrowserStream or PluginWindow, that needs to fire events to a Plug...