uint32.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // @generated Code generated by gen-atomicint.
  2. // Copyright (c) 2020-2023 Uber Technologies, Inc.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. package atomic
  22. import (
  23. "encoding/json"
  24. "strconv"
  25. "sync/atomic"
  26. )
  27. // Uint32 is an atomic wrapper around uint32.
  28. type Uint32 struct {
  29. _ nocmp // disallow non-atomic comparison
  30. v uint32
  31. }
  32. // NewUint32 creates a new Uint32.
  33. func NewUint32(val uint32) *Uint32 {
  34. return &Uint32{v: val}
  35. }
  36. // Load atomically loads the wrapped value.
  37. func (i *Uint32) Load() uint32 {
  38. return atomic.LoadUint32(&i.v)
  39. }
  40. // Add atomically adds to the wrapped uint32 and returns the new value.
  41. func (i *Uint32) Add(delta uint32) uint32 {
  42. return atomic.AddUint32(&i.v, delta)
  43. }
  44. // Sub atomically subtracts from the wrapped uint32 and returns the new value.
  45. func (i *Uint32) Sub(delta uint32) uint32 {
  46. return atomic.AddUint32(&i.v, ^(delta - 1))
  47. }
  48. // Inc atomically increments the wrapped uint32 and returns the new value.
  49. func (i *Uint32) Inc() uint32 {
  50. return i.Add(1)
  51. }
  52. // Dec atomically decrements the wrapped uint32 and returns the new value.
  53. func (i *Uint32) Dec() uint32 {
  54. return i.Sub(1)
  55. }
  56. // CAS is an atomic compare-and-swap.
  57. //
  58. // Deprecated: Use CompareAndSwap.
  59. func (i *Uint32) CAS(old, new uint32) (swapped bool) {
  60. return i.CompareAndSwap(old, new)
  61. }
  62. // CompareAndSwap is an atomic compare-and-swap.
  63. func (i *Uint32) CompareAndSwap(old, new uint32) (swapped bool) {
  64. return atomic.CompareAndSwapUint32(&i.v, old, new)
  65. }
  66. // Store atomically stores the passed value.
  67. func (i *Uint32) Store(val uint32) {
  68. atomic.StoreUint32(&i.v, val)
  69. }
  70. // Swap atomically swaps the wrapped uint32 and returns the old value.
  71. func (i *Uint32) Swap(val uint32) (old uint32) {
  72. return atomic.SwapUint32(&i.v, val)
  73. }
  74. // MarshalJSON encodes the wrapped uint32 into JSON.
  75. func (i *Uint32) MarshalJSON() ([]byte, error) {
  76. return json.Marshal(i.Load())
  77. }
  78. // UnmarshalJSON decodes JSON into the wrapped uint32.
  79. func (i *Uint32) UnmarshalJSON(b []byte) error {
  80. var v uint32
  81. if err := json.Unmarshal(b, &v); err != nil {
  82. return err
  83. }
  84. i.Store(v)
  85. return nil
  86. }
  87. // String encodes the wrapped value as a string.
  88. func (i *Uint32) String() string {
  89. v := i.Load()
  90. return strconv.FormatUint(uint64(v), 10)
  91. }