Last active
August 18, 2016 12:26
-
-
Save T1mL3arn/a24559fd905267bbe62ffe9246a70b4f to your computer and use it in GitHub Desktop.
Macro to generate static fields with CbType's from enum
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package; | |
enum CbTypeAlias | |
{ | |
PLAYER; ENEMY; ITEM; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package; | |
@:build(DefaultCbTypeBuilder.build()) | |
class DefaultCbType {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It works for pure haxe flash target. But not for OpenFl flash target (and even html5).
It prints
Fixed by wrapping
#if !macro
for nape imports inDefaultCbTypeBuilder