/
gtype_uint32.go
84 lines (69 loc) · 2.38 KB
/
gtype_uint32.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// 版权所有 GoFrame 作者(https://goframe.org)。保留所有权利。
//
// 本源代码形式遵循 MIT 许可协议条款。如果随此文件未分发 MIT 许可副本,
// 您可以在 https://github.com/gogf/gf 获取一份。
package 安全变量类
import (
"strconv"
"sync/atomic"
"github.com/888go/goframe/util/gconv"
)
// Uint32 是一个结构体,用于对 uint32 类型进行并发安全操作。
type Uint32 struct {
value uint32
}
// NewUint32 创建并返回一个适用于uint32类型的并发安全对象,
// 其初始值为给定的 `value`。
func NewUint32(value ...uint32) *Uint32 {
if len(value) > 0 {
return &Uint32{
value: value[0],
}
}
return &Uint32{}
}
// Clone 克隆并返回一个用于 uint32 类型的新并发安全对象。
func (v *Uint32) Clone() *Uint32 {
return NewUint32(v.X取值())
}
// Set 方法通过原子操作将`value`存储到t.value中,并返回修改前的t.value的值。
func (v *Uint32) X设置值(value uint32) (old uint32) {
return atomic.SwapUint32(&v.value, value)
}
// Val 原子性地加载并返回 t.value。
func (v *Uint32) X取值() uint32 {
return atomic.LoadUint32(&v.value)
}
// Add 原子性地将 `delta` 加到 t.value 上,并返回新的值。
func (v *Uint32) Add(delta uint32) (new uint32) {
return atomic.AddUint32(&v.value, delta)
}
// Cas 执行值的比较并交换操作。
func (v *Uint32) Cas(old, new uint32) (swapped bool) {
return atomic.CompareAndSwapUint32(&v.value, old, new)
}
// String 实现了 String 接口以便进行字符串打印。
func (v *Uint32) String() string {
return strconv.FormatUint(uint64(v.X取值()), 10)
}
// MarshalJSON 实现了 json.Marshal 接口所需的 MarshalJSON 方法。
func (v Uint32) MarshalJSON() ([]byte, error) {
return []byte(strconv.FormatUint(uint64(v.X取值()), 10)), nil
}
// UnmarshalJSON 实现了 json.Unmarshal 接口的 UnmarshalJSON 方法。
func (v *Uint32) UnmarshalJSON(b []byte) error {
v.X设置值(转换类.X取正整数32位(string(b)))
return nil
}
// UnmarshalValue 是一个接口实现,用于为 `v` 设置任意类型的值。
func (v *Uint32) UnmarshalValue(value interface{}) error {
v.X设置值(转换类.X取正整数32位(value))
return nil
}
// DeepCopy 实现接口,用于当前类型的深度复制。
func (v *Uint32) DeepCopy() interface{} {
if v == nil {
return nil
}
return NewUint32(v.X取值())
}