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_automotive_vehicle_V2_0_VehiclePropConfigIndex_H_
18 #define android_hardware_automotive_vehicle_V2_0_VehiclePropConfigIndex_H_
19 
20 #include <utils/KeyedVector.h>
21 
22 #include <android/hardware/automotive/vehicle/2.0/IVehicle.h>
23 
24 namespace android {
25 namespace hardware {
26 namespace automotive {
27 namespace vehicle {
28 namespace V2_0 {
29 
30 /*
31  * This is thread-safe immutable class to hold vehicle property configuration
32  * data.
33  */
34 class VehiclePropConfigIndex {
35 public:
VehiclePropConfigIndex(const std::vector<VehiclePropConfig> & properties)36     VehiclePropConfigIndex(
37         const std::vector<VehiclePropConfig>& properties)
38         : mConfigs(properties), mPropToConfig(mConfigs)
39     {}
40 
hasConfig(int32_t property)41     bool hasConfig(int32_t property) const {
42         return mPropToConfig.indexOfKey(property) >= 0;
43     }
44 
getConfig(int32_t property)45     const VehiclePropConfig& getConfig(int32_t property) const {
46         return *mPropToConfig.valueFor(property);
47     }
48 
getAllConfigs()49     const std::vector<VehiclePropConfig>& getAllConfigs() const {
50         return mConfigs;
51     }
52 
53 private:
54     typedef KeyedVector<int32_t, const VehiclePropConfig*> PropConfigMap;
55     class ImmutablePropConfigMap : private PropConfigMap {
56     public:
ImmutablePropConfigMap(const std::vector<VehiclePropConfig> & configs)57         ImmutablePropConfigMap(const std::vector<VehiclePropConfig>& configs) {
58             setCapacity(configs.size());
59             for (auto& config : configs) {
60                 add(config.prop, &config);
61             }
62         }
63     public:
64         using PropConfigMap::valueFor;
65         using PropConfigMap::indexOfKey;
66     };
67 
68 private:
69     const std::vector<VehiclePropConfig> mConfigs;
70     const ImmutablePropConfigMap mPropToConfig;  // mConfigs must be declared
71                                                  // first.
72 };
73 
74 }  // namespace V2_0
75 }  // namespace vehicle
76 }  // namespace automotive
77 }  // namespace hardware
78 }  // namespace android
79 
80 #endif // android_hardware_automotive_vehicle_V2_0_VehiclePropConfigIndex_H_
81