github.com/sandwich-go/boost@v1.3.29/xcompress/gen_options_optiongen.go (about)

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