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

     1  // Code generated by optiongen. DO NOT EDIT.
     2  // optiongen: github.com/timestee/optiongen
     3  
     4  package xtime
     5  
     6  import "time"
     7  
     8  // Options should use NewOptions to initialize it
     9  type Options struct {
    10  	// annotation@TickDuration(comment="tick的duration,大于0是自动开启ticker")
    11  	TickDuration time.Duration
    12  	// annotation@TickHostingMode(comment="全托管模式,内部起一个协程执行tick的func")
    13  	TickHostingMode bool
    14  	// annotation@CountGauge(comment="统计tick数量监控")
    15  	TickCount CountGauge
    16  }
    17  
    18  // NewOptions new Options
    19  func NewOptions(opts ...Option) *Options {
    20  	cc := newDefaultOptions()
    21  	for _, opt := range opts {
    22  		opt(cc)
    23  	}
    24  	if watchDogOptions != nil {
    25  		watchDogOptions(cc)
    26  	}
    27  	return cc
    28  }
    29  
    30  // ApplyOption apply multiple new option
    31  func (cc *Options) ApplyOption(opts ...Option) {
    32  	for _, opt := range opts {
    33  		opt(cc)
    34  	}
    35  }
    36  
    37  // Option option func
    38  type Option func(cc *Options)
    39  
    40  // WithTickDuration tick的duration,大于0是自动开启ticker
    41  func WithTickDuration(v time.Duration) Option {
    42  	return func(cc *Options) {
    43  		cc.TickDuration = v
    44  	}
    45  }
    46  
    47  // WithTickHostingMode 全托管模式,内部起一个协程执行tick的func
    48  func WithTickHostingMode(v bool) Option {
    49  	return func(cc *Options) {
    50  		cc.TickHostingMode = v
    51  	}
    52  }
    53  
    54  // WithTickCount option func for filed TickCount
    55  func WithTickCount(v CountGauge) Option {
    56  	return func(cc *Options) {
    57  		cc.TickCount = v
    58  	}
    59  }
    60  
    61  // InstallOptionsWatchDog the installed func will called when NewOptions  called
    62  func InstallOptionsWatchDog(dog func(cc *Options)) { watchDogOptions = dog }
    63  
    64  // watchDogOptions global watch dog
    65  var watchDogOptions func(cc *Options)
    66  
    67  // newDefaultOptions new default Options
    68  func newDefaultOptions() *Options {
    69  	cc := &Options{}
    70  
    71  	for _, opt := range [...]Option{
    72  		WithTickDuration(0),
    73  		WithTickHostingMode(true),
    74  		WithTickCount(&noopGauge{}),
    75  	} {
    76  		opt(cc)
    77  	}
    78  
    79  	return cc
    80  }
    81  
    82  // all getter func
    83  func (cc *Options) GetTickDuration() time.Duration { return cc.TickDuration }
    84  func (cc *Options) GetTickHostingMode() bool       { return cc.TickHostingMode }
    85  func (cc *Options) GetTickCount() CountGauge       { return cc.TickCount }
    86  
    87  // OptionsVisitor visitor interface for Options
    88  type OptionsVisitor interface {
    89  	GetTickDuration() time.Duration
    90  	GetTickHostingMode() bool
    91  	GetTickCount() CountGauge
    92  }
    93  
    94  // OptionsInterface visitor + ApplyOption interface for Options
    95  type OptionsInterface interface {
    96  	OptionsVisitor
    97  	ApplyOption(...Option)
    98  }