1 /*
2  * Copyright (C) 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef android_hardware_audio_common_VersionUtils_H_
18 #define android_hardware_audio_common_VersionUtils_H_
19 
20 #include <hidl/HidlSupport.h>
21 #include <type_traits>
22 
23 namespace android {
24 namespace hardware {
25 namespace audio {
26 namespace common {
27 namespace utils {
28 
29 /** Converting between a bitfield or itself. */
30 template <class Enum>
31 class EnumBitfield {
32    public:
33     using Bitfield = ::android::hardware::hidl_bitfield<Enum>;
34 
35     EnumBitfield(const EnumBitfield&) = default;
EnumBitfield(Enum value)36     explicit EnumBitfield(Enum value) : mValue(value) {}
EnumBitfield(Bitfield value)37     explicit EnumBitfield(Bitfield value) : EnumBitfield(static_cast<Enum>(value)) {}
38 
39     EnumBitfield& operator=(const EnumBitfield&) = default;
40     EnumBitfield& operator=(Enum value) { return *this = EnumBitfield{value}; }
41     EnumBitfield& operator=(Bitfield value) { return *this = EnumBitfield{value}; }
42 
Enum()43     operator Enum() const { return mValue; }
Bitfield()44     operator Bitfield() const { return static_cast<Bitfield>(mValue); }
45 
46    private:
47     Enum mValue;
48 };
49 
50 /** ATD way to create a EnumBitfield. */
51 template <class Enum>
mkEnumBitfield(Enum value)52 EnumBitfield<Enum> mkEnumBitfield(Enum value) {
53     return EnumBitfield<Enum>{value};
54 }
55 
56 }  // namespace utils
57 }  // namespace common
58 }  // namespace audio
59 }  // namespace hardware
60 }  // namespace android
61 
62 #endif  // android_hardware_audio_common_VersionUtils_H_
63