PropertyMacro.hx 958 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import haxe.macro.Context;
  2. import haxe.macro.Expr;
  3. class PropertyMacro {
  4. public static macro function addIntProperty(name:String):Array<Field> {
  5. final fields = Context.getBuildFields();
  6. final privateField:Field = {
  7. name: name,
  8. access: [APublic],
  9. meta: [{
  10. name: ":isVar",
  11. pos: Context.currentPos()
  12. }],
  13. kind: FProp("get", "private set", macro : Int, null),
  14. pos: Context.currentPos()
  15. };
  16. final getterMethod = {
  17. name: "get_" + name,
  18. access: [],
  19. kind: FFun({
  20. args: [],
  21. ret: macro : Int,
  22. expr: macro return this.$name
  23. }),
  24. pos: Context.currentPos()
  25. };
  26. final setterMethod = {
  27. name: "set_" + name,
  28. access: [],
  29. kind: FFun({
  30. args: [{ name: "value", type: macro : Int }],
  31. ret: macro : Int,
  32. expr: macro return this.$name = value
  33. }),
  34. pos: Context.currentPos()
  35. };
  36. fields.push(privateField);
  37. fields.push(getterMethod);
  38. fields.push(setterMethod);
  39. return fields;
  40. }
  41. }