Flags.hx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * Copyright (C)2005-2017 Haxe Foundation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  20. * DEALINGS IN THE SOFTWARE.
  21. */
  22. package cs;
  23. /**
  24. Use this type to have access to the bitwise operators of C# enums that have a `cs.system.FlagsAttribute` attribute.
  25. Usage example:
  26. ```haxe
  27. import cs.system.reflection.BindingFlags;
  28. var binding = new Flags(BindingFlags.Public) | BindingFlags.Static | BindingFlags.NonPublic;
  29. ```
  30. **/
  31. abstract Flags<T : EnumValue>(T) from T to T
  32. {
  33. /**
  34. Creates a new `Flags` type with an optional initial value. If no initial value was specified,
  35. the default enum value for an empty flags attribute is specified
  36. **/
  37. @:extern inline public function new(?initial:T)
  38. this = initial;
  39. /**
  40. Accessible through the bitwise OR operator (`|`). Returns a new `Flags` type with the flags
  41. passed at `flags` added to it.
  42. **/
  43. @:op(A|B) @:extern inline public function add(flags:Flags<T>):Flags<T>
  44. {
  45. return new Flags(underlying() | flags.underlying());
  46. }
  47. /**
  48. Accessible through the bitwise AND operator (`&`). Returns a new `Flags` type with
  49. the flags that are set on both `this` and `flags`
  50. **/
  51. @:op(A&B) @:extern inline public function bitAnd(flags:Flags<T>):Flags<T>
  52. {
  53. return new Flags(underlying() & flags.underlying());
  54. }
  55. /**
  56. Accessible through the bitwise XOR operator (`^`).
  57. **/
  58. @:op(A^B) @:extern inline public function bitXor(flags:Flags<T>):Flags<T>
  59. {
  60. return new Flags(underlying() & flags.underlying());
  61. }
  62. /**
  63. Accesible through the bitwise negation operator (`~`). Returns a new `Flags` type
  64. with all unset flags as set - but the ones that are set already.
  65. **/
  66. @:op(~A) @:extern inline public function bitNeg():Flags<T>
  67. {
  68. return new Flags(~underlying());
  69. }
  70. /**
  71. Returns a new `Flags` type with all flags set by `flags` unset
  72. **/
  73. @:extern inline public function remove(flags:Flags<T>):Flags<T>
  74. {
  75. return new Flags(underlying() & ~flags.underlying());
  76. }
  77. /**
  78. Returns whether `flag` is present on `this` type
  79. **/
  80. @:extern inline public function has(flag:T):Bool
  81. {
  82. return underlying() & new Flags(flag).underlying() != null;
  83. }
  84. /**
  85. Returns whether `this` type has any flag set by `flags` also set
  86. **/
  87. @:extern inline public function hasAny(flags:Flags<T>):Bool
  88. {
  89. return underlying() & flags.underlying() != null;
  90. }
  91. /**
  92. Returns whether `this` type has all flags set by `flags` also set
  93. **/
  94. @:extern inline public function hasAll(flags:Flags<T>):Bool
  95. {
  96. return underlying() & flags.underlying() == flags.underlying();
  97. }
  98. @:extern inline private function underlying():EnumUnderlying<T>
  99. return this;
  100. }
  101. @:coreType private abstract EnumUnderlying<T> from T to T
  102. {
  103. @:op(A|B) public static function or<T>(lhs:EnumUnderlying<T>, rhs:EnumUnderlying<T>):T;
  104. @:op(A^B) public static function xor<T>(lhs:EnumUnderlying<T>, rhs:EnumUnderlying<T>):T;
  105. @:op(A&B) public static function and<T>(lhs:EnumUnderlying<T>, rhs:EnumUnderlying<T>):T;
  106. @:op(~A) public static function bneg<T>(t:EnumUnderlying<T>):T;
  107. }