github.com/hsfzxjy/dgo/go@v0.2.0/callback_flag.go (about)

     1  package dgo
     2  
     3  type CallbackFlag uint64
     4  
     5  const (
     6  	cfBitsStart     = 32
     7  	cfBitsFastStart = cfBitsStart + 4
     8  
     9  	CF CallbackFlag = 0
    10  
    11  	CF_POP CallbackFlag = 1 << (cfBitsStart + 0)
    12  
    13  	CF_WITHCONTEXT CallbackFlag = 1 << (cfBitsStart + 1)
    14  	CF_PACKARRAY   CallbackFlag = 1 << (cfBitsStart + 2)
    15  
    16  	CF_FAST      CallbackFlag = 1 << (cfBitsStart + 3)
    17  	CF_FAST_VOID CallbackFlag = CF_FAST + (0 << (cfBitsStart + 4))
    18  	CF_FAST_NIL  CallbackFlag = CF_FAST + (1 << (cfBitsStart + 4))
    19  	CF_FAST_YES  CallbackFlag = CF_FAST + (2 << (cfBitsStart + 4))
    20  	CF_FAST_NO   CallbackFlag = CF_FAST + (3 << (cfBitsStart + 4))
    21  
    22  	CF_FALLIBLE CallbackFlag = 1 << (cfBitsStart + 6)
    23  
    24  	cf_fut_reject  CallbackFlag = 0 << (cfBitsStart + 7)
    25  	cf_fut_resolve CallbackFlag = 1 << (cfBitsStart + 7)
    26  
    27  	cf_stream_value CallbackFlag = 0 << (cfBitsStart + 8)
    28  	cf_stream_error CallbackFlag = 1 << (cfBitsStart + 8)
    29  )
    30  
    31  // 8 <= n <= 15
    32  func CF_CUSTOM(n int) CallbackFlag {
    33  	return CallbackFlag(1 << (cfBitsStart + n))
    34  }
    35  
    36  func (cf CallbackFlag) Pop() CallbackFlag { return cf | CF_POP }
    37  func (cf CallbackFlag) HasPop() bool      { return cf&CF_POP != 0 }
    38  
    39  func (cf CallbackFlag) WithContext() CallbackFlag { return cf | CF_WITHCONTEXT }
    40  func (cf CallbackFlag) HasWithContext() bool      { return cf&CF_WITHCONTEXT != 0 }
    41  
    42  func (cf CallbackFlag) PackArray() CallbackFlag { return cf | CF_PACKARRAY }
    43  func (cf CallbackFlag) HasPackArray() bool      { return cf&CF_PACKARRAY != 0 }
    44  
    45  func (cf CallbackFlag) Fast(kind CFFastKind) CallbackFlag {
    46  	switch kind {
    47  	case CFFK_NONE:
    48  		// clear CF_FAST
    49  		return cf & ^CF_FAST
    50  	case CFFK_VOID, CFFK_NIL, CFFK_YES, CFFK_NO:
    51  		// set CF_FAST
    52  		cf = cf | CF_FAST
    53  		cf = cf & ^((CF_FAST << 1) | (CF_FAST << 2))
    54  		cf = cf + (CallbackFlag(kind-1) << cfBitsFastStart)
    55  		return cf
    56  	default:
    57  		return cf
    58  	}
    59  }
    60  func (cf CallbackFlag) HasFast() bool { return cf&CF_FAST != 0 }
    61  
    62  func (cf CallbackFlag) FastKind() CFFastKind {
    63  	if cf&CF_FAST == 0 {
    64  		return CFFK_NONE
    65  	}
    66  	kind := cf & ((CF_FAST << 1) | (CF_FAST << 2))
    67  	kind >>= cfBitsFastStart
    68  	return CFFastKind(kind + 1)
    69  }
    70  
    71  func (cf CallbackFlag) Fallible() CallbackFlag { return cf | CF_FALLIBLE }
    72  func (cf CallbackFlag) HasFallible() bool      { return cf&CF_FALLIBLE != 0 }
    73  
    74  type CFFastKind int
    75  
    76  const (
    77  	CFFK_NONE CFFastKind = iota
    78  	CFFK_VOID
    79  	CFFK_NIL
    80  	CFFK_YES
    81  	CFFK_NO
    82  )