blob: 6b2e449fe5381bdac9c5d8cd021254074454f99f [file] [log] [blame]
mukesh agrawal4d0401c2012-01-06 16:05:31 -08001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Chris Masoneb925cc82011-06-22 15:39:57 -07002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "shill/property_store.h"
6
7#include <map>
8#include <string>
9#include <vector>
10
11#include <base/basictypes.h>
Eric Shienbrood3e20a232012-02-16 11:35:56 -050012#include <base/stl_util.h>
Chris Masoneb925cc82011-06-22 15:39:57 -070013
14#include "shill/error.h"
Christopher Wileyb691efd2012-08-09 13:51:51 -070015#include "shill/logging.h"
Chris Masoneb925cc82011-06-22 15:39:57 -070016#include "shill/property_accessor.h"
17
18using std::map;
19using std::string;
20using std::vector;
21
22namespace shill {
23
24PropertyStore::PropertyStore() {}
25
26PropertyStore::~PropertyStore() {}
27
mukesh agrawal66b0aca2012-01-30 15:28:28 -080028bool PropertyStore::Contains(const string &prop) const {
29 return (ContainsKey(bool_properties_, prop) ||
30 ContainsKey(int16_properties_, prop) ||
31 ContainsKey(int32_properties_, prop) ||
32 ContainsKey(key_value_store_properties_, prop) ||
33 ContainsKey(string_properties_, prop) ||
34 ContainsKey(stringmap_properties_, prop) ||
35 ContainsKey(stringmaps_properties_, prop) ||
36 ContainsKey(strings_properties_, prop) ||
37 ContainsKey(uint8_properties_, prop) ||
38 ContainsKey(uint16_properties_, prop) ||
Jason Glasgowacdc11f2012-03-30 14:12:22 -040039 ContainsKey(uint32_properties_, prop) ||
Paul Stewarte18c33b2012-07-10 20:48:44 -070040 ContainsKey(uint64_properties_, prop) ||
Jason Glasgowacdc11f2012-03-30 14:12:22 -040041 ContainsKey(rpc_identifier_properties_, prop) ||
42 ContainsKey(rpc_identifiers_properties_, prop));
Chris Masoneb925cc82011-06-22 15:39:57 -070043}
44
Paul Stewarte6e8e492013-01-17 11:00:50 -080045bool PropertyStore::GetBoolProperty(const string &name,
46 bool *value,
47 Error *error) const {
48 return GetProperty(name, value, error, bool_properties_, "a bool");
49}
50
51bool PropertyStore::GetInt16Property(const string &name,
52 int16 *value,
53 Error *error) const {
54 return GetProperty(name, value, error, int16_properties_, "an int16");
55}
56
57bool PropertyStore::GetInt32Property(const string &name,
58 int32 *value,
59 Error *error) const {
60 return GetProperty(name, value, error, int32_properties_, "an int32");
61}
62
63bool PropertyStore::GetKeyValueStoreProperty(const string &name,
64 KeyValueStore *value,
65 Error *error) const {
66 return GetProperty(name, value, error, key_value_store_properties_,
67 "a key value store");
68}
69
70bool PropertyStore::GetRpcIdentifierProperty(const string &name,
71 RpcIdentifier *value,
72 Error *error) const {
73 return GetProperty(name, value, error, rpc_identifier_properties_,
74 "an rpc_identifier");
75}
76
77bool PropertyStore::GetStringProperty(const string &name,
78 string *value,
79 Error *error) const {
80 return GetProperty(name, value, error, string_properties_, "a string");
81}
82
83bool PropertyStore::GetStringmapProperty(const string &name,
84 Stringmap *values,
85 Error *error) const {
86 return GetProperty(name, values, error, stringmap_properties_,
87 "a string map");
88}
89
90bool PropertyStore::GetStringmapsProperty(const string &name,
91 Stringmaps *values,
92 Error *error) const {
93 return GetProperty(name, values, error, stringmaps_properties_,
94 "a string map list");
95}
96
97bool PropertyStore::GetStringsProperty(const string &name,
98 Strings *values,
99 Error *error) const {
100 return GetProperty(name, values, error, strings_properties_, "a string list");
101}
102
103bool PropertyStore::GetUint8Property(const string &name,
104 uint8 *value,
105 Error *error) const {
106 return GetProperty(name, value, error, uint8_properties_, "a uint8");
107}
108
109bool PropertyStore::GetUint16Property(const string &name,
110 uint16 *value,
111 Error *error) const {
112 return GetProperty(name, value, error, uint16_properties_, "a uint16");
113}
114
115bool PropertyStore::GetUint32Property(const string &name,
116 uint32 *value,
117 Error *error) const {
118 return GetProperty(name, value, error, uint32_properties_, "a uint32");
119}
120
121bool PropertyStore::GetUint64Property(const string &name,
122 uint64 *value,
123 Error *error) const {
124 return GetProperty(name, value, error, uint64_properties_, "a uint64");
125}
126
mukesh agrawal66b0aca2012-01-30 15:28:28 -0800127bool PropertyStore::SetBoolProperty(const string &name,
Chris Masoneb925cc82011-06-22 15:39:57 -0700128 bool value,
129 Error *error) {
mukesh agrawalffa3d042011-10-06 15:26:10 -0700130 return SetProperty(name, value, error, bool_properties_, "a bool");
Chris Masoneb925cc82011-06-22 15:39:57 -0700131}
132
mukesh agrawal66b0aca2012-01-30 15:28:28 -0800133bool PropertyStore::SetInt16Property(const string &name,
Chris Masoneb925cc82011-06-22 15:39:57 -0700134 int16 value,
135 Error *error) {
mukesh agrawalffa3d042011-10-06 15:26:10 -0700136 return SetProperty(name, value, error, int16_properties_, "an int16");
Chris Masoneb925cc82011-06-22 15:39:57 -0700137}
138
mukesh agrawal66b0aca2012-01-30 15:28:28 -0800139bool PropertyStore::SetInt32Property(const string &name,
Chris Masoneb925cc82011-06-22 15:39:57 -0700140 int32 value,
141 Error *error) {
mukesh agrawalffa3d042011-10-06 15:26:10 -0700142 return SetProperty(name, value, error, int32_properties_, "an int32.");
Chris Masoneb925cc82011-06-22 15:39:57 -0700143}
144
mukesh agrawal66b0aca2012-01-30 15:28:28 -0800145bool PropertyStore::SetStringProperty(const string &name,
146 const string &value,
Chris Masoneb925cc82011-06-22 15:39:57 -0700147 Error *error) {
mukesh agrawalffa3d042011-10-06 15:26:10 -0700148 return SetProperty(name, value, error, string_properties_, "a string");
Chris Masoneb925cc82011-06-22 15:39:57 -0700149}
150
mukesh agrawal66b0aca2012-01-30 15:28:28 -0800151bool PropertyStore::SetStringmapProperty(const string &name,
152 const map<string, string> &values,
153 Error *error) {
mukesh agrawalffa3d042011-10-06 15:26:10 -0700154 return SetProperty(name, values, error, stringmap_properties_,
155 "a string map");
Chris Masoneb925cc82011-06-22 15:39:57 -0700156}
157
mukesh agrawal66b0aca2012-01-30 15:28:28 -0800158bool PropertyStore::SetStringsProperty(const string &name,
159 const vector<string> &values,
Chris Masoneb925cc82011-06-22 15:39:57 -0700160 Error *error) {
mukesh agrawalffa3d042011-10-06 15:26:10 -0700161 return SetProperty(name, values, error, strings_properties_, "a string list");
Chris Masoneb925cc82011-06-22 15:39:57 -0700162}
163
mukesh agrawal66b0aca2012-01-30 15:28:28 -0800164bool PropertyStore::SetUint8Property(const string &name,
Chris Masoneb925cc82011-06-22 15:39:57 -0700165 uint8 value,
166 Error *error) {
mukesh agrawalffa3d042011-10-06 15:26:10 -0700167 return SetProperty(name, value, error, uint8_properties_, "a uint8");
Chris Masoneb925cc82011-06-22 15:39:57 -0700168}
169
mukesh agrawal66b0aca2012-01-30 15:28:28 -0800170bool PropertyStore::SetUint16Property(const string &name,
Chris Masoneb925cc82011-06-22 15:39:57 -0700171 uint16 value,
172 Error *error) {
mukesh agrawalffa3d042011-10-06 15:26:10 -0700173 return SetProperty(name, value, error, uint16_properties_, "a uint16");
Chris Masoneb925cc82011-06-22 15:39:57 -0700174}
175
mukesh agrawal66b0aca2012-01-30 15:28:28 -0800176bool PropertyStore::SetUint32Property(const string &name,
Chris Masoneb925cc82011-06-22 15:39:57 -0700177 uint32 value,
178 Error *error) {
mukesh agrawalffa3d042011-10-06 15:26:10 -0700179 return SetProperty(name, value, error, uint32_properties_, "a uint32");
Chris Masoneb925cc82011-06-22 15:39:57 -0700180}
181
Paul Stewarte18c33b2012-07-10 20:48:44 -0700182bool PropertyStore::SetUint64Property(const string &name,
183 uint64 value,
184 Error *error) {
185 return SetProperty(name, value, error, uint64_properties_, "a uint64");
186}
187
Jason Glasgowacdc11f2012-03-30 14:12:22 -0400188bool PropertyStore::SetRpcIdentifierProperty(const string &name,
189 const RpcIdentifier &value,
190 Error *error) {
191 return SetProperty(name, value, error, rpc_identifier_properties_,
192 "an rpc_identifier");
193}
194
mukesh agrawal4d260da2012-01-30 11:53:52 -0800195bool PropertyStore::ClearProperty(const string &name, Error *error) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700196 SLOG(Property, 2) << "Clearing " << name << ".";
mukesh agrawal4d260da2012-01-30 11:53:52 -0800197
198 if (ContainsKey(bool_properties_, name)) {
199 bool_properties_[name]->Clear(error);
200 } else if (ContainsKey(int16_properties_, name)) {
201 int16_properties_[name]->Clear(error);
202 } else if (ContainsKey(int32_properties_, name)) {
203 int32_properties_[name]->Clear(error);
204 } else if (ContainsKey(key_value_store_properties_, name)) {
205 key_value_store_properties_[name]->Clear(error);
206 } else if (ContainsKey(string_properties_, name)) {
207 string_properties_[name]->Clear(error);
208 } else if (ContainsKey(stringmap_properties_, name)) {
209 stringmap_properties_[name]->Clear(error);
210 } else if (ContainsKey(stringmaps_properties_, name)) {
211 stringmaps_properties_[name]->Clear(error);
212 } else if (ContainsKey(strings_properties_, name)) {
213 strings_properties_[name]->Clear(error);
214 } else if (ContainsKey(uint8_properties_, name)) {
215 uint8_properties_[name]->Clear(error);
216 } else if (ContainsKey(uint16_properties_, name)) {
217 uint16_properties_[name]->Clear(error);
218 } else if (ContainsKey(uint32_properties_, name)) {
219 uint32_properties_[name]->Clear(error);
Paul Stewarte18c33b2012-07-10 20:48:44 -0700220 } else if (ContainsKey(uint64_properties_, name)) {
221 uint64_properties_[name]->Clear(error);
Jason Glasgowacdc11f2012-03-30 14:12:22 -0400222 } else if (ContainsKey(rpc_identifier_properties_, name)) {
223 rpc_identifier_properties_[name]->Clear(error);
224 } else if (ContainsKey(rpc_identifiers_properties_, name)) {
225 rpc_identifiers_properties_[name]->Clear(error);
mukesh agrawal4d260da2012-01-30 11:53:52 -0800226 } else {
227 error->Populate(
228 Error::kInvalidProperty, "Property " + name + " does not exist.");
229 }
230 return error->IsSuccess();
231}
232
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800233ReadablePropertyConstIterator<bool> PropertyStore::GetBoolPropertiesIter()
mukesh agrawalde29fa82011-09-16 16:16:36 -0700234 const {
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800235 return ReadablePropertyConstIterator<bool>(bool_properties_);
Chris Masonea8a2c252011-06-27 22:16:30 -0700236}
237
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800238ReadablePropertyConstIterator<int16> PropertyStore::GetInt16PropertiesIter()
mukesh agrawalde29fa82011-09-16 16:16:36 -0700239 const {
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800240 return ReadablePropertyConstIterator<int16>(int16_properties_);
Chris Masonea8a2c252011-06-27 22:16:30 -0700241}
242
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800243ReadablePropertyConstIterator<int32> PropertyStore::GetInt32PropertiesIter()
mukesh agrawalde29fa82011-09-16 16:16:36 -0700244 const {
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800245 return ReadablePropertyConstIterator<int32>(int32_properties_);
Chris Masone889666b2011-07-03 12:58:50 -0700246}
247
Darin Petkov63138a92012-02-06 14:09:15 +0100248ReadablePropertyConstIterator<KeyValueStore>
249PropertyStore::GetKeyValueStorePropertiesIter() const {
250 return
251 ReadablePropertyConstIterator<KeyValueStore>(key_value_store_properties_);
252}
253
Jason Glasgowacdc11f2012-03-30 14:12:22 -0400254ReadablePropertyConstIterator<RpcIdentifier>
255PropertyStore::GetRpcIdentifierPropertiesIter() const {
256 return ReadablePropertyConstIterator<RpcIdentifier>(
257 rpc_identifier_properties_);
258}
259
mukesh agrawal2366eed2012-03-20 18:21:50 -0700260ReadablePropertyConstIterator<RpcIdentifiers>
261PropertyStore::GetRpcIdentifiersPropertiesIter() const {
262 return ReadablePropertyConstIterator<RpcIdentifiers>(
263 rpc_identifiers_properties_);
264}
265
mukesh agrawal66b0aca2012-01-30 15:28:28 -0800266ReadablePropertyConstIterator<string>
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800267PropertyStore::GetStringPropertiesIter() const {
mukesh agrawal66b0aca2012-01-30 15:28:28 -0800268 return ReadablePropertyConstIterator<string>(string_properties_);
Chris Masonea8a2c252011-06-27 22:16:30 -0700269}
270
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800271ReadablePropertyConstIterator<Stringmap>
272PropertyStore::GetStringmapPropertiesIter() const {
273 return ReadablePropertyConstIterator<Stringmap>(stringmap_properties_);
Chris Masonea8a2c252011-06-27 22:16:30 -0700274}
275
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800276ReadablePropertyConstIterator<Stringmaps>
277PropertyStore::GetStringmapsPropertiesIter()
278 const {
279 return ReadablePropertyConstIterator<Stringmaps>(stringmaps_properties_);
280}
281
282ReadablePropertyConstIterator<Strings> PropertyStore::GetStringsPropertiesIter()
283 const {
284 return ReadablePropertyConstIterator<Strings>(strings_properties_);
285}
286
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800287ReadablePropertyConstIterator<uint8> PropertyStore::GetUint8PropertiesIter()
288 const {
289 return ReadablePropertyConstIterator<uint8>(uint8_properties_);
290}
291
292ReadablePropertyConstIterator<uint16> PropertyStore::GetUint16PropertiesIter()
293 const {
294 return ReadablePropertyConstIterator<uint16>(uint16_properties_);
295}
296
297ReadablePropertyConstIterator<uint32> PropertyStore::GetUint32PropertiesIter()
298 const {
299 return ReadablePropertyConstIterator<uint32>(uint32_properties_);
Chris Masonea8a2c252011-06-27 22:16:30 -0700300}
301
Paul Stewarte18c33b2012-07-10 20:48:44 -0700302ReadablePropertyConstIterator<uint64> PropertyStore::GetUint64PropertiesIter()
303 const {
304 return ReadablePropertyConstIterator<uint64>(uint64_properties_);
305}
306
Chris Masoneb925cc82011-06-22 15:39:57 -0700307void PropertyStore::RegisterBool(const string &name, bool *prop) {
mukesh agrawal4d260da2012-01-30 11:53:52 -0800308 DCHECK(!Contains(name) || ContainsKey(bool_properties_, name))
309 << "(Already registered " << name << ")";
Chris Masoneb925cc82011-06-22 15:39:57 -0700310 bool_properties_[name] = BoolAccessor(new PropertyAccessor<bool>(prop));
311}
312
313void PropertyStore::RegisterConstBool(const string &name, const bool *prop) {
mukesh agrawal4d260da2012-01-30 11:53:52 -0800314 DCHECK(!Contains(name) || ContainsKey(bool_properties_, name))
315 << "(Already registered " << name << ")";
Chris Masoneb925cc82011-06-22 15:39:57 -0700316 bool_properties_[name] = BoolAccessor(new ConstPropertyAccessor<bool>(prop));
317}
318
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800319void PropertyStore::RegisterWriteOnlyBool(const string &name, bool *prop) {
mukesh agrawal4d260da2012-01-30 11:53:52 -0800320 DCHECK(!Contains(name) || ContainsKey(bool_properties_, name))
321 << "(Already registered " << name << ")";
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800322 bool_properties_[name] = BoolAccessor(
323 new WriteOnlyPropertyAccessor<bool>(prop));
324}
325
Chris Masoneb925cc82011-06-22 15:39:57 -0700326void PropertyStore::RegisterInt16(const string &name, int16 *prop) {
mukesh agrawal4d260da2012-01-30 11:53:52 -0800327 DCHECK(!Contains(name) || ContainsKey(int16_properties_, name))
328 << "(Already registered " << name << ")";
Chris Masoneb925cc82011-06-22 15:39:57 -0700329 int16_properties_[name] = Int16Accessor(new PropertyAccessor<int16>(prop));
330}
331
332void PropertyStore::RegisterConstInt16(const string &name, const int16 *prop) {
mukesh agrawal4d260da2012-01-30 11:53:52 -0800333 DCHECK(!Contains(name) || ContainsKey(int16_properties_, name))
334 << "(Already registered " << name << ")";
Chris Masoneb925cc82011-06-22 15:39:57 -0700335 int16_properties_[name] =
336 Int16Accessor(new ConstPropertyAccessor<int16>(prop));
337}
338
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800339void PropertyStore::RegisterWriteOnlyInt16(const string &name, int16 *prop) {
mukesh agrawal4d260da2012-01-30 11:53:52 -0800340 DCHECK(!Contains(name) || ContainsKey(int16_properties_, name))
341 << "(Already registered " << name << ")";
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800342 int16_properties_[name] =
343 Int16Accessor(new WriteOnlyPropertyAccessor<int16>(prop));
344}
Chris Masoneb925cc82011-06-22 15:39:57 -0700345void PropertyStore::RegisterInt32(const string &name, int32 *prop) {
mukesh agrawal4d260da2012-01-30 11:53:52 -0800346 DCHECK(!Contains(name) || ContainsKey(int32_properties_, name))
347 << "(Already registered " << name << ")";
Chris Masoneb925cc82011-06-22 15:39:57 -0700348 int32_properties_[name] = Int32Accessor(new PropertyAccessor<int32>(prop));
349}
350
351void PropertyStore::RegisterConstInt32(const string &name, const int32 *prop) {
mukesh agrawal4d260da2012-01-30 11:53:52 -0800352 DCHECK(!Contains(name) || ContainsKey(int32_properties_, name))
353 << "(Already registered " << name << ")";
Chris Masoneb925cc82011-06-22 15:39:57 -0700354 int32_properties_[name] =
355 Int32Accessor(new ConstPropertyAccessor<int32>(prop));
356}
357
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800358void PropertyStore::RegisterWriteOnlyInt32(const string &name, int32 *prop) {
mukesh agrawal4d260da2012-01-30 11:53:52 -0800359 DCHECK(!Contains(name) || ContainsKey(int32_properties_, name))
360 << "(Already registered " << name << ")";
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800361 int32_properties_[name] =
362 Int32Accessor(new WriteOnlyPropertyAccessor<int32>(prop));
363}
364
Chris Masoneb925cc82011-06-22 15:39:57 -0700365void PropertyStore::RegisterString(const string &name, string *prop) {
mukesh agrawal4d260da2012-01-30 11:53:52 -0800366 DCHECK(!Contains(name) || ContainsKey(string_properties_, name))
367 << "(Already registered " << name << ")";
Chris Masoneb925cc82011-06-22 15:39:57 -0700368 string_properties_[name] = StringAccessor(new PropertyAccessor<string>(prop));
369}
370
371void PropertyStore::RegisterConstString(const string &name,
372 const string *prop) {
mukesh agrawal4d260da2012-01-30 11:53:52 -0800373 DCHECK(!Contains(name) || ContainsKey(string_properties_, name))
374 << "(Already registered " << name << ")";
Chris Masoneb925cc82011-06-22 15:39:57 -0700375 string_properties_[name] =
376 StringAccessor(new ConstPropertyAccessor<string>(prop));
377}
378
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800379void PropertyStore::RegisterWriteOnlyString(const string &name, string *prop) {
mukesh agrawal4d260da2012-01-30 11:53:52 -0800380 DCHECK(!Contains(name) || ContainsKey(string_properties_, name))
381 << "(Already registered " << name << ")";
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800382 string_properties_[name] =
383 StringAccessor(new WriteOnlyPropertyAccessor<string>(prop));
384}
385
Chris Masone43b48a12011-07-01 13:37:07 -0700386void PropertyStore::RegisterStringmap(const string &name, Stringmap *prop) {
mukesh agrawal4d260da2012-01-30 11:53:52 -0800387 DCHECK(!Contains(name) || ContainsKey(stringmap_properties_, name))
388 << "(Already registered " << name << ")";
Chris Masoneb925cc82011-06-22 15:39:57 -0700389 stringmap_properties_[name] =
Chris Masone43b48a12011-07-01 13:37:07 -0700390 StringmapAccessor(new PropertyAccessor<Stringmap>(prop));
Chris Masoneb925cc82011-06-22 15:39:57 -0700391}
392
393void PropertyStore::RegisterConstStringmap(const string &name,
Chris Masone43b48a12011-07-01 13:37:07 -0700394 const Stringmap *prop) {
mukesh agrawal4d260da2012-01-30 11:53:52 -0800395 DCHECK(!Contains(name) || ContainsKey(stringmap_properties_, name))
396 << "(Already registered " << name << ")";
Chris Masoneb925cc82011-06-22 15:39:57 -0700397 stringmap_properties_[name] =
Chris Masone43b48a12011-07-01 13:37:07 -0700398 StringmapAccessor(new ConstPropertyAccessor<Stringmap>(prop));
399}
400
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800401void PropertyStore::RegisterWriteOnlyStringmap(const string &name,
402 Stringmap *prop) {
mukesh agrawal4d260da2012-01-30 11:53:52 -0800403 DCHECK(!Contains(name) || ContainsKey(stringmap_properties_, name))
404 << "(Already registered " << name << ")";
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800405 stringmap_properties_[name] =
406 StringmapAccessor(new WriteOnlyPropertyAccessor<Stringmap>(prop));
407}
408
Darin Petkovc0865312011-09-16 15:31:20 -0700409void PropertyStore::RegisterStringmaps(const string &name, Stringmaps *prop) {
mukesh agrawal4d260da2012-01-30 11:53:52 -0800410 DCHECK(!Contains(name) || ContainsKey(stringmaps_properties_, name))
411 << "(Already registered " << name << ")";
Darin Petkovc0865312011-09-16 15:31:20 -0700412 stringmaps_properties_[name] =
413 StringmapsAccessor(new PropertyAccessor<Stringmaps>(prop));
414}
415
416void PropertyStore::RegisterConstStringmaps(const string &name,
417 const Stringmaps *prop) {
mukesh agrawal4d260da2012-01-30 11:53:52 -0800418 DCHECK(!Contains(name) || ContainsKey(stringmaps_properties_, name))
419 << "(Already registered " << name << ")";
Darin Petkovc0865312011-09-16 15:31:20 -0700420 stringmaps_properties_[name] =
421 StringmapsAccessor(new ConstPropertyAccessor<Stringmaps>(prop));
422}
423
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800424void PropertyStore::RegisterWriteOnlyStringmaps(const string &name,
425 Stringmaps *prop) {
mukesh agrawal4d260da2012-01-30 11:53:52 -0800426 DCHECK(!Contains(name) || ContainsKey(stringmaps_properties_, name))
427 << "(Already registered " << name << ")";
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800428 stringmaps_properties_[name] =
429 StringmapsAccessor(new WriteOnlyPropertyAccessor<Stringmaps>(prop));
430}
431
Chris Masone43b48a12011-07-01 13:37:07 -0700432void PropertyStore::RegisterStrings(const string &name, Strings *prop) {
mukesh agrawal4d260da2012-01-30 11:53:52 -0800433 DCHECK(!Contains(name) || ContainsKey(strings_properties_, name))
434 << "(Already registered " << name << ")";
Chris Masone43b48a12011-07-01 13:37:07 -0700435 strings_properties_[name] =
436 StringsAccessor(new PropertyAccessor<Strings>(prop));
Chris Masoneb925cc82011-06-22 15:39:57 -0700437}
438
Chris Masone889666b2011-07-03 12:58:50 -0700439void PropertyStore::RegisterConstStrings(const string &name,
440 const Strings *prop) {
mukesh agrawal4d260da2012-01-30 11:53:52 -0800441 DCHECK(!Contains(name) || ContainsKey(strings_properties_, name))
442 << "(Already registered " << name << ")";
Chris Masone889666b2011-07-03 12:58:50 -0700443 strings_properties_[name] =
444 StringsAccessor(new ConstPropertyAccessor<Strings>(prop));
445}
446
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800447void PropertyStore::RegisterWriteOnlyStrings(const string &name,
448 Strings *prop) {
mukesh agrawal4d260da2012-01-30 11:53:52 -0800449 DCHECK(!Contains(name) || ContainsKey(strings_properties_, name))
450 << "(Already registered " << name << ")";
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800451 strings_properties_[name] =
452 StringsAccessor(new WriteOnlyPropertyAccessor<Strings>(prop));
453}
454
Chris Masone889666b2011-07-03 12:58:50 -0700455void PropertyStore::RegisterUint8(const string &name, uint8 *prop) {
mukesh agrawal4d260da2012-01-30 11:53:52 -0800456 DCHECK(!Contains(name) || ContainsKey(uint8_properties_, name))
457 << "(Already registered " << name << ")";
Chris Masone889666b2011-07-03 12:58:50 -0700458 uint8_properties_[name] = Uint8Accessor(new PropertyAccessor<uint8>(prop));
459}
460
Chris Masoneb925cc82011-06-22 15:39:57 -0700461void PropertyStore::RegisterConstUint8(const string &name, const uint8 *prop) {
mukesh agrawal4d260da2012-01-30 11:53:52 -0800462 DCHECK(!Contains(name) || ContainsKey(uint8_properties_, name))
463 << "(Already registered " << name << ")";
Chris Masoneb925cc82011-06-22 15:39:57 -0700464 uint8_properties_[name] =
465 Uint8Accessor(new ConstPropertyAccessor<uint8>(prop));
466}
467
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800468void PropertyStore::RegisterWriteOnlyUint8(const string &name, uint8 *prop) {
mukesh agrawal4d260da2012-01-30 11:53:52 -0800469 DCHECK(!Contains(name) || ContainsKey(uint8_properties_, name))
470 << "(Already registered " << name << ")";
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800471 uint8_properties_[name] =
472 Uint8Accessor(new WriteOnlyPropertyAccessor<uint8>(prop));
473}
474
mukesh agrawal66b0aca2012-01-30 15:28:28 -0800475void PropertyStore::RegisterUint16(const string &name, uint16 *prop) {
mukesh agrawal4d260da2012-01-30 11:53:52 -0800476 DCHECK(!Contains(name) || ContainsKey(uint16_properties_, name))
477 << "(Already registered " << name << ")";
Chris Masoneb925cc82011-06-22 15:39:57 -0700478 uint16_properties_[name] = Uint16Accessor(new PropertyAccessor<uint16>(prop));
479}
480
mukesh agrawal4d260da2012-01-30 11:53:52 -0800481void PropertyStore::RegisterUint32(const std::string &name, uint32 *prop) {
482 DCHECK(!Contains(name) || ContainsKey(uint32_properties_, name))
483 << "(Already registered " << name << ")";
484 uint32_properties_[name] = Uint32Accessor(new PropertyAccessor<uint32>(prop));
485}
486
Chris Masoneb925cc82011-06-22 15:39:57 -0700487void PropertyStore::RegisterConstUint16(const string &name,
488 const uint16 *prop) {
mukesh agrawal4d260da2012-01-30 11:53:52 -0800489 DCHECK(!Contains(name) || ContainsKey(uint16_properties_, name))
490 << "(Already registered " << name << ")";
Chris Masoneb925cc82011-06-22 15:39:57 -0700491 uint16_properties_[name] =
492 Uint16Accessor(new ConstPropertyAccessor<uint16>(prop));
493}
494
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800495void PropertyStore::RegisterWriteOnlyUint16(const string &name, uint16 *prop) {
mukesh agrawal4d260da2012-01-30 11:53:52 -0800496 DCHECK(!Contains(name) || ContainsKey(uint16_properties_, name))
497 << "(Already registered " << name << ")";
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800498 uint16_properties_[name] =
499 Uint16Accessor(new WriteOnlyPropertyAccessor<uint16>(prop));
500}
501
mukesh agrawal66b0aca2012-01-30 15:28:28 -0800502void PropertyStore::RegisterDerivedBool(const string &name,
Chris Masone27c4aa52011-07-02 13:10:14 -0700503 const BoolAccessor &accessor) {
mukesh agrawal4d260da2012-01-30 11:53:52 -0800504 DCHECK(!Contains(name) || ContainsKey(bool_properties_, name))
505 << "(Already registered " << name << ")";
Chris Masone27c4aa52011-07-02 13:10:14 -0700506 bool_properties_[name] = accessor;
507}
508
mukesh agrawal66b0aca2012-01-30 15:28:28 -0800509void PropertyStore::RegisterDerivedInt32(const string &name,
mukesh agrawal4d0401c2012-01-06 16:05:31 -0800510 const Int32Accessor &accessor) {
mukesh agrawal4d260da2012-01-30 11:53:52 -0800511 DCHECK(!Contains(name) || ContainsKey(int32_properties_, name))
512 << "(Already registered " << name << ")";
mukesh agrawal4d0401c2012-01-06 16:05:31 -0800513 int32_properties_[name] = accessor;
514}
515
Darin Petkov63138a92012-02-06 14:09:15 +0100516void PropertyStore::RegisterDerivedKeyValueStore(
mukesh agrawal66b0aca2012-01-30 15:28:28 -0800517 const string &name,
Darin Petkov63138a92012-02-06 14:09:15 +0100518 const KeyValueStoreAccessor &acc) {
mukesh agrawal4d260da2012-01-30 11:53:52 -0800519 DCHECK(!Contains(name) || ContainsKey(key_value_store_properties_, name))
520 << "(Already registered " << name << ")";
Darin Petkov63138a92012-02-06 14:09:15 +0100521 key_value_store_properties_[name] = acc;
522}
523
Jason Glasgowacdc11f2012-03-30 14:12:22 -0400524void PropertyStore::RegisterDerivedRpcIdentifier(
525 const string &name,
526 const RpcIdentifierAccessor &acc) {
527 DCHECK(!Contains(name) || ContainsKey(rpc_identifier_properties_, name))
528 << "(Already registered " << name << ")";
529 rpc_identifier_properties_[name] = acc;
530}
531
mukesh agrawal2366eed2012-03-20 18:21:50 -0700532void PropertyStore::RegisterDerivedRpcIdentifiers(
533 const string &name,
534 const RpcIdentifiersAccessor &accessor) {
535 DCHECK(!Contains(name) || ContainsKey(rpc_identifiers_properties_, name))
536 << "(Already registered " << name << ")";
537 rpc_identifiers_properties_[name] = accessor;
538}
539
mukesh agrawal66b0aca2012-01-30 15:28:28 -0800540void PropertyStore::RegisterDerivedString(const string &name,
Chris Masone27c4aa52011-07-02 13:10:14 -0700541 const StringAccessor &accessor) {
mukesh agrawal4d260da2012-01-30 11:53:52 -0800542 DCHECK(!Contains(name) || ContainsKey(string_properties_, name))
543 << "(Already registered " << name << ")";
Chris Masone27c4aa52011-07-02 13:10:14 -0700544 string_properties_[name] = accessor;
545}
546
mukesh agrawal66b0aca2012-01-30 15:28:28 -0800547void PropertyStore::RegisterDerivedStrings(const string &name,
Chris Masone27c4aa52011-07-02 13:10:14 -0700548 const StringsAccessor &accessor) {
mukesh agrawal4d260da2012-01-30 11:53:52 -0800549 DCHECK(!Contains(name) || ContainsKey(strings_properties_, name))
550 << "(Already registered " << name << ")";
Chris Masone27c4aa52011-07-02 13:10:14 -0700551 strings_properties_[name] = accessor;
Chris Masoneb925cc82011-06-22 15:39:57 -0700552}
553
Eric Shienbrood30bc0ec2012-03-21 18:19:46 -0400554void PropertyStore::RegisterDerivedStringmap(const string &name,
555 const StringmapAccessor &acc) {
556 DCHECK(!Contains(name) || ContainsKey(stringmap_properties_, name))
557 << "(Already registered " << name << ")";
558 stringmap_properties_[name] = acc;
559}
560
mukesh agrawal66b0aca2012-01-30 15:28:28 -0800561void PropertyStore::RegisterDerivedStringmaps(const string &name,
Chris Masone889666b2011-07-03 12:58:50 -0700562 const StringmapsAccessor &acc) {
mukesh agrawal4d260da2012-01-30 11:53:52 -0800563 DCHECK(!Contains(name) || ContainsKey(stringmaps_properties_, name))
564 << "(Already registered " << name << ")";
Chris Masone889666b2011-07-03 12:58:50 -0700565 stringmaps_properties_[name] = acc;
566}
567
mukesh agrawal66b0aca2012-01-30 15:28:28 -0800568void PropertyStore::RegisterDerivedUint16(const string &name,
Paul Stewartbe5f5b32011-12-07 17:11:11 -0800569 const Uint16Accessor &acc) {
mukesh agrawal4d260da2012-01-30 11:53:52 -0800570 DCHECK(!Contains(name) || ContainsKey(uint16_properties_, name))
571 << "(Already registered " << name << ")";
Paul Stewartbe5f5b32011-12-07 17:11:11 -0800572 uint16_properties_[name] = acc;
573}
574
Paul Stewarte18c33b2012-07-10 20:48:44 -0700575void PropertyStore::RegisterDerivedUint64(const string &name,
576 const Uint64Accessor &acc) {
577 DCHECK(!Contains(name) || ContainsKey(uint64_properties_, name))
578 << "(Already registered " << name << ")";
579 uint64_properties_[name] = acc;
580}
581
mukesh agrawal66b0aca2012-01-30 15:28:28 -0800582// private methods
583
mukesh agrawalffa3d042011-10-06 15:26:10 -0700584template <class V>
Paul Stewarte6e8e492013-01-17 11:00:50 -0800585bool PropertyStore::GetProperty(
586 const string &name,
587 V *value,
588 Error *error,
589 const map< string, std::tr1::shared_ptr<
590 AccessorInterface<V> > >&collection,
591 const string &value_type_english) const {
592 SLOG(Property, 2) << "Getting " << name << " as " << value_type_english
593 << ".";
594 typename map< string, std::tr1::shared_ptr<
595 AccessorInterface<V> > >::const_iterator it = collection.find(name);
596 if (it != collection.end()) {
597 V val = it->second->Get(error);
598 if (error->IsSuccess()) {
599 *value = val;
600 }
601 } else {
602 if (Contains(name)) {
603 error->Populate(
604 Error::kInvalidArguments,
605 "Property " + name + " is not " + value_type_english + ".");
606 } else {
607 error->Populate(
608 Error::kInvalidProperty, "Property " + name + " does not exist.");
609 }
610 }
611 return error->IsSuccess();
612};
613
614template <class V>
mukesh agrawalffa3d042011-10-06 15:26:10 -0700615bool PropertyStore::SetProperty(
616 const string &name,
617 const V &value,
618 Error *error,
619 map< string, std::tr1::shared_ptr< AccessorInterface<V> > >&collection,
620 const string &value_type_english) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700621 SLOG(Property, 2) << "Setting " << name << " as " << value_type_english
622 << ".";
mukesh agrawalffa3d042011-10-06 15:26:10 -0700623 if (ContainsKey(collection, name)) {
624 collection[name]->Set(value, error);
625 } else {
626 if (Contains(name)) {
627 error->Populate(
628 Error::kInvalidArguments,
629 "Property " + name + " is not " + value_type_english + ".");
630 } else {
631 error->Populate(
632 Error::kInvalidProperty, "Property " + name + " does not exist.");
633 }
634 }
635 return error->IsSuccess();
636};
637
Chris Masoneb925cc82011-06-22 15:39:57 -0700638} // namespace shill