github.com/15mga/kiwi@v0.0.2-0.20240324021231-b95d5c3ac751/ds/fn_link.go (about)

     1  package ds
     2  
     3  import (
     4  	"reflect"
     5  	"sync"
     6  
     7  	"github.com/15mga/kiwi/util"
     8  )
     9  
    10  var (
    11  	_FnLinkPool = sync.Pool{
    12  		New: func() any {
    13  			return &FnLink{
    14  				Link: NewLink[util.Fn](),
    15  			}
    16  		},
    17  	}
    18  )
    19  
    20  func NewFnLink() *FnLink {
    21  	return _FnLinkPool.Get().(*FnLink)
    22  }
    23  
    24  type FnLink struct {
    25  	*Link[util.Fn]
    26  }
    27  
    28  func (l *FnLink) Invoke() bool {
    29  	if l.count == 0 {
    30  		return false
    31  	}
    32  	for e := l.head; e != nil; e = e.Next {
    33  		e.Value()
    34  	}
    35  	return true
    36  }
    37  
    38  func (l *FnLink) InvokeAndReset() bool {
    39  	if l.count == 0 {
    40  		return false
    41  	}
    42  	for e := l.head; e != nil; e = e.Next {
    43  		e.Value()
    44  	}
    45  	l.head = nil
    46  	l.tail = nil
    47  	l.count = 0
    48  	return true
    49  }
    50  
    51  func (l *FnLink) Dispose() {
    52  	l.Link.Dispose()
    53  	_FnLinkPool.Put(l)
    54  }
    55  
    56  func (l *FnLink) Del(fn util.Fn) {
    57  	pointer := reflect.ValueOf(fn).Pointer()
    58  	_ = l.Link.Del(func(f util.Fn) bool {
    59  		return reflect.ValueOf(f).Pointer() == pointer
    60  	})
    61  }
    62  
    63  func (l *FnLink) Reset() {
    64  	if l.count == 0 {
    65  		return
    66  	}
    67  	l.head = nil
    68  	l.tail = nil
    69  	l.count = 0
    70  }
    71  
    72  func NewFnLink1[T any]() *FnLink1[T] {
    73  	l := &FnLink1[T]{
    74  		Link: NewLink[func(T)](),
    75  	}
    76  	return l
    77  }
    78  
    79  type FnLink1[T any] struct {
    80  	*Link[func(T)]
    81  }
    82  
    83  func (l *FnLink1[T]) Invoke(obj T) {
    84  	for e := l.head; e != nil; e = e.Next {
    85  		e.Value(obj)
    86  	}
    87  }
    88  
    89  func (l *FnLink1[T]) Del(fn func(T)) {
    90  	pointer := reflect.ValueOf(fn).Pointer()
    91  	_ = l.Link.Del(func(f func(T)) bool {
    92  		return reflect.ValueOf(f).Pointer() == pointer
    93  	})
    94  }
    95  
    96  func (l *FnLink1[T]) Reset() {
    97  	l.head = nil
    98  	l.tail = nil
    99  }
   100  
   101  func NewFnLink2[T0, T1 any]() *FnLink2[T0, T1] {
   102  	return &FnLink2[T0, T1]{
   103  		Link: NewLink[func(T0, T1)](),
   104  	}
   105  }
   106  
   107  type FnLink2[T0, T1 any] struct {
   108  	*Link[func(T0, T1)]
   109  }
   110  
   111  func (l *FnLink2[T0, T1]) Invoke(v0 T0, v1 T1) {
   112  	for e := l.head; e != nil; e = e.Next {
   113  		e.Value(v0, v1)
   114  	}
   115  }
   116  
   117  func (l *FnLink2[T0, T1]) Del(fn func(T0, T1)) {
   118  	pointer := reflect.ValueOf(fn).Pointer()
   119  	_ = l.Link.Del(func(f func(T0, T1)) bool {
   120  		return reflect.ValueOf(f).Pointer() == pointer
   121  	})
   122  }
   123  
   124  func (l *FnLink2[T0, T1]) Reset() {
   125  	l.head = nil
   126  	l.tail = nil
   127  }
   128  
   129  func NewFnLink3[T0, T1, T2 any]() *FnLink3[T0, T1, T2] {
   130  	return &FnLink3[T0, T1, T2]{
   131  		Link: NewLink[func(T0, T1, T2)](),
   132  	}
   133  }
   134  
   135  type FnLink3[T0, T1, T2 any] struct {
   136  	*Link[func(T0, T1, T2)]
   137  }
   138  
   139  func (l *FnLink3[T0, T1, T2]) Invoke(v0 T0, v1 T1, v2 T2) {
   140  	for e := l.head; e != nil; e = e.Next {
   141  		e.Value(v0, v1, v2)
   142  	}
   143  }
   144  
   145  func (l *FnLink3[T0, T1, T2]) Del(fn func(T0, T1, T2)) {
   146  	pointer := reflect.ValueOf(fn).Pointer()
   147  	_ = l.Link.Del(func(f func(T0, T1, T2)) bool {
   148  		return reflect.ValueOf(f).Pointer() == pointer
   149  	})
   150  }
   151  
   152  func (l *FnLink3[T0, T1, T2]) Reset() {
   153  	l.head = nil
   154  	l.tail = nil
   155  }
   156  
   157  func NewFnLink4[T0, T1, T2, T3 any]() *FnLink4[T0, T1, T2, T3] {
   158  	return &FnLink4[T0, T1, T2, T3]{
   159  		Link: NewLink[func(T0, T1, T2, T3)](),
   160  	}
   161  }
   162  
   163  type FnLink4[T0, T1, T2, T3 any] struct {
   164  	*Link[func(T0, T1, T2, T3)]
   165  }
   166  
   167  func (l *FnLink4[T0, T1, T2, T3]) Invoke(v0 T0, v1 T1, v2 T2, v3 T3) {
   168  	for e := l.head; e != nil; e = e.Next {
   169  		e.Value(v0, v1, v2, v3)
   170  	}
   171  }
   172  
   173  func (l *FnLink4[T0, T1, T2, T3]) Del(fn func(T0, T1, T2, T3)) {
   174  	pointer := reflect.ValueOf(fn).Pointer()
   175  	_ = l.Link.Del(func(f func(T0, T1, T2, T3)) bool {
   176  		return reflect.ValueOf(f).Pointer() == pointer
   177  	})
   178  }
   179  
   180  func (l *FnLink4[T0, T1, T2, T3]) Reset() {
   181  	l.head = nil
   182  	l.tail = nil
   183  }
   184  
   185  func NewFnLink5[T0, T1, T2, T3, T4 any]() *FnLink5[T0, T1, T2, T3, T4] {
   186  	return &FnLink5[T0, T1, T2, T3, T4]{
   187  		Link: NewLink[func(T0, T1, T2, T3, T4)](),
   188  	}
   189  }
   190  
   191  type FnLink5[T0, T1, T2, T3, T4 any] struct {
   192  	*Link[func(T0, T1, T2, T3, T4)]
   193  }
   194  
   195  func (l *FnLink5[T0, T1, T2, T3, T4]) Invoke(v0 T0, v1 T1, v2 T2, v3 T3, v4 T4) {
   196  	for e := l.head; e != nil; e = e.Next {
   197  		e.Value(v0, v1, v2, v3, v4)
   198  	}
   199  }
   200  
   201  func (l *FnLink5[T0, T1, T2, T3, T4]) Del(fn func(T0, T1, T2, T3, T4)) {
   202  	pointer := reflect.ValueOf(fn).Pointer()
   203  	_ = l.Link.Del(func(f func(T0, T1, T2, T3, T4)) bool {
   204  		return reflect.ValueOf(f).Pointer() == pointer
   205  	})
   206  }
   207  
   208  func (l *FnLink5[T0, T1, T2, T3, T4]) Reset() {
   209  	l.head = nil
   210  	l.tail = nil
   211  }
   212  
   213  func NewFnLink6[T0, T1, T2, T3, T4, T5 any]() *FnLink6[T0, T1, T2, T3, T4, T5] {
   214  	return &FnLink6[T0, T1, T2, T3, T4, T5]{
   215  		Link: NewLink[func(T0, T1, T2, T3, T4, T5)](),
   216  	}
   217  }
   218  
   219  type FnLink6[T0, T1, T2, T3, T4, T5 any] struct {
   220  	*Link[func(T0, T1, T2, T3, T4, T5)]
   221  }
   222  
   223  func (l *FnLink6[T0, T1, T2, T3, T4, T5]) Invoke(v0 T0, v1 T1, v2 T2, v3 T3, v4 T4, v5 T5) {
   224  	for e := l.head; e != nil; e = e.Next {
   225  		e.Value(v0, v1, v2, v3, v4, v5)
   226  	}
   227  }
   228  
   229  func (l *FnLink6[T0, T1, T2, T3, T4, T5]) Del(fn func(T0, T1, T2, T3, T4, T5)) {
   230  	pointer := reflect.ValueOf(fn).Pointer()
   231  	_ = l.Link.Del(func(f func(T0, T1, T2, T3, T4, T5)) bool {
   232  		return reflect.ValueOf(f).Pointer() == pointer
   233  	})
   234  }
   235  
   236  func (l *FnLink6[T0, T1, T2, T3, T4, T5]) Reset() {
   237  	l.head = nil
   238  	l.tail = nil
   239  }