...

Text file src/go.uber.org/atomic/internal/gen-atomicwrapper/wrapper.tmpl

Documentation: go.uber.org/atomic/internal/gen-atomicwrapper

     1// Copyright (c) 2020-{{.ToYear}} Uber Technologies, Inc.
     2//
     3// Permission is hereby granted, free of charge, to any person obtaining a copy
     4// of this software and associated documentation files (the "Software"), to deal
     5// in the Software without restriction, including without limitation the rights
     6// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7// copies of the Software, and to permit persons to whom the Software is
     8// furnished to do so, subject to the following conditions:
     9//
    10// The above copyright notice and this permission notice shall be included in
    11// all copies or substantial portions of the Software.
    12//
    13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19// THE SOFTWARE.
    20
    21package atomic
    22
    23{{ with .Imports }}
    24import (
    25	{{ range . -}}
    26		{{ printf "%q" . }}
    27	{{ end }}
    28)
    29{{ end }}
    30
    31// {{ .Name }} is an atomic type-safe wrapper for {{ .Type }} values.
    32type {{ .Name }} struct{
    33	_ nocmp // disallow non-atomic comparison
    34
    35	v {{ .Wrapped }}
    36}
    37
    38var _zero{{ .Name }} {{ .Type }}
    39
    40
    41// New{{ .Name }} creates a new {{ .Name }}.
    42func New{{ .Name }}(val {{ .Type }}) *{{ .Name }} {
    43	x := &{{ .Name }}{}
    44	if val != _zero{{ .Name }} {
    45		x.Store(val)
    46	}
    47	return x
    48}
    49
    50// Load atomically loads the wrapped {{ .Type }}.
    51func (x *{{ .Name }}) Load() {{ .Type }} {
    52	{{ if .Unpack -}}
    53		return {{ .Unpack }}(x.v.Load())
    54	{{- else -}}
    55		if v := x.v.Load(); v != nil {
    56			return v.({{ .Type }})
    57		}
    58		return _zero{{ .Name }}
    59	{{- end }}
    60}
    61
    62// Store atomically stores the passed {{ .Type }}.
    63func (x *{{ .Name }}) Store(val {{ .Type }}) {
    64	x.v.Store({{ .Pack }}(val))
    65}
    66
    67{{ if .CAS -}}
    68	// CAS is an atomic compare-and-swap for {{ .Type }} values.
    69	//
    70	// Deprecated: Use CompareAndSwap.
    71	func (x *{{ .Name }}) CAS(old, new {{ .Type }}) (swapped bool) {
    72		return x.CompareAndSwap(old, new)
    73	}
    74{{- end }}
    75
    76{{ if .CompareAndSwap -}}
    77	// CompareAndSwap is an atomic compare-and-swap for {{ .Type }} values.
    78	func (x *{{ .Name }}) CompareAndSwap(old, new {{ .Type }}) (swapped bool) {
    79	 	{{ if eq .Wrapped "Value" -}}
    80			if x.v.CompareAndSwap({{ .Pack }}(old), {{ .Pack }}(new)) {
    81				return true
    82			}
    83
    84			if old == _zero{{ .Name }} {
    85				// If the old value is the empty value, then it's possible the 
    86				// underlying Value hasn't been set and is nil, so retry with nil.
    87				return x.v.CompareAndSwap(nil, {{ .Pack }}(new))
    88			}
    89
    90			return false
    91		{{- else -}}
    92			return x.v.CompareAndSwap({{ .Pack }}(old), {{ .Pack }}(new))
    93		{{- end }}
    94	}
    95{{- end }}
    96
    97{{ if .Swap -}}
    98	// Swap atomically stores the given {{ .Type }} and returns the old
    99	// value.
   100	func (x *{{ .Name }}) Swap(val {{ .Type }}) (old {{ .Type }}) {
   101		return {{ .Unpack }}(x.v.Swap({{ .Pack }}(val)))
   102	}
   103{{- end }}
   104
   105{{ if .JSON -}}
   106	// MarshalJSON encodes the wrapped {{ .Type }} into JSON.
   107	func (x *{{ .Name }}) MarshalJSON() ([]byte, error) {
   108		return json.Marshal(x.Load())
   109	}
   110
   111	// UnmarshalJSON decodes a {{ .Type }} from JSON.
   112	func (x *{{ .Name }}) UnmarshalJSON(b []byte) error {
   113		var v {{ .Type }}
   114		if err := json.Unmarshal(b, &v); err != nil {
   115			return err
   116		}
   117		x.Store(v)
   118		return nil
   119	}
   120{{- end }}

View as plain text