Flags.hx 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. package cs;
  2. /**
  3. Use this type to have access to the bitwise operators of C# enums that have a `cs.system.FlagsAttribute` attribute.
  4. Usage example:
  5. ```haxe
  6. import cs.system.reflection.BindingFlags;
  7. var binding = new Flags(BindingFlags.Public) | BindingFlags.Static | BindingFlags.NonPublic;
  8. ```
  9. **/
  10. abstract Flags<T : EnumValue>(T) from T to T
  11. {
  12. /**
  13. Creates a new `Flags` type with an optional initial value. If no initial value was specified,
  14. the default enum value for an empty flags attribute is specified
  15. **/
  16. @:extern inline public function new(?initial:T)
  17. this = initial;
  18. /**
  19. Accessible through the bitwise OR operator (`|`). Returns a new `Flags` type with the flags
  20. passed at `flags` added to it.
  21. **/
  22. @:op(A|B) @:extern inline public function add(flags:Flags<T>):Flags<T>
  23. {
  24. return new Flags(underlying() | flags.underlying());
  25. }
  26. /**
  27. Accessible through the bitwise AND operator (`&`). Returns a new `Flags` type with
  28. the flags that are set on both `this` and `flags`
  29. **/
  30. @:op(A&B) @:extern inline public function bitAnd(flags:Flags<T>):Flags<T>
  31. {
  32. return new Flags(underlying() & flags.underlying());
  33. }
  34. /**
  35. Accessible through the bitwise XOR operator (`^`).
  36. **/
  37. @:op(A^B) @:extern inline public function bitXor(flags:Flags<T>):Flags<T>
  38. {
  39. return new Flags(underlying() & flags.underlying());
  40. }
  41. /**
  42. Accesible through the bitwise negation operator (`~`). Returns a new `Flags` type
  43. with all unset flags as set - but the ones that are set already.
  44. **/
  45. @:op(~A) @:extern inline public function bitNeg():Flags<T>
  46. {
  47. return new Flags(~underlying());
  48. }
  49. /**
  50. Returns a new `Flags` type with all flags set by `flags` unset
  51. **/
  52. @:extern inline public function remove(flags:Flags<T>):Flags<T>
  53. {
  54. return new Flags(underlying() & ~flags.underlying());
  55. }
  56. /**
  57. Returns whether `flag` is present on `this` type
  58. **/
  59. @:extern inline public function has(flag:T):Bool
  60. {
  61. return underlying() & new Flags(flag).underlying() != null;
  62. }
  63. /**
  64. Returns whether `this` type has any flag set by `flags` also set
  65. **/
  66. @:extern inline public function hasAny(flags:Flags<T>):Bool
  67. {
  68. return underlying() & flags.underlying() != null;
  69. }
  70. /**
  71. Returns whether `this` type has all flags set by `flags` also set
  72. **/
  73. @:extern inline public function hasAll(flags:Flags<T>):Bool
  74. {
  75. return underlying() & flags.underlying() == flags.underlying();
  76. }
  77. @:extern inline private function underlying():EnumUnderlying<T>
  78. return this;
  79. }
  80. @:coreType private abstract EnumUnderlying<T> from T to T
  81. {
  82. @:op(A|B) public static function or<T>(lhs:EnumUnderlying<T>, rhs:EnumUnderlying<T>):T;
  83. @:op(A^B) public static function xor<T>(lhs:EnumUnderlying<T>, rhs:EnumUnderlying<T>):T;
  84. @:op(A&B) public static function and<T>(lhs:EnumUnderlying<T>, rhs:EnumUnderlying<T>):T;
  85. @:op(~A) public static function bneg<T>(t:EnumUnderlying<T>):T;
  86. }