1 /* 2 Derived from source code of TrueCrypt 7.1a, which is 3 Copyright (c) 2008-2012 TrueCrypt Developers Association and which is governed 4 by the TrueCrypt License 3.0. 5 6 Modifications and additions to the original source code (contained in this file) 7 and all other portions of this file are Copyright (c) 2013-2017 IDRIX 8 and are governed by the Apache License 2.0 the full text of which is 9 contained in the file License.txt included in VeraCrypt binary and source 10 code distribution packages. 11 */ 12 13 #ifndef TC_HEADER_Platform_Event 14 #define TC_HEADER_Platform_Event 15 16 #include "PlatformBase.h" 17 #include "ForEach.h" 18 #include "Mutex.h" 19 #include "SharedPtr.h" 20 21 namespace VeraCrypt 22 { 23 struct EventArgs 24 { 25 virtual ~EventArgs () { } 26 }; 27 28 class EventConnectorBase 29 { 30 public: 31 virtual ~EventConnectorBase () { } 32 virtual void operator() (EventArgs &args) = 0; 33 34 virtual EventConnectorBase *CloneNew () const = 0; 35 virtual void *GetHandler () const = 0; 36 }; 37 38 typedef list < shared_ptr <EventConnectorBase> > EventHandlerList; 39 40 template <class T> 41 class EventConnector : public EventConnectorBase 42 { 43 public: 44 typedef void (T::*EventHandlerFunction) (EventArgs &); 45 46 EventConnector (T *handler, EventHandlerFunction function) 47 : Handler (handler), Function (function) { } 48 49 virtual void operator() (EventArgs &args) { (Handler->*Function) (args); } 50 51 virtual EventConnectorBase *CloneNew () const { return new EventConnector <T> (*this); } 52 virtual void *GetHandler () const { return Handler; } 53 54 protected: 55 T *Handler; 56 EventHandlerFunction Function; 57 }; 58 59 class Event 60 { 61 public: 62 Event () { } 63 virtual ~Event () { } 64 65 void Connect (const EventConnectorBase &connector); 66 void Disconnect (void *handler); 67 void Raise (); 68 void Raise (EventArgs &args); 69 70 protected: 71 EventHandlerList ConnectedHandlers; 72 Mutex HandlersMutex; 73 74 private: 75 Event (const Event &); 76 Event &operator= (const Event &); 77 }; 78 79 struct ExceptionEventArgs : public EventArgs 80 { 81 ExceptionEventArgs (exception &ex) : mException (ex) { } 82 exception &mException; 83 84 private: 85 ExceptionEventArgs (const ExceptionEventArgs &); 86 ExceptionEventArgs &operator= (const ExceptionEventArgs &); 87 }; 88 } 89 90 #endif // TC_HEADER_Platform_Event