Pavel Maltsev | a2f426a | 2016-10-04 10:17:05 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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_vehicle_V2_0_VehicleUtils_H_ |
| 18 | #define android_hardware_vehicle_V2_0_VehicleUtils_H_ |
| 19 | |
| 20 | #include <hidl/HidlSupport.h> |
| 21 | #include <android/hardware/vehicle/2.0/types.h> |
| 22 | |
| 23 | namespace android { |
| 24 | namespace hardware { |
| 25 | namespace vehicle { |
| 26 | namespace V2_0 { |
| 27 | |
| 28 | hidl_string init_hidl_string(const char *cstr) { |
| 29 | hidl_string hidlString; |
| 30 | hidlString = cstr; |
| 31 | return hidlString; |
| 32 | } |
| 33 | |
| 34 | template <typename T> |
| 35 | hidl_vec<T> init_hidl_vec(std::initializer_list<T> values) { |
| 36 | hidl_vec<T> vector; |
| 37 | vector.resize(values.size()); |
| 38 | size_t i = 0; |
| 39 | for (auto& c : values) { |
| 40 | vector[i++] = c; |
| 41 | } |
| 42 | return vector; |
| 43 | } |
| 44 | |
| 45 | // OR operator for class enums. The return type will be enum's underline type. |
| 46 | template <typename ENUM> |
| 47 | typename std::underlying_type<ENUM>::type operator |(ENUM v1, ENUM v2) { |
| 48 | return static_cast<typename std::underlying_type<ENUM>::type>(v1) |
| 49 | | static_cast<typename std::underlying_type<ENUM>::type>(v2); |
| 50 | } |
| 51 | |
| 52 | // AND operator for class enums. The return type will be enum's underline type. |
| 53 | template <typename ENUM> |
| 54 | typename std::underlying_type<ENUM>::type operator &(ENUM v1, ENUM v2) { |
| 55 | return static_cast<typename std::underlying_type<ENUM>::type>(v1) |
| 56 | | static_cast<typename std::underlying_type<ENUM>::type>(v2); |
| 57 | } |
| 58 | |
| 59 | // Returns underlying (integer) value for given enum. |
| 60 | template <typename ENUM> |
| 61 | typename std::underlying_type<ENUM>::type enum_val(ENUM const value) |
| 62 | { |
| 63 | return static_cast<typename std::underlying_type<ENUM>::type>(value); |
| 64 | } |
| 65 | |
| 66 | VehiclePropertyType getPropType(VehicleProperty prop) { |
| 67 | return static_cast<VehiclePropertyType>( |
| 68 | static_cast<int32_t>(prop) & static_cast<int32_t>(VehiclePropertyType::MASK)); |
| 69 | } |
| 70 | |
| 71 | VehiclePropertyGroup getPropGroup(VehicleProperty prop) { |
| 72 | return static_cast<VehiclePropertyGroup>( |
| 73 | static_cast<int32_t>(prop) & static_cast<int32_t>(VehiclePropertyGroup::MASK)); |
| 74 | } |
| 75 | |
| 76 | bool checkPropType(VehicleProperty prop, VehiclePropertyType type) { |
| 77 | return getPropType(prop) == type; |
| 78 | } |
| 79 | |
| 80 | bool isSystemProperty(VehicleProperty prop) { |
| 81 | return VehiclePropertyGroup::SYSTEM == getPropGroup(prop); |
| 82 | } |
| 83 | |
| 84 | |
| 85 | } // namespace V2_0 |
| 86 | } // namespace vehicle |
| 87 | } // namespace hardware |
| 88 | } // namespace android |
| 89 | |
| 90 | #endif android_hardware_vehicle_V2_0_VehicleUtils_H_ |