@@ -28,6 +28,7 @@ package main
28
28
29
29
import (
30
30
"bytes"
31
+ "embed"
31
32
"errors"
32
33
"flag"
33
34
"fmt"
@@ -93,7 +94,7 @@ func run(args []string) error {
93
94
}
94
95
95
96
var buff bytes.Buffer
96
- if err := _tmpl .Execute (& buff , data ); err != nil {
97
+ if err := _tmpl .ExecuteTemplate (& buff , "wrapper.tmpl" , data ); err != nil {
97
98
return fmt .Errorf ("render template: %v" , err )
98
99
}
99
100
@@ -102,127 +103,14 @@ func run(args []string) error {
102
103
return fmt .Errorf ("reformat source: %v" , err )
103
104
}
104
105
106
+ io .WriteString (w , "// @generated Code generated by gen-atomicint.\n \n " )
105
107
_ , err = w .Write (bs )
106
108
return err
107
109
}
108
110
109
- var _tmpl = template .Must (template .New ("value.go" ).Parse (`// @generated Code generated by gen-atomicint.
111
+ var (
112
+ //go:embed *.tmpl
113
+ _tmplFS embed.FS
110
114
111
- // Copyright (c) 2020-{{.ToYear}} Uber Technologies, Inc.
112
- //
113
- // Permission is hereby granted, free of charge, to any person obtaining a copy
114
- // of this software and associated documentation files (the "Software"), to deal
115
- // in the Software without restriction, including without limitation the rights
116
- // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
117
- // copies of the Software, and to permit persons to whom the Software is
118
- // furnished to do so, subject to the following conditions:
119
- //
120
- // The above copyright notice and this permission notice shall be included in
121
- // all copies or substantial portions of the Software.
122
- //
123
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
124
- // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
125
- // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
126
- // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
127
- // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
128
- // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
129
- // THE SOFTWARE.
130
-
131
- package atomic
132
-
133
- import (
134
- "encoding/json"
135
- "strconv"
136
- "sync/atomic"
115
+ _tmpl = template .Must (template .New ("atomicint" ).ParseFS (_tmplFS , "*.tmpl" ))
137
116
)
138
-
139
- // {{ .Name }} is an atomic wrapper around {{ .Wrapped }}.
140
- type {{ .Name }} struct {
141
- _ nocmp // disallow non-atomic comparison
142
-
143
- v {{ .Wrapped }}
144
- }
145
-
146
- // New{{ .Name }} creates a new {{ .Name }}.
147
- func New{{ .Name }}(val {{ .Wrapped }}) *{{ .Name }} {
148
- return &{{ .Name }}{v: val}
149
- }
150
-
151
- // Load atomically loads the wrapped value.
152
- func (i *{{ .Name }}) Load() {{ .Wrapped }} {
153
- return atomic.Load{{ .Name }}(&i.v)
154
- }
155
-
156
- // Add atomically adds to the wrapped {{ .Wrapped }} and returns the new value.
157
- func (i *{{ .Name }}) Add(delta {{ .Wrapped }}) {{ .Wrapped }} {
158
- return atomic.Add{{ .Name }}(&i.v, delta)
159
- }
160
-
161
- // Sub atomically subtracts from the wrapped {{ .Wrapped }} and returns the new value.
162
- func (i *{{ .Name }}) Sub(delta {{ .Wrapped }}) {{ .Wrapped }} {
163
- return atomic.Add{{ .Name }}(&i.v,
164
- {{- if .Unsigned -}}
165
- ^(delta - 1)
166
- {{- else -}}
167
- -delta
168
- {{- end -}}
169
- )
170
- }
171
-
172
- // Inc atomically increments the wrapped {{ .Wrapped }} and returns the new value.
173
- func (i *{{ .Name }}) Inc() {{ .Wrapped }} {
174
- return i.Add(1)
175
- }
176
-
177
- // Dec atomically decrements the wrapped {{ .Wrapped }} and returns the new value.
178
- func (i *{{ .Name }}) Dec() {{ .Wrapped }} {
179
- return i.Sub(1)
180
- }
181
-
182
- // CAS is an atomic compare-and-swap.
183
- //
184
- // Deprecated: Use CompareAndSwap.
185
- func (i *{{ .Name }}) CAS(old, new {{ .Wrapped }}) (swapped bool) {
186
- return i.CompareAndSwap(old, new)
187
- }
188
-
189
- // CompareAndSwap is an atomic compare-and-swap.
190
- func (i *{{ .Name }}) CompareAndSwap(old, new {{ .Wrapped }}) (swapped bool) {
191
- return atomic.CompareAndSwap{{ .Name }}(&i.v, old, new)
192
- }
193
-
194
- // Store atomically stores the passed value.
195
- func (i *{{ .Name }}) Store(val {{ .Wrapped }}) {
196
- atomic.Store{{ .Name }}(&i.v, val)
197
- }
198
-
199
- // Swap atomically swaps the wrapped {{ .Wrapped }} and returns the old value.
200
- func (i *{{ .Name }}) Swap(val {{ .Wrapped }}) (old {{ .Wrapped }}) {
201
- return atomic.Swap{{ .Name }}(&i.v, val)
202
- }
203
-
204
- // MarshalJSON encodes the wrapped {{ .Wrapped }} into JSON.
205
- func (i *{{ .Name }}) MarshalJSON() ([]byte, error) {
206
- return json.Marshal(i.Load())
207
- }
208
-
209
- // UnmarshalJSON decodes JSON into the wrapped {{ .Wrapped }}.
210
- func (i *{{ .Name }}) UnmarshalJSON(b []byte) error {
211
- var v {{ .Wrapped }}
212
- if err := json.Unmarshal(b, &v); err != nil {
213
- return err
214
- }
215
- i.Store(v)
216
- return nil
217
- }
218
-
219
- // String encodes the wrapped value as a string.
220
- func (i *{{ .Name }}) String() string {
221
- v := i.Load()
222
- {{ if .Unsigned -}}
223
- return strconv.FormatUint(uint64(v), 10)
224
- {{- else -}}
225
- return strconv.FormatInt(int64(v), 10)
226
- {{- end }}
227
- }
228
- ` ))
0 commit comments