aboutsummaryrefslogtreecommitdiff
path: root/gd/storage/mutation_entry.h
blob: fcb902c77fd2c8f3ed50f1d9957b7e93e99c1d8b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*
 * 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 <string>
#include <type_traits>

#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 <typename T, typename std::enable_if<std::is_integral_v<T>, 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 <typename T, typename std::enable_if<std::is_enum_v<T>, 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<T>;
    return MutationEntry::Set<EnumUnderlyingType>(
        property_type,
        std::move(section_param),
        std::move(property_param),
        static_cast<EnumUnderlyingType>(value_param));
  }

  template <typename T, typename std::enable_if<std::is_same_v<T, bool>, 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 <typename T, typename std::enable_if<std::is_same_v<T, std::string>, 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 <typename T, typename std::enable_if<std::is_base_of_v<Serializable<T>, 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<T, std::vector>::value &&
              std::is_base_of_v<Serializable<typename T::value_type>, 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<std::string> 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