Elia Battiston | c8d0d46 | 2022-02-22 16:30:51 +0100 | [diff] [blame] | 1 | // @generated Code generated by gen-atomicwrapper. |
| 2 | |
| 3 | // Copyright (c) 2020 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 | "math" |
| 28 | ) |
| 29 | |
| 30 | // Float64 is an atomic type-safe wrapper for float64 values. |
| 31 | type Float64 struct { |
| 32 | _ nocmp // disallow non-atomic comparison |
| 33 | |
| 34 | v Uint64 |
| 35 | } |
| 36 | |
| 37 | var _zeroFloat64 float64 |
| 38 | |
| 39 | // NewFloat64 creates a new Float64. |
| 40 | func NewFloat64(v float64) *Float64 { |
| 41 | x := &Float64{} |
| 42 | if v != _zeroFloat64 { |
| 43 | x.Store(v) |
| 44 | } |
| 45 | return x |
| 46 | } |
| 47 | |
| 48 | // Load atomically loads the wrapped float64. |
| 49 | func (x *Float64) Load() float64 { |
| 50 | return math.Float64frombits(x.v.Load()) |
| 51 | } |
| 52 | |
| 53 | // Store atomically stores the passed float64. |
| 54 | func (x *Float64) Store(v float64) { |
| 55 | x.v.Store(math.Float64bits(v)) |
| 56 | } |
| 57 | |
| 58 | // CAS is an atomic compare-and-swap for float64 values. |
| 59 | func (x *Float64) CAS(o, n float64) bool { |
| 60 | return x.v.CAS(math.Float64bits(o), math.Float64bits(n)) |
| 61 | } |
| 62 | |
| 63 | // MarshalJSON encodes the wrapped float64 into JSON. |
| 64 | func (x *Float64) MarshalJSON() ([]byte, error) { |
| 65 | return json.Marshal(x.Load()) |
| 66 | } |
| 67 | |
| 68 | // UnmarshalJSON decodes a float64 from JSON. |
| 69 | func (x *Float64) UnmarshalJSON(b []byte) error { |
| 70 | var v float64 |
| 71 | if err := json.Unmarshal(b, &v); err != nil { |
| 72 | return err |
| 73 | } |
| 74 | x.Store(v) |
| 75 | return nil |
| 76 | } |