github.com/sandwich-go/boost@v1.3.29/xhash/hash14v/gen_options_optiongen.go (about) 1 // Code generated by optiongen. DO NOT EDIT. 2 // optiongen: github.com/timestee/optiongen 3 4 package hash14v 5 6 import ( 7 "sync/atomic" 8 "unsafe" 9 10 "github.com/sandwich-go/boost/z" 11 ) 12 13 // Options should use NewOptions to initialize it 14 type Options struct { 15 HashKey []byte `xconf:"hash_key" usage:"hash使用的key"` 16 HashOffset []byte `xconf:"hash_offset" usage:"hash的偏移值"` 17 UsingReservedBuff bool `xconf:"using_reserved_buff" usage:"解压缩等级"` 18 } 19 20 // NewOptions new Options 21 func NewOptions(opts ...Option) *Options { 22 cc := newDefaultOptions() 23 for _, opt := range opts { 24 opt(cc) 25 } 26 if watchDogOptions != nil { 27 watchDogOptions(cc) 28 } 29 return cc 30 } 31 32 // ApplyOption apply multiple new option and return the old ones 33 // sample: 34 // old := cc.ApplyOption(WithTimeout(time.Second)) 35 // defer cc.ApplyOption(old...) 36 func (cc *Options) ApplyOption(opts ...Option) []Option { 37 var previous []Option 38 for _, opt := range opts { 39 previous = append(previous, opt(cc)) 40 } 41 return previous 42 } 43 44 // Option option func 45 type Option func(cc *Options) Option 46 47 // WithHashKey hash使用的key 48 func WithHashKey(v []byte) Option { 49 return func(cc *Options) Option { 50 previous := cc.HashKey 51 cc.HashKey = v 52 return WithHashKey(previous) 53 } 54 } 55 56 // WithHashOffset hash的偏移值 57 func WithHashOffset(v []byte) Option { 58 return func(cc *Options) Option { 59 previous := cc.HashOffset 60 cc.HashOffset = v 61 return WithHashOffset(previous) 62 } 63 } 64 65 // WithUsingReservedBuff 解压缩等级 66 func WithUsingReservedBuff(v bool) Option { 67 return func(cc *Options) Option { 68 previous := cc.UsingReservedBuff 69 cc.UsingReservedBuff = v 70 return WithUsingReservedBuff(previous) 71 } 72 } 73 74 // InstallOptionsWatchDog the installed func will called when NewOptions called 75 func InstallOptionsWatchDog(dog func(cc *Options)) { watchDogOptions = dog } 76 77 // watchDogOptions global watch dog 78 var watchDogOptions func(cc *Options) 79 80 // newDefaultOptions new default Options 81 func newDefaultOptions() *Options { 82 cc := &Options{} 83 84 for _, opt := range [...]Option{ 85 WithHashKey(z.StringToBytes("nlCwbUUd")), 86 WithHashOffset(z.StringToBytes("FAAAAAA")), 87 WithUsingReservedBuff(false), 88 } { 89 opt(cc) 90 } 91 92 return cc 93 } 94 95 // AtomicSetFunc used for XConf 96 func (cc *Options) AtomicSetFunc() func(interface{}) { return AtomicOptionsSet } 97 98 // atomicOptions global *Options holder 99 var atomicOptions unsafe.Pointer 100 101 // onAtomicOptionsSet global call back when AtomicOptionsSet called by XConf. 102 // use OptionsInterface.ApplyOption to modify the updated cc 103 // if passed in cc not valid, then return false, cc will not set to atomicOptions 104 var onAtomicOptionsSet func(cc OptionsInterface) bool 105 106 // InstallCallbackOnAtomicOptionsSet install callback 107 func InstallCallbackOnAtomicOptionsSet(callback func(cc OptionsInterface) bool) { 108 onAtomicOptionsSet = callback 109 } 110 111 // AtomicOptionsSet atomic setter for *Options 112 func AtomicOptionsSet(update interface{}) { 113 cc := update.(*Options) 114 if onAtomicOptionsSet != nil && !onAtomicOptionsSet(cc) { 115 return 116 } 117 atomic.StorePointer(&atomicOptions, (unsafe.Pointer)(cc)) 118 } 119 120 // AtomicOptions return atomic *OptionsVisitor 121 func AtomicOptions() OptionsVisitor { 122 current := (*Options)(atomic.LoadPointer(&atomicOptions)) 123 if current == nil { 124 defaultOne := newDefaultOptions() 125 if watchDogOptions != nil { 126 watchDogOptions(defaultOne) 127 } 128 atomic.CompareAndSwapPointer(&atomicOptions, nil, (unsafe.Pointer)(defaultOne)) 129 return (*Options)(atomic.LoadPointer(&atomicOptions)) 130 } 131 return current 132 } 133 134 // all getter func 135 func (cc *Options) GetHashKey() []byte { return cc.HashKey } 136 func (cc *Options) GetHashOffset() []byte { return cc.HashOffset } 137 func (cc *Options) GetUsingReservedBuff() bool { return cc.UsingReservedBuff } 138 139 // OptionsVisitor visitor interface for Options 140 type OptionsVisitor interface { 141 GetHashKey() []byte 142 GetHashOffset() []byte 143 GetUsingReservedBuff() bool 144 } 145 146 // OptionsInterface visitor + ApplyOption interface for Options 147 type OptionsInterface interface { 148 OptionsVisitor 149 ApplyOption(...Option) []Option 150 }