q5/xvalue.go
aozhiwei a3ab521064 1
2020-09-08 17:00:41 +08:00

203 lines
3.6 KiB
Go

package q5
import (
"strconv"
)
const (
XVT_UNDEFINED = 0
XVT_INT = iota
XVT_FLOAT = iota
XVT_STRING = iota
XVT_BYTES = iota
XVT_USERDATA = iota
)
type XValue struct
{
_type int8
_val interface{}
}
func (this *XValue) GetType() int8 {
if this._type == XVT_UNDEFINED {
return XVT_INT
} else {
return this._type
}
}
func (this *XValue) Reset() {
this._type = XVT_UNDEFINED
this._val = nil
}
func (this *XValue) IsUndefined() bool {
return this._type == XVT_UNDEFINED
}
func (this *XValue) SetUInt8(val uint8) *XValue {
this.SetInt64(int64(val))
return this
}
func (this *XValue) SetInt8(val int8) *XValue {
this.SetInt64(int64(val))
return this
}
func (this *XValue) SetUInt16(val uint16) *XValue {
this.SetInt64(int64(val))
return this
}
func (this *XValue) SetInt16(val int16) *XValue {
this.SetInt64(int64(val))
return this
}
func (this *XValue) SetUInt32(val uint32) *XValue {
this.SetInt64(int64(val))
return this
}
func (this *XValue) SetInt32(val int32) *XValue {
this.SetInt64(int64(val))
return this
}
func (this *XValue) SetInt64(val int64) *XValue {
val_copy := val
this._type = XVT_INT
this._val = &val_copy
return this
}
func (this *XValue) SetFloat32(val float32) *XValue {
this.SetFloat64(float64(val))
return this
}
func (this *XValue) SetFloat64(val float64) *XValue {
val_copy := val
this._type = XVT_FLOAT
this._val = &val_copy
return this
}
func (this *XValue) SetString(val string) *XValue {
val_copy := val + ""
this._type = XVT_STRING
this._val = &val_copy
return this
}
func (this *XValue) SetBytes(val []byte) *XValue {
val_copy := val
this._type = XVT_FLOAT
this._val = &val_copy
return this
}
func (this *XValue) SetUserData(val interface{}) *XValue {
val_copy := val
this._type = XVT_FLOAT
this._val = &val_copy
return this
}
func (this *XValue) GetUInt8() uint8 {
return uint8(this.GetInt64())
}
func (this *XValue) GetInt8() int8 {
return int8(this.GetInt64())
}
func (this *XValue) GetUInt16() uint16 {
return uint16(this.GetInt64())
}
func (this *XValue) GetInt16() int16 {
return int16(this.GetInt64())
}
func (this *XValue) GetUInt32() uint32 {
return uint32(this.GetInt64())
}
func (this *XValue) GetInt32() int32 {
return int32(this.GetInt64())
}
func (this *XValue) GetInt64() int64 {
switch this._type {
case XVT_INT:
return *(this._val.(*int64))
case XVT_FLOAT:
return int64(*(this._val.(*float64)))
case XVT_STRING, XVT_BYTES, XVT_USERDATA:
val, err := strconv.ParseInt(this.GetString(), 10, 64)
if err == nil {
return val
} else {
val, err := strconv.ParseFloat(this.GetString(), 64)
if err == nil {
return int64(val)
} else {
return 0
}
}
default:
return 0
}
}
func (this *XValue) GetFloat32() float32 {
return float32(this.GetFloat64())
}
func (this *XValue) GetFloat64() float64 {
switch this._type {
case XVT_INT:
return float64(*(this._val.(*int64)))
case XVT_FLOAT:
return *(this._val.(*float64))
case XVT_STRING, XVT_BYTES, XVT_USERDATA:
val, err := strconv.ParseFloat(this.GetString(), 64)
if err == nil {
return val
} else {
val, err := strconv.ParseInt(this.GetString(), 10, 64)
if err == nil {
return float64(val)
} else {
return 0
}
}
default:
return 0
}
}
func (this *XValue) GetString() string {
switch this._type {
case XVT_INT:
return strconv.FormatInt(this.GetInt64(), 10)
case XVT_FLOAT:
return strconv.FormatFloat(this.GetFloat64(), 'E', -1, 64)
case XVT_STRING:
return *(this._val.(*string))
default:
return ""
}
}
func (this *XValue) GetUserData() interface{} {
if this._type == XVT_USERDATA {
return this._val
} else {
return nil
}
}