flag.tpl (2961B)
1 package bitutil 2 3 import ( 4 "strings" 5 "unsafe" 6 ) 7 8 {{ range $idx, $size := . }} 9 10 // Flags{{ $size.Size }} is a type-casted unsigned integer with helper 11 // methods for easily managing up to {{ $size.Size }} bit-flags. 12 type Flags{{ $size.Size }} uint{{ $size.Size }} 13 14 // Get will fetch the flag bit value at index 'bit'. 15 func (f Flags{{ $size.Size }}) Get(bit uint8) bool { 16 mask := Flags{{ $size.Size }}(1) << bit 17 return (f & mask != 0) 18 } 19 20 // Set will set the flag bit value at index 'bit'. 21 func (f Flags{{ $size.Size }}) Set(bit uint8) Flags{{ $size.Size }} { 22 mask := Flags{{ $size.Size }}(1) << bit 23 return f | mask 24 } 25 26 // Unset will unset the flag bit value at index 'bit'. 27 func (f Flags{{ $size.Size }}) Unset(bit uint8) Flags{{ $size.Size }} { 28 mask := Flags{{ $size.Size }}(1) << bit 29 return f & ^mask 30 } 31 32 {{ range $idx := $size.Bits }} 33 34 // Get{{ $idx }} will fetch the flag bit value at index {{ $idx }}. 35 func (f Flags{{ $size.Size }}) Get{{ $idx }}() bool { 36 const mask = Flags{{ $size.Size }}(1) << {{ $idx }} 37 return (f & mask != 0) 38 } 39 40 // Set{{ $idx }} will set the flag bit value at index {{ $idx }}. 41 func (f Flags{{ $size.Size }}) Set{{ $idx }}() Flags{{ $size.Size }} { 42 const mask = Flags{{ $size.Size }}(1) << {{ $idx }} 43 return f | mask 44 } 45 46 // Unset{{ $idx }} will unset the flag bit value at index {{ $idx }}. 47 func (f Flags{{ $size.Size }}) Unset{{ $idx }}() Flags{{ $size.Size }} { 48 const mask = Flags{{ $size.Size }}(1) << {{ $idx }} 49 return f & ^mask 50 } 51 52 {{ end }} 53 54 // String returns a human readable representation of Flags{{ $size.Size }}. 55 func (f Flags{{ $size.Size }}) String() string { 56 var ( 57 i int 58 val bool 59 buf []byte 60 ) 61 62 // Make a prealloc est. based on longest-possible value 63 const prealloc = 1+(len("false ")*{{ $size.Size }})-1+1 64 buf = make([]byte, prealloc) 65 66 buf[i] = '{' 67 i++ 68 69 {{ range $idx := .Bits }} 70 val = f.Get{{ $idx }}() 71 i += copy(buf[i:], bool2str(val)) 72 buf[i] = ' ' 73 i++ 74 {{ end }} 75 76 buf[i-1] = '}' 77 buf = buf[:i] 78 79 return *(*string)(unsafe.Pointer(&buf)) 80 } 81 82 // GoString returns a more verbose human readable representation of Flags{{ $size.Size }}. 83 func (f Flags{{ $size.Size }})GoString() string { 84 var ( 85 i int 86 val bool 87 buf []byte 88 ) 89 90 // Make a prealloc est. based on longest-possible value 91 const prealloc = len("bitutil.Flags{{ $size.Size }}{")+(len("{{ sub $size.Size 1 }}=false ")*{{ $size.Size }})-1+1 92 buf = make([]byte, prealloc) 93 94 i += copy(buf[i:], "bitutil.Flags{{ $size.Size }}{") 95 96 {{ range $idx := .Bits }} 97 val = f.Get{{ $idx }}() 98 i += copy(buf[i:], "{{ $idx }}=") 99 i += copy(buf[i:], bool2str(val)) 100 buf[i] = ' ' 101 i++ 102 {{ end }} 103 104 buf[i-1] = '}' 105 buf = buf[:i] 106 107 return *(*string)(unsafe.Pointer(&buf)) 108 } 109 110 {{ end }} 111 112 func bool2str(b bool) string { 113 if b { 114 return "true" 115 } 116 return "false" 117 }