github.com/wangyougui/gf/v2@v2.6.5/container/gtype/gtype_uint64.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  	"strconv"
    11  	"sync/atomic"
    12  
    13  	"github.com/wangyougui/gf/v2/util/gconv"
    14  )
    15  
    16  // Uint64 is a struct for concurrent-safe operation for type uint64.
    17  type Uint64 struct {
    18  	value uint64
    19  }
    20  
    21  // NewUint64 creates and returns a concurrent-safe object for uint64 type,
    22  // with given initial value `value`.
    23  func NewUint64(value ...uint64) *Uint64 {
    24  	if len(value) > 0 {
    25  		return &Uint64{
    26  			value: value[0],
    27  		}
    28  	}
    29  	return &Uint64{}
    30  }
    31  
    32  // Clone clones and returns a new concurrent-safe object for uint64 type.
    33  func (v *Uint64) Clone() *Uint64 {
    34  	return NewUint64(v.Val())
    35  }
    36  
    37  // Set atomically stores `value` into t.value and returns the previous value of t.value.
    38  func (v *Uint64) Set(value uint64) (old uint64) {
    39  	return atomic.SwapUint64(&v.value, value)
    40  }
    41  
    42  // Val atomically loads and returns t.value.
    43  func (v *Uint64) Val() uint64 {
    44  	return atomic.LoadUint64(&v.value)
    45  }
    46  
    47  // Add atomically adds `delta` to t.value and returns the new value.
    48  func (v *Uint64) Add(delta uint64) (new uint64) {
    49  	return atomic.AddUint64(&v.value, delta)
    50  }
    51  
    52  // Cas executes the compare-and-swap operation for value.
    53  func (v *Uint64) Cas(old, new uint64) (swapped bool) {
    54  	return atomic.CompareAndSwapUint64(&v.value, old, new)
    55  }
    56  
    57  // String implements String interface for string printing.
    58  func (v *Uint64) String() string {
    59  	return strconv.FormatUint(v.Val(), 10)
    60  }
    61  
    62  // MarshalJSON implements the interface MarshalJSON for json.Marshal.
    63  func (v Uint64) MarshalJSON() ([]byte, error) {
    64  	return []byte(strconv.FormatUint(v.Val(), 10)), nil
    65  }
    66  
    67  // UnmarshalJSON implements the interface UnmarshalJSON for json.Unmarshal.
    68  func (v *Uint64) UnmarshalJSON(b []byte) error {
    69  	v.Set(gconv.Uint64(string(b)))
    70  	return nil
    71  }
    72  
    73  // UnmarshalValue is an interface implement which sets any type of value for `v`.
    74  func (v *Uint64) UnmarshalValue(value interface{}) error {
    75  	v.Set(gconv.Uint64(value))
    76  	return nil
    77  }
    78  
    79  // DeepCopy implements interface for deep copy of current type.
    80  func (v *Uint64) DeepCopy() interface{} {
    81  	if v == nil {
    82  		return nil
    83  	}
    84  	return NewUint64(v.Val())
    85  }