github.com/wangyougui/gf/v2@v2.6.5/container/gtype/gtype_float32.go (about) 1 // Copyright GoFrame Author(https://goframe.org). All Rights Reserved. 2 // 3 // This Source Code Form is subject to the terms of the MIT License. 4 // If a copy of the MIT was not distributed with this file, 5 // You can obtain one at https://github.com/wangyougui/gf. 6 7 package gtype 8 9 import ( 10 "math" 11 "strconv" 12 "sync/atomic" 13 14 "github.com/wangyougui/gf/v2/util/gconv" 15 ) 16 17 // Float32 is a struct for concurrent-safe operation for type float32. 18 type Float32 struct { 19 value uint32 20 } 21 22 // NewFloat32 creates and returns a concurrent-safe object for float32 type, 23 // with given initial value `value`. 24 func NewFloat32(value ...float32) *Float32 { 25 if len(value) > 0 { 26 return &Float32{ 27 value: math.Float32bits(value[0]), 28 } 29 } 30 return &Float32{} 31 } 32 33 // Clone clones and returns a new concurrent-safe object for float32 type. 34 func (v *Float32) Clone() *Float32 { 35 return NewFloat32(v.Val()) 36 } 37 38 // Set atomically stores `value` into t.value and returns the previous value of t.value. 39 func (v *Float32) Set(value float32) (old float32) { 40 return math.Float32frombits(atomic.SwapUint32(&v.value, math.Float32bits(value))) 41 } 42 43 // Val atomically loads and returns t.value. 44 func (v *Float32) Val() float32 { 45 return math.Float32frombits(atomic.LoadUint32(&v.value)) 46 } 47 48 // Add atomically adds `delta` to t.value and returns the new value. 49 func (v *Float32) Add(delta float32) (new float32) { 50 for { 51 old := math.Float32frombits(v.value) 52 new = old + delta 53 if atomic.CompareAndSwapUint32( 54 &v.value, 55 math.Float32bits(old), 56 math.Float32bits(new), 57 ) { 58 break 59 } 60 } 61 return 62 } 63 64 // Cas executes the compare-and-swap operation for value. 65 func (v *Float32) Cas(old, new float32) (swapped bool) { 66 return atomic.CompareAndSwapUint32(&v.value, math.Float32bits(old), math.Float32bits(new)) 67 } 68 69 // String implements String interface for string printing. 70 func (v *Float32) String() string { 71 return strconv.FormatFloat(float64(v.Val()), 'g', -1, 32) 72 } 73 74 // MarshalJSON implements the interface MarshalJSON for json.Marshal. 75 func (v Float32) MarshalJSON() ([]byte, error) { 76 return []byte(strconv.FormatFloat(float64(v.Val()), 'g', -1, 32)), nil 77 } 78 79 // UnmarshalJSON implements the interface UnmarshalJSON for json.Unmarshal. 80 func (v *Float32) UnmarshalJSON(b []byte) error { 81 v.Set(gconv.Float32(string(b))) 82 return nil 83 } 84 85 // UnmarshalValue is an interface implement which sets any type of value for `v`. 86 func (v *Float32) UnmarshalValue(value interface{}) error { 87 v.Set(gconv.Float32(value)) 88 return nil 89 } 90 91 // DeepCopy implements interface for deep copy of current type. 92 func (v *Float32) DeepCopy() interface{} { 93 if v == nil { 94 return nil 95 } 96 return NewFloat32(v.Val()) 97 }