blob: 2bd9f80ffb38c5443c7383b2e51d5494e790aea5 [file] [log] [blame]
{{/*
// Copyright 2019 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
*/}}
{{- define "BitsDefinition" -}}
{{- $bits := . }}
{{ range $comment := $bits.DocComments }}
//{{ $comment }}
{{- end }}
type {{ $bits.Name }} {{ $bits.Type }}
const (
{{- range $memb := $bits.Members }}
{{ $bits.Name }}{{ $memb.Name }} {{ $bits.Name }} = {{ $memb.Value }}
{{- end }}
{{ $bits.Name }}_Mask {{ $bits.Name }} = {{ $bits.Mask }}
)
func (_ {{ $bits.Name }}) I_BitsMask() {{ $bits.Name }} {
return {{ $bits.Name }}_Mask
}
func (_ {{ $bits.Name }}) I_BitsIsStrict() bool {
return {{ $bits.IsStrict }}
}
func (x {{ $bits.Name }}) HasUnknownBits() bool {
return x.GetUnknownBits() != 0
}
func (x {{ $bits.Name }}) GetUnknownBits() uint64 {
return uint64(^{{ $bits.Name }}_Mask & x)
}
func (x {{ $bits.Name }}) InvertBits() {{ $bits.Name }} {
return {{ $bits.Name }}_Mask & ^x
}
// HasBits validates that all flipped bits in the mask are set.
func (x {{ $bits.Name }}) HasBits(mask {{ $bits.Name }}) bool {
return mask | x == x
}
// ClearBits ensures all flipped bits in the mask are unset.
func (x {{ $bits.Name }}) ClearBits(mask {{ $bits.Name }}) {{ $bits.Name }} {
return ^mask & x
}
func (x {{ $bits.Name }}) String() string {
var buf _strings.Builder
{{- range $memb := $bits.Members }}
if {{ $memb.Value }} & x != 0 {
if buf.Len() != 0 {
buf.WriteRune('|')
}
buf.WriteString("{{ $memb.Name }}")
}
{{- end }}
if buf.Len() == 0 {
buf.WriteString("<empty bits>")
}
return buf.String()
}
{{- end -}}