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

     1  package codec
     2  
     3  import (
     4  	"unsafe"
     5  
     6  	dgo "github.com/hsfzxjy/dgo/go"
     7  )
     8  
     9  type integer interface {
    10  	~int8 | ~int16 | ~int32 | ~int64 | ~int |
    11  		~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uint | ~uintptr
    12  }
    13  
    14  func DecodeInteger[T integer](obj *dgo.Dart_CObject) T {
    15  	if obj.Type == dgo.Dart_CObject_kInt32 {
    16  		return T(*(*int32)(unsafe.Pointer(&obj.Value)))
    17  	} else {
    18  		return T(*(*int64)(unsafe.Pointer(&obj.Value)))
    19  	}
    20  }
    21  
    22  func DecodeUint64(obj *dgo.Dart_CObject) uint64 {
    23  	return uint64(*(*int64)(unsafe.Pointer(&obj.Value)))
    24  }
    25  
    26  func EncodeInteger(value int64, obj *dgo.Dart_CObject) {
    27  	obj.Type = dgo.Dart_CObject_kInt64
    28  	*(*int64)(unsafe.Pointer(&obj.Value)) = value
    29  }
    30  
    31  type float interface {
    32  	~float32 | ~float64
    33  }
    34  
    35  func DecodeFloat[T float](obj *dgo.Dart_CObject) T {
    36  	return T(*(*float64)(unsafe.Pointer(&obj.Value)))
    37  }
    38  
    39  func EncodeFloat(value float64, obj *dgo.Dart_CObject) {
    40  	obj.Type = dgo.Dart_CObject_kDouble
    41  	*(*float64)(unsafe.Pointer(&obj.Value)) = value
    42  }
    43  
    44  func DecodeBool(obj *dgo.Dart_CObject) bool {
    45  	return *(*bool)(unsafe.Pointer(&obj.Value))
    46  }
    47  
    48  func EncodeBool(value bool, obj *dgo.Dart_CObject) {
    49  	obj.Type = dgo.Dart_CObject_kBool
    50  	*(*bool)(unsafe.Pointer(&obj.Value)) = value
    51  }