• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2020 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 #pragma once
17 
18 #include <string>
19 #include <type_traits>
20 
21 #include "common/strings.h"
22 #include "common/type_helper.h"
23 #include "storage/serializable.h"
24 
25 namespace bluetooth {
26 namespace storage {
27 
28 class MutationEntry {
29  public:
30   enum EntryType { SET, REMOVE_PROPERTY, REMOVE_SECTION };
31 
32   enum PropertyType { NORMAL, MEMORY_ONLY };
33 
34   template <typename T, typename std::enable_if<std::is_integral_v<T>, int>::type = 0>
Set(PropertyType property_type,std::string section_param,std::string property_param,T value_param)35   static MutationEntry Set(
36       PropertyType property_type, std::string section_param, std::string property_param, T value_param) {
37     return MutationEntry::Set(
38         property_type, std::move(section_param), std::move(property_param), std::to_string(value_param));
39   }
40 
41   template <typename T, typename std::enable_if<std::is_enum_v<T>, int>::type = 0>
Set(PropertyType property_type,std::string section_param,std::string property_param,T value_param)42   static MutationEntry Set(
43       PropertyType property_type, std::string section_param, std::string property_param, T value_param) {
44     using EnumUnderlyingType = typename std::underlying_type_t<T>;
45     return MutationEntry::Set<EnumUnderlyingType>(
46         property_type,
47         std::move(section_param),
48         std::move(property_param),
49         static_cast<EnumUnderlyingType>(value_param));
50   }
51 
52   template <typename T, typename std::enable_if<std::is_same_v<T, bool>, int>::type = 0>
Set(PropertyType property_type,std::string section_param,std::string property_param,T value_param)53   static MutationEntry Set(
54       PropertyType property_type, std::string section_param, std::string property_param, T value_param) {
55     return MutationEntry::Set(
56         property_type, std::move(section_param), std::move(property_param), common::ToString(value_param));
57   }
58 
59   template <typename T, typename std::enable_if<std::is_same_v<T, std::string>, int>::type = 0>
Set(PropertyType property_type,std::string section_param,std::string property_param,T value_param)60   static MutationEntry Set(
61       PropertyType property_type, std::string section_param, std::string property_param, T value_param) {
62     return MutationEntry::Set(
63         property_type, std::move(section_param), std::move(property_param), std::move(value_param));
64   }
65 
66   template <typename T, typename std::enable_if<std::is_base_of_v<Serializable<T>, T>, int>::type = 0>
Set(PropertyType property_type,std::string section_param,std::string property_param,const T & value_param)67   static MutationEntry Set(
68       PropertyType property_type, std::string section_param, std::string property_param, const T& value_param) {
69     return MutationEntry::Set(
70         property_type, std::move(section_param), std::move(property_param), value_param.ToLegacyConfigString());
71   }
72 
73   template <
74       typename T,
75       typename std::enable_if<
76           bluetooth::common::is_specialization_of<T, std::vector>::value &&
77               std::is_base_of_v<Serializable<typename T::value_type>, typename T::value_type>,
78           int>::type = 0>
Set(PropertyType property_type,std::string section_param,std::string property_param,const T & value_param)79   static MutationEntry Set(
80       PropertyType property_type, std::string section_param, std::string property_param, const T& value_param) {
81     std::vector<std::string> str_values;
82     str_values.reserve(value_param.size());
83     for (const auto& v : value_param) {
84       str_values.push_back(v.ToLegacyConfigString());
85     }
86     return MutationEntry::Set(
87         property_type, std::move(section_param), std::move(property_param), common::StringJoin(str_values, " "));
88   }
89 
Set(PropertyType property_type,std::string section_param,std::string property_param,std::string value_param)90   static MutationEntry Set(
91       PropertyType property_type, std::string section_param, std::string property_param, std::string value_param) {
92     return MutationEntry(
93         EntryType::SET, property_type, std::move(section_param), std::move(property_param), std::move(value_param));
94   }
95 
Remove(PropertyType property_type,std::string section_param)96   static MutationEntry Remove(PropertyType property_type, std::string section_param) {
97     return MutationEntry(EntryType::REMOVE_SECTION, property_type, std::move(section_param));
98   }
99 
Remove(PropertyType property_type,std::string section_param,std::string property_param)100   static MutationEntry Remove(PropertyType property_type, std::string section_param, std::string property_param) {
101     return MutationEntry(
102         EntryType::REMOVE_PROPERTY, property_type, std::move(section_param), std::move(property_param));
103   }
104 
105  private:
106   friend class ConfigCache;
107   friend class Mutation;
108 
109   MutationEntry(
110       EntryType entry_type_param,
111       PropertyType property_type_param,
112       std::string section_param,
113       std::string property_param = "",
114       std::string value_param = "");
115 
116   EntryType entry_type;
117   PropertyType property_type;
118   std::string section;
119   std::string property;
120   std::string value;
121 };
122 
123 }  // namespace storage
124 }  // namespace bluetooth