/* * Copyright 2020 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once #include #include #include "common/strings.h" #include "common/type_helper.h" #include "storage/serializable.h" namespace bluetooth { namespace storage { class MutationEntry { public: enum EntryType { SET, REMOVE_PROPERTY, REMOVE_SECTION }; enum PropertyType { NORMAL, MEMORY_ONLY }; template , int>::type = 0> static MutationEntry Set( PropertyType property_type, std::string section_param, std::string property_param, T value_param) { return MutationEntry::Set( property_type, std::move(section_param), std::move(property_param), std::to_string(value_param)); } template , int>::type = 0> static MutationEntry Set( PropertyType property_type, std::string section_param, std::string property_param, T value_param) { using EnumUnderlyingType = typename std::underlying_type_t; return MutationEntry::Set( property_type, std::move(section_param), std::move(property_param), static_cast(value_param)); } template , int>::type = 0> static MutationEntry Set( PropertyType property_type, std::string section_param, std::string property_param, T value_param) { return MutationEntry::Set( property_type, std::move(section_param), std::move(property_param), common::ToString(value_param)); } template , int>::type = 0> static MutationEntry Set( PropertyType property_type, std::string section_param, std::string property_param, T value_param) { return MutationEntry::Set( property_type, std::move(section_param), std::move(property_param), std::move(value_param)); } template , T>, int>::type = 0> static MutationEntry Set( PropertyType property_type, std::string section_param, std::string property_param, const T& value_param) { return MutationEntry::Set( property_type, std::move(section_param), std::move(property_param), value_param.ToLegacyConfigString()); } template < typename T, typename std::enable_if< bluetooth::common::is_specialization_of::value && std::is_base_of_v, typename T::value_type>, int>::type = 0> static MutationEntry Set( PropertyType property_type, std::string section_param, std::string property_param, const T& value_param) { std::vector str_values; str_values.reserve(value_param.size()); for (const auto& v : value_param) { str_values.push_back(v.ToLegacyConfigString()); } return MutationEntry::Set( property_type, std::move(section_param), std::move(property_param), common::StringJoin(str_values, " ")); } static MutationEntry Set( PropertyType property_type, std::string section_param, std::string property_param, std::string value_param) { return MutationEntry( EntryType::SET, property_type, std::move(section_param), std::move(property_param), std::move(value_param)); } static MutationEntry Remove(PropertyType property_type, std::string section_param) { return MutationEntry(EntryType::REMOVE_SECTION, property_type, std::move(section_param)); } static MutationEntry Remove(PropertyType property_type, std::string section_param, std::string property_param) { return MutationEntry( EntryType::REMOVE_PROPERTY, property_type, std::move(section_param), std::move(property_param)); } private: friend class ConfigCache; friend class Mutation; MutationEntry( EntryType entry_type_param, PropertyType property_type_param, std::string section_param, std::string property_param = "", std::string value_param = ""); EntryType entry_type; PropertyType property_type; std::string section; std::string property; std::string value; }; } // namespace storage } // namespace bluetooth