adding small usage note for bitmasks
This commit is contained in:
		
							parent
							
								
									0887f0c76e
								
							
						
					
					
						commit
						d5b1d449e5
					
				@ -1,5 +1,36 @@
 | 
				
			|||||||
package types
 | 
					package types
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/*
 | 
				
			||||||
 | 
						See https://yourbasic.org/golang/bitmask-flag-set-clear/ for more information.
 | 
				
			||||||
 | 
						To use this, set constants like thus:
 | 
				
			||||||
 | 
						
 | 
				
			||||||
 | 
						const (
 | 
				
			||||||
 | 
							OPT1 types.MaskBit = 1 << iota
 | 
				
			||||||
 | 
							OPT2
 | 
				
			||||||
 | 
							OPT3
 | 
				
			||||||
 | 
							// ...
 | 
				
			||||||
 | 
						)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						type Object struct {
 | 
				
			||||||
 | 
							Opts BitMask
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						o := Object{
 | 
				
			||||||
 | 
							BitMask: uint8(0)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						o.AddFlag(OPT1)
 | 
				
			||||||
 | 
						o.AddFlag(OPT3)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						This would return true:
 | 
				
			||||||
 | 
							o.HasFlag(OPT1)
 | 
				
			||||||
 | 
						As would this:
 | 
				
			||||||
 | 
							o.HasFlag(OPT3)
 | 
				
			||||||
 | 
						But this would return false:
 | 
				
			||||||
 | 
							o.HasFlag(OPT2)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					*/
 | 
				
			||||||
type BitMask interface {
 | 
					type BitMask interface {
 | 
				
			||||||
	HasFlag(bit MaskBit) bool
 | 
						HasFlag(bit MaskBit) bool
 | 
				
			||||||
	AddFlag(bit MaskBit)
 | 
						AddFlag(bit MaskBit)
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user