package; import haxe.macro.Expr; import haxe.macro.Context; import haxe.macro.Type; import haxe.macro.TypeTools; class DisplaylistMacroGeneric { public static var cache = new Map(); static public function build(available:String):ComplexType { trace("DisplaylistMacroGeneric:",Context.getLocalType()); switch ( Context.getLocalType() ) { case TInst(_,[TInst(_.get() => { kind: KExpr(macro $v{(i:String)}) },_)]): return buildClass(available, i); case t: Context.error("Class expected", Context.currentPos()); return null; } } static public function buildClass(available:String, used:String):ComplexType { var className = 'Displaylist_' + used; var avail:Array = available.split(","); var error:Bool = false; if (!cache.exists(className)) { trace("build new Class:"+className+"--------------"); var fields = Context.getBuildFields(); var myFunc:Function = { expr: Context.parse('return new Element<"$used">()', Context.currentPos()), ret: TPath({ name:'Element', pack: [], params: [ TPExpr({ expr: EConst(CString(used)), pos: Context.currentPos() })] }), args:[] } var f:Field = { name: "addElement", access: [Access.APublic], kind: FieldType.FFun(myFunc), pos: Context.currentPos(), }; fields.push(f); // create new class var typeDefinition:TypeDefinition = { pos : Context.currentPos(), pack : [], //package??? name : className, kind: TDClass(), fields:fields, params: [], } Context.defineType(typeDefinition); cache[className] = true; } return TPath({ pack:[], name:className, params:[] }); } }