3 * Copyright (c) 2020 Project CHIP Authors
4 * Copyright (c) 2013-2017 Nest Labs, Inc.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
21 * This file defines functions for manipulating Boolean flags in
35 * Stores bit flags in a type safe manner.
37 * @tparam StorageType is the underlying storage type (like uint16_t, uint32_t etc.)
38 * @tparam FlagsEnum is the typesafe flags setting
40 template <typename StorageType, typename FlagsEnum>
44 static_assert(sizeof(StorageType) >= sizeof(FlagsEnum), "All flags should fit in the storage type");
47 BitFlags(const BitFlags &) = default;
48 BitFlags & operator=(const BitFlags &) = default;
50 explicit BitFlags(FlagsEnum v) { Set(v); }
52 explicit BitFlags(StorageType value) { SetRaw(value); }
54 BitFlags & Set(FlagsEnum v)
56 mValue = static_cast<StorageType>(mValue | static_cast<StorageType>(v));
60 BitFlags & Clear(FlagsEnum v)
62 mValue &= static_cast<StorageType>(~static_cast<StorageType>(v));
66 BitFlags & Set(FlagsEnum v, bool isSet) { return isSet ? Set(v) : Clear(v); }
68 bool Has(FlagsEnum v) const { return (mValue & static_cast<StorageType>(v)) != 0; }
70 bool Has(StorageType other) const { return (mValue & other) == other; }
72 BitFlags & Set(const BitFlags & other)
74 mValue |= other.mValue;
78 StorageType Raw() const { return mValue; }
79 BitFlags & SetRaw(StorageType value)
85 /** Check that no flags outside the arguments are set.*/
86 template <typename... Args>
87 bool HasOnly(Args &&... args) const
89 return IsZeroAfterClearing(mValue, std::forward<Args>(args)...);
93 StorageType mValue = 0;
95 template <typename... Args>
96 static bool IsZeroAfterClearing(StorageType value, FlagsEnum flagToClear, Args &&... args)
98 value &= static_cast<StorageType>(~static_cast<StorageType>(flagToClear));
99 return IsZeroAfterClearing(value, std::forward<Args>(args)...);
102 static bool IsZeroAfterClearing(StorageType value) { return value == 0; }
106 * @deprecated Use typesafe BitFlags class instead.
108 template <typename FlagsT, typename FlagT>
109 inline bool GetFlag(const FlagsT & inFlags, const FlagT inFlag)
111 return (inFlags & static_cast<FlagsT>(inFlag)) != 0;
115 * @deprecated Use typesafe BitFlags class instead.
117 template <typename FlagsT, typename FlagT>
118 inline void ClearFlag(FlagsT & inFlags, const FlagT inFlag)
120 inFlags &= static_cast<FlagsT>(~static_cast<FlagsT>(inFlag));
124 * @deprecated Use typesafe BitFlags class instead.
126 template <typename FlagsT, typename FlagT>
127 inline void SetFlag(FlagsT & inFlags, const FlagT inFlag)
129 inFlags = static_cast<FlagsT>(inFlags | static_cast<FlagsT>(inFlag));
133 * @deprecated Use typesafe BitFlags class instead.
135 template <typename FlagsT, typename FlagT>
136 inline void SetFlag(FlagsT & inFlags, const FlagT inFlag, const bool inValue)
140 SetFlag(inFlags, inFlag);
144 ClearFlag(inFlags, inFlag);