Flags.hx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. * Copyright (C)2005-2019 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. Creates a new `Flags` type with an optional initial value. If no initial value was specified,
  34. the default enum value for an empty flags attribute is specified
  35. **/
  36. extern inline public function new(?initial:T)
  37. this = initial == null ? cast null : initial;
  38. /**
  39. Accessible through the bitwise OR operator (`|`). Returns a new `Flags` type with the flags
  40. passed at `flags` added to it.
  41. **/
  42. @:op(A | B) extern inline public function add(flags:Flags<T>):Flags<T> {
  43. return new Flags(underlying() | flags.underlying());
  44. }
  45. /**
  46. Accessible through the bitwise AND operator (`&`). Returns a new `Flags` type with
  47. the flags that are set on both `this` and `flags`
  48. **/
  49. @:op(A & B) extern inline public function bitAnd(flags:Flags<T>):Flags<T> {
  50. return new Flags(underlying() & flags.underlying());
  51. }
  52. /**
  53. Accessible through the bitwise XOR operator (`^`).
  54. **/
  55. @:op(A ^ B) extern inline public function bitXor(flags:Flags<T>):Flags<T> {
  56. return new Flags(underlying() & flags.underlying());
  57. }
  58. /**
  59. Accesible through the bitwise negation operator (`~`). Returns a new `Flags` type
  60. with all unset flags as set - but the ones that are set already.
  61. **/
  62. @:op(~A) extern inline public function bitNeg():Flags<T> {
  63. return new Flags(~underlying());
  64. }
  65. /**
  66. Returns a new `Flags` type with all flags set by `flags` unset
  67. **/
  68. extern inline public function remove(flags:Flags<T>):Flags<T> {
  69. return new Flags(underlying() & ~flags.underlying());
  70. }
  71. /**
  72. Returns whether `flag` is present on `this` type
  73. **/
  74. extern inline public function has(flag:T):Bool {
  75. return underlying() & new Flags(flag).underlying() != null;
  76. }
  77. /**
  78. Returns whether `this` type has any flag set by `flags` also set
  79. **/
  80. extern inline public function hasAny(flags:Flags<T>):Bool {
  81. return underlying() & flags.underlying() != null;
  82. }
  83. /**
  84. Returns whether `this` type has all flags set by `flags` also set
  85. **/
  86. extern inline public function hasAll(flags:Flags<T>):Bool {
  87. return underlying() & flags.underlying() == flags.underlying();
  88. }
  89. extern inline private function underlying():EnumUnderlying<T>
  90. return this;
  91. }
  92. @:coreType private abstract EnumUnderlying<T> from T to T {
  93. @:op(A | B) public static function or<T>(lhs:EnumUnderlying<T>, rhs:EnumUnderlying<T>):T;
  94. @:op(A ^ B) public static function xor<T>(lhs:EnumUnderlying<T>, rhs:EnumUnderlying<T>):T;
  95. @:op(A & B) public static function and<T>(lhs:EnumUnderlying<T>, rhs:EnumUnderlying<T>):T;
  96. @:op(~A) public static function bneg<T>(t:EnumUnderlying<T>):T;
  97. }