summaryrefslogtreecommitdiff
path: root/property_store.cc
blob: 4254f1a0b7753c4c343b34049255cc2738dec4aa (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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
// Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "shill/property_store.h"

#include <map>
#include <string>
#include <vector>

#include <base/basictypes.h>
#include <base/logging.h>
#include <base/stl_util-inl.h>

#include "shill/error.h"
#include "shill/property_accessor.h"

using std::map;
using std::string;
using std::vector;

namespace shill {

PropertyStore::PropertyStore() {}

PropertyStore::~PropertyStore() {}

bool PropertyStore::Contains(const std::string &prop) {
  return (bool_properties_.find(prop) != bool_properties_.end() ||
          int16_properties_.find(prop) != int16_properties_.end() ||
          int32_properties_.find(prop) != int32_properties_.end() ||
          string_properties_.find(prop) != string_properties_.end() ||
          stringmap_properties_.find(prop) != stringmap_properties_.end() ||
          stringmaps_properties_.find(prop) != stringmaps_properties_.end() ||
          strintpair_properties_.find(prop) != strintpair_properties_.end() ||
          strings_properties_.find(prop) != strings_properties_.end() ||
          uint8_properties_.find(prop) != uint8_properties_.end() ||
          uint16_properties_.find(prop) != uint16_properties_.end() ||
          uint32_properties_.find(prop) != uint32_properties_.end());
}

bool PropertyStore::SetBoolProperty(const std::string& name,
                                    bool value,
                                    Error *error) {
  VLOG(2) << "Setting " << name << " as a bool.";
  bool set = (ContainsKey(bool_properties_, name) &&
              bool_properties_[name]->Set(value));
  if (!set && error)
    error->Populate(Error::kInvalidArguments, name + " is not a R/W bool.");
  return set;
}

bool PropertyStore::SetInt16Property(const std::string& name,
                                     int16 value,
                                     Error *error) {
  VLOG(2) << "Setting " << name << " as an int16.";
  bool set = (ContainsKey(int16_properties_, name) &&
              int16_properties_[name]->Set(value));
  if (!set && error)
    error->Populate(Error::kInvalidArguments, name + " is not a R/W int16.");
  return set;
}

bool PropertyStore::SetInt32Property(const std::string& name,
                                     int32 value,
                                     Error *error) {
  VLOG(2) << "Setting " << name << " as an int32.";
  bool set = (ContainsKey(int32_properties_, name) &&
              int32_properties_[name]->Set(value));
  if (!set && error)
    error->Populate(Error::kInvalidArguments, name + " is not a R/W int32.");
  return set;
}

bool PropertyStore::SetStringProperty(const std::string& name,
                                      const std::string& value,
                                      Error *error) {
  VLOG(2) << "Setting " << name << " as a string.";
  bool set = (ContainsKey(string_properties_, name) &&
              string_properties_[name]->Set(value));
  if (!set && error)
    error->Populate(Error::kInvalidArguments, name + " is not a R/W string.");
  return set;
}

bool PropertyStore::SetStringmapProperty(
    const std::string& name,
    const std::map<std::string, std::string>& values,
    Error *error) {
  VLOG(2) << "Setting " << name << " as a string map.";
  bool set = (ContainsKey(stringmap_properties_, name) &&
              stringmap_properties_[name]->Set(values));
  if (!set && error)
    error->Populate(Error::kInvalidArguments, name + " is not a R/W strmap.");
  return set;
}

bool PropertyStore::SetStringsProperty(const std::string& name,
                                       const std::vector<std::string>& values,
                                       Error *error) {
  VLOG(2) << "Setting " << name << " as a string list.";
  bool set = (ContainsKey(strings_properties_, name) &&
              strings_properties_[name]->Set(values));
  if (!set && error)
    error->Populate(Error::kInvalidArguments, name + " is not a R/W str list.");
  return set;
}

bool PropertyStore::SetUint8Property(const std::string& name,
                                     uint8 value,
                                     Error *error) {
  VLOG(2) << "Setting " << name << " as a uint8.";
  bool set = (ContainsKey(uint8_properties_, name) &&
              uint8_properties_[name]->Set(value));
  if (!set && error)
    error->Populate(Error::kInvalidArguments, name + " is not a R/W uint8.");
  return set;
}

bool PropertyStore::SetUint16Property(const std::string& name,
                                      uint16 value,
                                      Error *error) {
  VLOG(2) << "Setting " << name << " as a uint16.";
  bool set = (ContainsKey(uint16_properties_, name) &&
              uint16_properties_[name]->Set(value));
  if (!set && error)
    error->Populate(Error::kInvalidArguments, name + " is not a R/W uint16.");
  return set;
}

bool PropertyStore::SetUint32Property(const std::string& name,
                                      uint32 value,
                                      Error *error) {
  VLOG(2) << "Setting " << name << " as a uint32.";
  bool set = (ContainsKey(uint32_properties_, name) &&
              uint32_properties_[name]->Set(value));
  if (!set && error)
    error->Populate(Error::kInvalidArguments, name + " is not a R/W uint32.");
  return set;
}

PropertyConstIterator<bool> PropertyStore::GetBoolPropertiesIter() {
  return PropertyConstIterator<bool>(bool_properties_);
}

PropertyConstIterator<int16> PropertyStore::GetInt16PropertiesIter() {
  return PropertyConstIterator<int16>(int16_properties_);
}

PropertyConstIterator<int32> PropertyStore::GetInt32PropertiesIter() {
  return PropertyConstIterator<int32>(int32_properties_);
}

PropertyConstIterator<std::string> PropertyStore::GetStringPropertiesIter() {
  return PropertyConstIterator<std::string>(string_properties_);
}

PropertyConstIterator<Stringmap> PropertyStore::GetStringmapPropertiesIter() {
  return PropertyConstIterator<Stringmap>(stringmap_properties_);
}

PropertyConstIterator<Strings> PropertyStore::GetStringsPropertiesIter() {
  return PropertyConstIterator<Strings>(strings_properties_);
}

PropertyConstIterator<StrIntPair> PropertyStore::GetStrIntPairPropertiesIter() {
  return PropertyConstIterator<StrIntPair>(strintpair_properties_);
}

PropertyConstIterator<uint8> PropertyStore::GetUint8PropertiesIter() {
  return PropertyConstIterator<uint8>(uint8_properties_);
}

PropertyConstIterator<uint16> PropertyStore::GetUint16PropertiesIter() {
  return PropertyConstIterator<uint16>(uint16_properties_);
}

PropertyConstIterator<uint32> PropertyStore::GetUint32PropertiesIter() {
  return PropertyConstIterator<uint32>(uint32_properties_);
}

void PropertyStore::RegisterBool(const string &name, bool *prop) {
  bool_properties_[name] = BoolAccessor(new PropertyAccessor<bool>(prop));
}

void PropertyStore::RegisterConstBool(const string &name, const bool *prop) {
  bool_properties_[name] = BoolAccessor(new ConstPropertyAccessor<bool>(prop));
}

void PropertyStore::RegisterInt16(const string &name, int16 *prop) {
  int16_properties_[name] = Int16Accessor(new PropertyAccessor<int16>(prop));
}

void PropertyStore::RegisterConstInt16(const string &name, const int16 *prop) {
  int16_properties_[name] =
      Int16Accessor(new ConstPropertyAccessor<int16>(prop));
}

void PropertyStore::RegisterInt32(const string &name, int32 *prop) {
  int32_properties_[name] = Int32Accessor(new PropertyAccessor<int32>(prop));
}

void PropertyStore::RegisterConstInt32(const string &name, const int32 *prop) {
  int32_properties_[name] =
      Int32Accessor(new ConstPropertyAccessor<int32>(prop));
}

void PropertyStore::RegisterString(const string &name, string *prop) {
  string_properties_[name] = StringAccessor(new PropertyAccessor<string>(prop));
}

void PropertyStore::RegisterConstString(const string &name,
                                        const string *prop) {
  string_properties_[name] =
      StringAccessor(new ConstPropertyAccessor<string>(prop));
}

void PropertyStore::RegisterStringmap(const string &name, Stringmap *prop) {
  stringmap_properties_[name] =
      StringmapAccessor(new PropertyAccessor<Stringmap>(prop));
}

void PropertyStore::RegisterConstStringmap(const string &name,
                                           const Stringmap *prop) {
  stringmap_properties_[name] =
      StringmapAccessor(new ConstPropertyAccessor<Stringmap>(prop));
}

void PropertyStore::RegisterStrings(const string &name, Strings *prop) {
  strings_properties_[name] =
      StringsAccessor(new PropertyAccessor<Strings>(prop));
}

void PropertyStore::RegisterConstStrings(const string &name,
                                         const Strings *prop) {
  strings_properties_[name] =
      StringsAccessor(new ConstPropertyAccessor<Strings>(prop));
}

void PropertyStore::RegisterUint8(const string &name, uint8 *prop) {
  uint8_properties_[name] = Uint8Accessor(new PropertyAccessor<uint8>(prop));
}

void PropertyStore::RegisterConstUint8(const string &name, const uint8 *prop) {
  uint8_properties_[name] =
      Uint8Accessor(new ConstPropertyAccessor<uint8>(prop));
}

void PropertyStore::RegisterUint16(const std::string &name, uint16 *prop) {
  uint16_properties_[name] = Uint16Accessor(new PropertyAccessor<uint16>(prop));
}

void PropertyStore::RegisterConstUint16(const string &name,
                                        const uint16 *prop) {
  uint16_properties_[name] =
      Uint16Accessor(new ConstPropertyAccessor<uint16>(prop));
}

void PropertyStore::RegisterDerivedBool(const std::string &name,
                                        const BoolAccessor &accessor) {
  bool_properties_[name] = accessor;
}

void PropertyStore::RegisterDerivedString(const std::string &name,
                                          const StringAccessor &accessor) {
  string_properties_[name] = accessor;
}

void PropertyStore::RegisterDerivedStrings(const std::string &name,
                                           const StringsAccessor &accessor) {
  strings_properties_[name] = accessor;
}

void PropertyStore::RegisterDerivedStringmaps(const std::string &name,
                                              const StringmapsAccessor &acc) {
  stringmaps_properties_[name] = acc;
}

void PropertyStore::RegisterDerivedStrIntPair(const std::string &name,
                                              const StrIntPairAccessor &acc) {
  strintpair_properties_[name] = acc;
}

}  // namespace shill