do bitmask better, add (COMPLETELY 100% EXPERIMENTAL NOT DONE YET) eventlog support to logger
This commit is contained in:
46
bitmask/bitmasks.go
Normal file
46
bitmask/bitmasks.go
Normal file
@@ -0,0 +1,46 @@
|
||||
package bitmask
|
||||
|
||||
// MaskBit is a flag container.
|
||||
type MaskBit uint8
|
||||
|
||||
/*
|
||||
NewMaskBit is a convenience function.
|
||||
It will return a MaskBit with a (referenced) value of 0, so set your consts up accordingly.
|
||||
It is highly recommended to set this default as a "None" flag (separate from your iotas!)
|
||||
as shown in the example.
|
||||
*/
|
||||
func NewMaskBit() (m *MaskBit) {
|
||||
|
||||
m = new(MaskBit)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// HasFlag is true if m has MaskBit flag set/enabled.
|
||||
func (m *MaskBit) HasFlag(flag MaskBit) (r bool) {
|
||||
|
||||
var b MaskBit = *m
|
||||
|
||||
if b&flag != 0 {
|
||||
r = true
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// AddFlag adds MaskBit flag to m.
|
||||
func (m *MaskBit) AddFlag(flag MaskBit) {
|
||||
*m |= flag
|
||||
return
|
||||
}
|
||||
|
||||
// ClearFlag removes MaskBit flag from m.
|
||||
func (m *MaskBit) ClearFlag(flag MaskBit) {
|
||||
*m &= flag
|
||||
return
|
||||
}
|
||||
|
||||
// ToggleFlag switches MaskBit flag in m to its inverse; if true, it is now false and vice versa.
|
||||
func (m *MaskBit) ToggleFlag(flag MaskBit) {
|
||||
*m ^= flag
|
||||
return
|
||||
}
|
||||
45
bitmask/doc.go
Normal file
45
bitmask/doc.go
Normal file
@@ -0,0 +1,45 @@
|
||||
/*
|
||||
Package bitmask handles a flag-like opt/bitmask system.
|
||||
|
||||
See https://yourbasic.org/golang/bitmask-flag-set-clear/ for more information.
|
||||
|
||||
To use this, set constants like thus:
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"r00t2.io/goutils/bitmask"
|
||||
)
|
||||
|
||||
const OPTNONE types.MaskBit = 0
|
||||
const (
|
||||
OPT1 types.MaskBit = 1 << iota
|
||||
OPT2
|
||||
OPT3
|
||||
// ...
|
||||
)
|
||||
|
||||
var MyMask *MaskBit
|
||||
|
||||
func main() {
|
||||
MyMask = types.NewMaskBit
|
||||
|
||||
MyMask.AddFlag(OPT1)
|
||||
MyMask.AddFlag(OPT3)
|
||||
|
||||
_ = MyMask
|
||||
}
|
||||
|
||||
This would return true:
|
||||
|
||||
MyMask.HasFlag(OPT1)
|
||||
|
||||
As would this:
|
||||
|
||||
MyMask.HasFlag(OPT3)
|
||||
|
||||
But this would return false:
|
||||
|
||||
MyMask.HasFlag(OPT2)
|
||||
*/
|
||||
package bitmask
|
||||
Reference in New Issue
Block a user