1 module poison.core.action; 2 3 /// An action. 4 class Action { 5 private: 6 /// The function pointer. 7 void function() _f; 8 9 /// The delegate. 10 void delegate() _d; 11 12 public: 13 /** 14 * Creates a new instance of the action passing a function pointer. 15 * Params: 16 * f = The function pointer. 17 */ 18 this(void function() f) { 19 _f = f; 20 } 21 22 23 /** 24 * Creates a new instance of the action passing a delegate. 25 * Params: 26 * d = The delegate. 27 */ 28 this(void delegate() d) { 29 _d = d; 30 } 31 32 /// Operator overload for calling Action implicit. 33 void opCall() { 34 if (_f) { 35 _f(); 36 } 37 else if (_d) { 38 _d(); 39 } 40 } 41 } 42 43 /// An action that takes a generic argument. 44 class ActionArgs(T) { 45 private: 46 /// The function pointer. 47 void function(T) _f; 48 49 /// The delegate. 50 void delegate(T) _d; 51 52 public: 53 /** 54 * Creates a new instance of the action passing a function pointer. 55 * Params: 56 * f = The function pointer. 57 */ 58 this(void function(T) f) { 59 _f = f; 60 } 61 62 /** 63 * Creates a new instance of the action passing a delegate. 64 * Params: 65 * d = The delegate. 66 */ 67 this(void delegate(T) d) { 68 _d = d; 69 } 70 71 /** 72 * Operator overload for calling Action implicit. 73 * Params: 74 * arg = The argument to pass. 75 */ 76 void opCall(T arg) { 77 if (_f) { 78 _f(arg); 79 } 80 else if (_d) { 81 _d(arg); 82 } 83 } 84 }