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