-
Notifications
You must be signed in to change notification settings - Fork 4
/
Singleton.hx
39 lines (36 loc) · 1.27 KB
/
Singleton.hx
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
import haxe.macro.Context;
import haxe.macro.Expr;
#if !macro @:autoBuild(Singleton.build()) #end
class Singleton
{
macro static public function build():Array<Field>
{
var local = Context.getLocalClass().toString().split(".");
var pos = Context.currentPos();
var pack = local.splice(0, local.length - 1);
var clazz = local[0];
var type = TPath( { pack: pack , name: clazz, params: [], sub: null } );
var fields = Context.getBuildFields();
fields.push( { name: "instance", pos: pos, access: [AStatic, APrivate], kind: FVar(type, null) } );
fields.push( { name: "getInstance", doc: "Get the instance of this singleton", pos: pos, access: [AStatic, APublic], kind:
FFun({
args: [], params: [], ret: type,
expr: {
expr: {
EBlock([
{
expr: EIf(
{ expr: EBinop(OpEq, { expr: EConst(CIdent("instance")), pos: pos }, { expr: EConst(CIdent("null")), pos: pos } ), pos: pos },
{ expr: EBinop(OpAssign, { expr: EConst(CIdent("instance")), pos: pos }, { expr: ENew( { name: clazz, pack: pack, params: [] }, []), pos: pos } ), pos: pos }, null),
pos: pos
},
{ expr: EReturn( { expr: EConst(CIdent("instance")), pos: pos } ), pos: pos }
]);
},
pos: pos
}
})
});
return fields;
}
}