github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/pkg/util/bitflag.go (about)

     1  // Copyright 2020 PingCAP, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package util
    15  
    16  // Flag is a uint64 flag to show a 64 bit mask
    17  type Flag uint64
    18  
    19  // HasAll means has all flags
    20  func (f *Flag) HasAll(flags ...Flag) bool {
    21  	for _, flag := range flags {
    22  		if flag&*f == 0 {
    23  			return false
    24  		}
    25  	}
    26  	return true
    27  }
    28  
    29  // HasOne means has one of the flags
    30  func (f *Flag) HasOne(flags ...Flag) bool {
    31  	for _, flag := range flags {
    32  		if flag&*f != 0 {
    33  			return true
    34  		}
    35  	}
    36  	return false
    37  }
    38  
    39  // Add add flags
    40  func (f *Flag) Add(flags ...Flag) {
    41  	for _, flag := range flags {
    42  		*f |= flag
    43  	}
    44  }
    45  
    46  // Remove remove flags
    47  func (f *Flag) Remove(flags ...Flag) {
    48  	for _, flag := range flags {
    49  		*f ^= flag
    50  	}
    51  }
    52  
    53  // Clear clear all flags
    54  func (f *Flag) Clear() {
    55  	*f ^= *f
    56  }