Skip to content

Instantly share code, notes, and snippets.

@back2dos
Last active August 29, 2015 14:04
Show Gist options
  • Select an option

  • Save back2dos/b31b0de7e37a4ac03baf to your computer and use it in GitHub Desktop.

Select an option

Save back2dos/b31b0de7e37a4ac03baf to your computer and use it in GitHub Desktop.
abstract Flags(Int) {
public inline function new(i = 0) {
this = i;
}
public inline function has( index : Int ) : Bool {
return this & (1 << index) != 0;
}
public inline function set( index: Int ) : Void {
this |= 1 << index;
}
public inline function unset( index: Int ) : Void {
this &= 0xFFFFFFF - (1 << index);
}
macro static public function make(exprs:Array<haxe.macro.Expr>) {
var ret = 0;
exprs.reverse();
for (e in exprs)
ret = (ret << 1) +
switch e {
case macro true, macro 1: 1;
case macro false, macro 0: 0;
default: haxe.macro.Context.error('should be either of true, false, 1 or 0', e.pos);
}
return macro new Flags($v{ret});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment