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