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