Last active
September 3, 2023 12:18
-
-
Save back2dos/c9410ed3ed476ecc1007 to your computer and use it in GitHub Desktop.
Store all class names for runtime use.
This file contains 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 ; | |
#if macro | |
import haxe.macro.Context.*; | |
using haxe.macro.Tools; | |
#end | |
class ClassList { | |
static var NAME = 'ClassList';//must be the fully qualified name of this class | |
macro static function build() { | |
onGenerate(function (types) { | |
var names = [], | |
self = getType(NAME).getClass(); | |
for (t in types) | |
switch t { | |
case TInst(_.get() => c, _): | |
names.push(makeExpr(c.name, c.pos)); | |
default: | |
} | |
self.meta.remove('classes'); | |
self.meta.add('classes', names, self.pos); | |
}); | |
return macro cast haxe.rtti.Meta.getType($p{NAME.split('.')}); | |
} | |
#if !macro | |
static public var ALL_NAMES(default, null):Array<String> = build(); | |
#end | |
} |
This file contains 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 ; | |
class Usage { | |
static function main() { | |
trace(ClassList.ALL_NAMES); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment