Selaa lähdekoodia

haxe.EnumFlags.setTo (closes #10473)

Aleksandr Kuzmenko 3 vuotta sitten
vanhempi
commit
3fac24b5e2
2 muutettua tiedostoa jossa 26 lisäystä ja 0 poistoa
  1. 16 0
      std/haxe/EnumFlags.hx
  2. 10 0
      tests/unit/src/unitstd/haxe/EnumFlags.unit.hx

+ 16 - 0
std/haxe/EnumFlags.hx

@@ -76,6 +76,22 @@ abstract EnumFlags<T:EnumValue>(Int) {
 		this &= 0xFFFFFFFF - (1 << Type.enumIndex(v));
 	}
 
+	/**
+		Depending on the value of `condition` sets (`condition=true`) or unsets (`condition=false`)
+		the index of enum instance `v`.
+
+		This method is optimized if `v` is an enum instance expression such as
+		`SomeEnum.SomeCtor`.
+
+		If `v` is `null`, the result is unspecified.
+	**/
+	public inline function setTo(v:T, condition:Bool):Void {
+		if(condition)
+			set(v)
+		else
+			unset(v);
+	}
+
 	/**
 		Convert a integer bitflag into a typed one (this is a no-op, it does not
 		have any impact on speed).

+ 10 - 0
tests/unit/src/unitstd/haxe/EnumFlags.unit.hx

@@ -42,6 +42,7 @@ flags.has(EA) == false;
 flags.has(EB) == true;
 flags.has(EC) == false;
 
+
 // Big Enum (32)
 var bigFlags = new haxe.EnumFlags(1<<31);
 bigFlags.has( EF_31 ) == true;
@@ -49,3 +50,12 @@ bigFlags.unset( EF_31 );
 bigFlags.has( EF_31 ) == false;
 bigFlags.set( EF_31 );
 bigFlags.has( EF_31 ) == true;
+
+// setTo
+var flags = new haxe.EnumFlags();
+flags.setTo(EB, true);
+flags.has(EB) == true;
+flags.setTo(EB, false);
+flags.has(EB) == false;
+flags.setTo(EB, true);
+flags.has(EB) == true;