Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Proper way of raising events from C++/CLI

By: a guest on Feb 26th, 2012  |  syntax: None  |  size: 1.98 KB  |  views: 20  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. public delegate void f(int);
  2.  
  3. public ref struct E {
  4.    f ^ _E;
  5. public:
  6.    void handler(int i) {
  7.       System::Console::WriteLine(i);
  8.    }
  9.  
  10.    E() {
  11.       _E = nullptr;
  12.    }
  13.  
  14.    event f^ Event {
  15.       void add(f ^ d) {
  16.          _E += d;
  17.       }
  18.       void remove(f ^ d) {
  19.         _E -= d;
  20.       }
  21.       void raise(int i) {
  22.          f^ tmp = _E;
  23.          if (tmp) {
  24.             tmp->Invoke(i);
  25.          }
  26.       }
  27.    }
  28.  
  29.    static void Go() {
  30.       E^ pE = gcnew E;
  31.       pE->Event += gcnew f(pE, &E::handler);
  32.       pE->Event(17);
  33.    }
  34. };
  35.  
  36. int main() {
  37.    E::Go();
  38. }
  39.        
  40. public ref class MyClass
  41. {
  42. public:
  43.     event System::EventHandler ^ MyEvent;
  44. };
  45.        
  46. public class MyClass
  47. {
  48.     // Fields
  49.     private EventHandler <backing_store>MyEvent;
  50.  
  51.     // Events
  52.     public event EventHandler MyEvent
  53.     {
  54.         [MethodImpl(MethodImplOptions.Synchronized)] add
  55.         {
  56.             this.<backing_store>MyEvent = (EventHandler) Delegate.Combine(this.<backing_store>MyEvent, value);
  57.         }
  58.         [MethodImpl(MethodImplOptions.Synchronized)] remove
  59.         {
  60.             this.<backing_store>MyEvent = (EventHandler) Delegate.Remove(this.<backing_store>MyEvent, value);
  61.         }
  62.         raise
  63.         {
  64.             EventHandler <tmp> = null;
  65.             <tmp> = this.<backing_store>MyEvent;
  66.             if (<tmp> != null)
  67.             {
  68.                 <tmp>(value0, value1);
  69.             }
  70.         }
  71.     }
  72. }
  73.        
  74. // event keyword introduces the scope wherein I'm defining the required methods
  75. // "f" is my delegate type
  76. // "Event" is the unrealistic name of the event itself
  77. event f^ Event
  78. {
  79.       // add is public (because the event block is public)
  80.       // "_E" is the private delegate data member of type "f"
  81.       void add(f ^ d) { _E += d; }
  82.  
  83.    // making remove private
  84.    private:
  85.       void remove(f ^ d) { _E -= d; }
  86.  
  87.    // making raise protected
  88.    protected:
  89.       void raise(int i)
  90.       {
  91.          // check for nullptr
  92.          if (_E)
  93.          {
  94.             _E->Invoke(i);
  95.          }
  96.       }
  97. }// end event block