forked from mirror/abool
Add IsNotSet()
This commit is contained in:
parent
3a33536c80
commit
8ae5c93531
|
@ -17,6 +17,7 @@ cond := abool.New() // default to false
|
||||||
cond.Set() // Sets to true
|
cond.Set() // Sets to true
|
||||||
cond.IsSet() // Returns true
|
cond.IsSet() // Returns true
|
||||||
cond.UnSet() // Sets to false
|
cond.UnSet() // Sets to false
|
||||||
|
cond.IsNotSet() // Returns true
|
||||||
cond.SetTo(any) // Sets to whatever you want
|
cond.SetTo(any) // Sets to whatever you want
|
||||||
cond.SetToIf(new, old) // Sets to `new` only if the Boolean matches the `old`, returns whether succeeded
|
cond.SetToIf(new, old) // Sets to `new` only if the Boolean matches the `old`, returns whether succeeded
|
||||||
cond.Toggle() // Inverts the boolean then returns the value before inverting
|
cond.Toggle() // Inverts the boolean then returns the value before inverting
|
||||||
|
|
5
bool.go
5
bool.go
|
@ -38,6 +38,11 @@ func (ab *AtomicBool) IsSet() bool {
|
||||||
return atomic.LoadInt32((*int32)(ab))&1 == 1
|
return atomic.LoadInt32((*int32)(ab))&1 == 1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// IsNotSet returns whether the Boolean is false.
|
||||||
|
func (ab *AtomicBool) IsNotSet() bool {
|
||||||
|
return !ab.IsSet()
|
||||||
|
}
|
||||||
|
|
||||||
// SetTo sets the boolean with given Boolean.
|
// SetTo sets the boolean with given Boolean.
|
||||||
func (ab *AtomicBool) SetTo(yes bool) {
|
func (ab *AtomicBool) SetTo(yes bool) {
|
||||||
if yes {
|
if yes {
|
||||||
|
|
20
bool_test.go
20
bool_test.go
|
@ -25,6 +25,10 @@ func TestBool(t *testing.T) {
|
||||||
t.Fatal("Empty value of AtomicBool should be false")
|
t.Fatal("Empty value of AtomicBool should be false")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if v.IsSet() == v.IsNotSet() {
|
||||||
|
t.Fatal("AtomicBool.IsNotSet() should be the opposite of IsSet()")
|
||||||
|
}
|
||||||
|
|
||||||
v.Set()
|
v.Set()
|
||||||
if !v.IsSet() {
|
if !v.IsSet() {
|
||||||
t.Fatal("AtomicBool.Set() failed")
|
t.Fatal("AtomicBool.Set() failed")
|
||||||
|
@ -165,11 +169,17 @@ func TestRace(t *testing.T) {
|
||||||
|
|
||||||
func ExampleAtomicBool() {
|
func ExampleAtomicBool() {
|
||||||
cond := New() // default to false
|
cond := New() // default to false
|
||||||
cond.Set() // set to true
|
any := true
|
||||||
cond.IsSet() // returns true
|
old := any
|
||||||
cond.UnSet() // set to false
|
new := !any
|
||||||
cond.SetTo(true) // set to whatever you want
|
|
||||||
cond.Toggle() // toggles the boolean value
|
cond.Set() // Sets to true
|
||||||
|
cond.IsSet() // Returns true
|
||||||
|
cond.UnSet() // Sets to false
|
||||||
|
cond.IsNotSet() // Returns true
|
||||||
|
cond.SetTo(any) // Sets to whatever you want
|
||||||
|
cond.SetToIf(new, old) // Sets to `new` only if the Boolean matches the `old`, returns whether succeeded
|
||||||
|
cond.Toggle() // Inverts the boolean then returns the value before inverting
|
||||||
}
|
}
|
||||||
|
|
||||||
// Benchmark Read
|
// Benchmark Read
|
||||||
|
|
Loading…
Reference in New Issue