Skip to content

Instantly share code, notes, and snippets.

@T1mL3arn
Last active August 18, 2016 12:26
Show Gist options
  • Save T1mL3arn/a24559fd905267bbe62ffe9246a70b4f to your computer and use it in GitHub Desktop.
Save T1mL3arn/a24559fd905267bbe62ffe9246a70b4f to your computer and use it in GitHub Desktop.
Macro to generate static fields with CbType's from enum
package;
enum CbTypeAlias
{
PLAYER; ENEMY; ITEM;
}
package;
@:build(DefaultCbTypeBuilder.build())
class DefaultCbType {}
package;
import haxe.macro.Context;
import haxe.macro.Expr;
import haxe.macro.Expr.Access;
import haxe.macro.Expr.Field;
import haxe.macro.Expr.FieldType;
import haxe.macro.TypeTools;
import haxe.macro.ExprTools;
#if !macro
import nape.callbacks.CbType;
import DefaultCbType;
#end
using haxe.macro.ExprTools;
/**
* Macro to generate static fields with CbType's from enum
* @author T1mL3arn
*/
class DefaultCbTypeBuilder
{
static public function build()
{
var cbaliaslist = Type.getEnumConstructs(CbTypeAlias);
var fields:Array<Field> = Context.getBuildFields();
var fillExpr:Array<Expr> = [];
//CbType fields
for (alias in cbaliaslist)
{
alias = alias.toUpperCase();
var cbTypePath:TypePath = {name:"CbType", pack:["nape", "callbacks"]};
var exprDef:ExprDef = ENew(cbTypePath, []);
var cbExpr:Expr = {expr:exprDef, pos:Context.currentPos()};
fields.push({
name: alias,
doc: 'CbType for "${alias.toLowerCase()}" alias. (autogenerated)',
access: [Access.APublic, Access.AStatic],
kind: FieldType.FVar(TPath(cbTypePath), cbExpr),
pos: Context.currentPos()
});
fillExpr.push(macro cache['${alias.toLowerCase()}'] = DefaultCbType.$alias);
}
//building static method
var fillMethodBody:Expr = {expr:EBlock(fillExpr), pos:Context.currentPos()};
var cacheArg:FunctionArg = {name:"cache", opt:false, type:macro:Map<String, nape.callbacks.CbType>};
var voidType:ComplexType = TPath({name:"Void", pack:[]});
var fillGivenCacheFunction:Function = {args:[cacheArg], ret:voidType, expr:fillMethodBody};
fields.push({
name: "fillGivenCache",
doc: "Fill's the given cache by all defined CbType objects",
access: [Access.APublic, Access.AStatic],
kind: FieldType.FFun(fillGivenCacheFunction),
pos: Context.currentPos()
});
return fields;
}
}
// somewhere in Main.hx
function init()
{
trace("types from fields:", DefaultCbType.ENEMY, DefaultCbType.PLAYER, DefaultCbType.ITEM);
// prints: types from fields:,CbType#5,CbType#4,CbType#6
trace("types from cache:");
var cache = new Map<String, CbType>();
DefaultCbType.fillGivenCache(cache);
for (key in cache.keys())
trace(key, cache[key]);
//prints
// Main.hx:25: types from cache
// Main.hx:29: enemy,CbType#5
// Main.hx:29: player,CbType#4
// Main.hx:29: item,CbType#6
}
@T1mL3arn
Copy link
Author

T1mL3arn commented Aug 18, 2016

It works for pure haxe flash target. But not for OpenFl flash target (and even html5).
It prints

nape/2,0,20/zpp_nape/util/Debug.hx:432: characters 21-40 : You cannot access the flash package while in a macro (for flash.display.Shape)

Fixed by wrapping #if !macro for nape imports in DefaultCbTypeBuilder

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment