blob: df6359d53265a8623ac224ce9728d301dd92308e [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
mukesh agrawalbebf1b82013-04-23 15:06:33 -070026PropertyStore::PropertyStore(PropertyChangeCallback on_property_changed) :
27 property_changed_callback_(on_property_changed) {}
28
Chris Masoneb925cc82011-06-22 15:39:57 -070029PropertyStore::~PropertyStore() {}
30
mukesh agrawal66b0aca2012-01-30 15:28:28 -080031bool PropertyStore::Contains(const string &prop) const {
32 return (ContainsKey(bool_properties_, prop) ||
33 ContainsKey(int16_properties_, prop) ||
34 ContainsKey(int32_properties_, prop) ||
35 ContainsKey(key_value_store_properties_, prop) ||
36 ContainsKey(string_properties_, prop) ||
37 ContainsKey(stringmap_properties_, prop) ||
38 ContainsKey(stringmaps_properties_, prop) ||
39 ContainsKey(strings_properties_, prop) ||
40 ContainsKey(uint8_properties_, prop) ||
41 ContainsKey(uint16_properties_, prop) ||
Jason Glasgowacdc11f2012-03-30 14:12:22 -040042 ContainsKey(uint32_properties_, prop) ||
Paul Stewarte18c33b2012-07-10 20:48:44 -070043 ContainsKey(uint64_properties_, prop) ||
Jason Glasgowacdc11f2012-03-30 14:12:22 -040044 ContainsKey(rpc_identifier_properties_, prop) ||
45 ContainsKey(rpc_identifiers_properties_, prop));
Chris Masoneb925cc82011-06-22 15:39:57 -070046}
47
Paul Stewarte6e8e492013-01-17 11:00:50 -080048bool PropertyStore::GetBoolProperty(const string &name,
49 bool *value,
50 Error *error) const {
51 return GetProperty(name, value, error, bool_properties_, "a bool");
52}
53
54bool PropertyStore::GetInt16Property(const string &name,
55 int16 *value,
56 Error *error) const {
57 return GetProperty(name, value, error, int16_properties_, "an int16");
58}
59
60bool PropertyStore::GetInt32Property(const string &name,
61 int32 *value,
62 Error *error) const {
63 return GetProperty(name, value, error, int32_properties_, "an int32");
64}
65
66bool PropertyStore::GetKeyValueStoreProperty(const string &name,
67 KeyValueStore *value,
68 Error *error) const {
69 return GetProperty(name, value, error, key_value_store_properties_,
70 "a key value store");
71}
72
73bool PropertyStore::GetRpcIdentifierProperty(const string &name,
74 RpcIdentifier *value,
75 Error *error) const {
76 return GetProperty(name, value, error, rpc_identifier_properties_,
77 "an rpc_identifier");
78}
79
80bool PropertyStore::GetStringProperty(const string &name,
81 string *value,
82 Error *error) const {
83 return GetProperty(name, value, error, string_properties_, "a string");
84}
85
86bool PropertyStore::GetStringmapProperty(const string &name,
87 Stringmap *values,
88 Error *error) const {
89 return GetProperty(name, values, error, stringmap_properties_,
90 "a string map");
91}
92
93bool PropertyStore::GetStringmapsProperty(const string &name,
94 Stringmaps *values,
95 Error *error) const {
96 return GetProperty(name, values, error, stringmaps_properties_,
97 "a string map list");
98}
99
100bool PropertyStore::GetStringsProperty(const string &name,
101 Strings *values,
102 Error *error) const {
103 return GetProperty(name, values, error, strings_properties_, "a string list");
104}
105
106bool PropertyStore::GetUint8Property(const string &name,
107 uint8 *value,
108 Error *error) const {
109 return GetProperty(name, value, error, uint8_properties_, "a uint8");
110}
111
112bool PropertyStore::GetUint16Property(const string &name,
113 uint16 *value,
114 Error *error) const {
115 return GetProperty(name, value, error, uint16_properties_, "a uint16");
116}
117
118bool PropertyStore::GetUint32Property(const string &name,
119 uint32 *value,
120 Error *error) const {
121 return GetProperty(name, value, error, uint32_properties_, "a uint32");
122}
123
124bool PropertyStore::GetUint64Property(const string &name,
125 uint64 *value,
126 Error *error) const {
127 return GetProperty(name, value, error, uint64_properties_, "a uint64");
128}
129
mukesh agrawal66b0aca2012-01-30 15:28:28 -0800130bool PropertyStore::SetBoolProperty(const string &name,
Chris Masoneb925cc82011-06-22 15:39:57 -0700131 bool value,
132 Error *error) {
mukesh agrawalffa3d042011-10-06 15:26:10 -0700133 return SetProperty(name, value, error, bool_properties_, "a bool");
Chris Masoneb925cc82011-06-22 15:39:57 -0700134}
135
mukesh agrawal66b0aca2012-01-30 15:28:28 -0800136bool PropertyStore::SetInt16Property(const string &name,
Chris Masoneb925cc82011-06-22 15:39:57 -0700137 int16 value,
138 Error *error) {
mukesh agrawalffa3d042011-10-06 15:26:10 -0700139 return SetProperty(name, value, error, int16_properties_, "an int16");
Chris Masoneb925cc82011-06-22 15:39:57 -0700140}
141
mukesh agrawal66b0aca2012-01-30 15:28:28 -0800142bool PropertyStore::SetInt32Property(const string &name,
Chris Masoneb925cc82011-06-22 15:39:57 -0700143 int32 value,
144 Error *error) {
mukesh agrawalffa3d042011-10-06 15:26:10 -0700145 return SetProperty(name, value, error, int32_properties_, "an int32.");
Chris Masoneb925cc82011-06-22 15:39:57 -0700146}
147
mukesh agrawal66b0aca2012-01-30 15:28:28 -0800148bool PropertyStore::SetStringProperty(const string &name,
149 const string &value,
Chris Masoneb925cc82011-06-22 15:39:57 -0700150 Error *error) {
mukesh agrawalffa3d042011-10-06 15:26:10 -0700151 return SetProperty(name, value, error, string_properties_, "a string");
Chris Masoneb925cc82011-06-22 15:39:57 -0700152}
153
mukesh agrawal66b0aca2012-01-30 15:28:28 -0800154bool PropertyStore::SetStringmapProperty(const string &name,
155 const map<string, string> &values,
156 Error *error) {
mukesh agrawalffa3d042011-10-06 15:26:10 -0700157 return SetProperty(name, values, error, stringmap_properties_,
158 "a string map");
Chris Masoneb925cc82011-06-22 15:39:57 -0700159}
160
mukesh agrawalbebf1b82013-04-23 15:06:33 -0700161bool PropertyStore::SetStringmapsProperty(
162 const string &name,
163 const vector<map<string, string> > &values,
164 Error *error) {
165 return SetProperty(name, values, error, stringmaps_properties_,
166 "a stringmaps");
167}
168
mukesh agrawal66b0aca2012-01-30 15:28:28 -0800169bool PropertyStore::SetStringsProperty(const string &name,
170 const vector<string> &values,
Chris Masoneb925cc82011-06-22 15:39:57 -0700171 Error *error) {
mukesh agrawalffa3d042011-10-06 15:26:10 -0700172 return SetProperty(name, values, error, strings_properties_, "a string list");
Chris Masoneb925cc82011-06-22 15:39:57 -0700173}
174
mukesh agrawal66b0aca2012-01-30 15:28:28 -0800175bool PropertyStore::SetUint8Property(const string &name,
Chris Masoneb925cc82011-06-22 15:39:57 -0700176 uint8 value,
177 Error *error) {
mukesh agrawalffa3d042011-10-06 15:26:10 -0700178 return SetProperty(name, value, error, uint8_properties_, "a uint8");
Chris Masoneb925cc82011-06-22 15:39:57 -0700179}
180
mukesh agrawal66b0aca2012-01-30 15:28:28 -0800181bool PropertyStore::SetUint16Property(const string &name,
Chris Masoneb925cc82011-06-22 15:39:57 -0700182 uint16 value,
183 Error *error) {
mukesh agrawalffa3d042011-10-06 15:26:10 -0700184 return SetProperty(name, value, error, uint16_properties_, "a uint16");
Chris Masoneb925cc82011-06-22 15:39:57 -0700185}
186
mukesh agrawal66b0aca2012-01-30 15:28:28 -0800187bool PropertyStore::SetUint32Property(const string &name,
Chris Masoneb925cc82011-06-22 15:39:57 -0700188 uint32 value,
189 Error *error) {
mukesh agrawalffa3d042011-10-06 15:26:10 -0700190 return SetProperty(name, value, error, uint32_properties_, "a uint32");
Chris Masoneb925cc82011-06-22 15:39:57 -0700191}
192
Paul Stewarte18c33b2012-07-10 20:48:44 -0700193bool PropertyStore::SetUint64Property(const string &name,
194 uint64 value,
195 Error *error) {
196 return SetProperty(name, value, error, uint64_properties_, "a uint64");
197}
198
Jason Glasgowacdc11f2012-03-30 14:12:22 -0400199bool PropertyStore::SetRpcIdentifierProperty(const string &name,
200 const RpcIdentifier &value,
201 Error *error) {
202 return SetProperty(name, value, error, rpc_identifier_properties_,
203 "an rpc_identifier");
204}
205
mukesh agrawal4d260da2012-01-30 11:53:52 -0800206bool PropertyStore::ClearProperty(const string &name, Error *error) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700207 SLOG(Property, 2) << "Clearing " << name << ".";
mukesh agrawal4d260da2012-01-30 11:53:52 -0800208
209 if (ContainsKey(bool_properties_, name)) {
210 bool_properties_[name]->Clear(error);
211 } else if (ContainsKey(int16_properties_, name)) {
212 int16_properties_[name]->Clear(error);
213 } else if (ContainsKey(int32_properties_, name)) {
214 int32_properties_[name]->Clear(error);
215 } else if (ContainsKey(key_value_store_properties_, name)) {
216 key_value_store_properties_[name]->Clear(error);
217 } else if (ContainsKey(string_properties_, name)) {
218 string_properties_[name]->Clear(error);
219 } else if (ContainsKey(stringmap_properties_, name)) {
220 stringmap_properties_[name]->Clear(error);
221 } else if (ContainsKey(stringmaps_properties_, name)) {
222 stringmaps_properties_[name]->Clear(error);
223 } else if (ContainsKey(strings_properties_, name)) {
224 strings_properties_[name]->Clear(error);
225 } else if (ContainsKey(uint8_properties_, name)) {
226 uint8_properties_[name]->Clear(error);
227 } else if (ContainsKey(uint16_properties_, name)) {
228 uint16_properties_[name]->Clear(error);
229 } else if (ContainsKey(uint32_properties_, name)) {
230 uint32_properties_[name]->Clear(error);
Paul Stewarte18c33b2012-07-10 20:48:44 -0700231 } else if (ContainsKey(uint64_properties_, name)) {
232 uint64_properties_[name]->Clear(error);
Jason Glasgowacdc11f2012-03-30 14:12:22 -0400233 } else if (ContainsKey(rpc_identifier_properties_, name)) {
234 rpc_identifier_properties_[name]->Clear(error);
235 } else if (ContainsKey(rpc_identifiers_properties_, name)) {
236 rpc_identifiers_properties_[name]->Clear(error);
mukesh agrawal4d260da2012-01-30 11:53:52 -0800237 } else {
238 error->Populate(
239 Error::kInvalidProperty, "Property " + name + " does not exist.");
240 }
mukesh agrawalbebf1b82013-04-23 15:06:33 -0700241 if (error->IsSuccess()) {
242 if (!property_changed_callback_.is_null()) {
243 property_changed_callback_.Run(name);
244 }
245 }
mukesh agrawal4d260da2012-01-30 11:53:52 -0800246 return error->IsSuccess();
247}
248
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800249ReadablePropertyConstIterator<bool> PropertyStore::GetBoolPropertiesIter()
mukesh agrawalde29fa82011-09-16 16:16:36 -0700250 const {
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800251 return ReadablePropertyConstIterator<bool>(bool_properties_);
Chris Masonea8a2c252011-06-27 22:16:30 -0700252}
253
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800254ReadablePropertyConstIterator<int16> PropertyStore::GetInt16PropertiesIter()
mukesh agrawalde29fa82011-09-16 16:16:36 -0700255 const {
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800256 return ReadablePropertyConstIterator<int16>(int16_properties_);
Chris Masonea8a2c252011-06-27 22:16:30 -0700257}
258
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800259ReadablePropertyConstIterator<int32> PropertyStore::GetInt32PropertiesIter()
mukesh agrawalde29fa82011-09-16 16:16:36 -0700260 const {
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800261 return ReadablePropertyConstIterator<int32>(int32_properties_);
Chris Masone889666b2011-07-03 12:58:50 -0700262}
263
Darin Petkov63138a92012-02-06 14:09:15 +0100264ReadablePropertyConstIterator<KeyValueStore>
265PropertyStore::GetKeyValueStorePropertiesIter() const {
266 return
267 ReadablePropertyConstIterator<KeyValueStore>(key_value_store_properties_);
268}
269
Jason Glasgowacdc11f2012-03-30 14:12:22 -0400270ReadablePropertyConstIterator<RpcIdentifier>
271PropertyStore::GetRpcIdentifierPropertiesIter() const {
272 return ReadablePropertyConstIterator<RpcIdentifier>(
273 rpc_identifier_properties_);
274}
275
mukesh agrawal2366eed2012-03-20 18:21:50 -0700276ReadablePropertyConstIterator<RpcIdentifiers>
277PropertyStore::GetRpcIdentifiersPropertiesIter() const {
278 return ReadablePropertyConstIterator<RpcIdentifiers>(
279 rpc_identifiers_properties_);
280}
281
mukesh agrawal66b0aca2012-01-30 15:28:28 -0800282ReadablePropertyConstIterator<string>
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800283PropertyStore::GetStringPropertiesIter() const {
mukesh agrawal66b0aca2012-01-30 15:28:28 -0800284 return ReadablePropertyConstIterator<string>(string_properties_);
Chris Masonea8a2c252011-06-27 22:16:30 -0700285}
286
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800287ReadablePropertyConstIterator<Stringmap>
288PropertyStore::GetStringmapPropertiesIter() const {
289 return ReadablePropertyConstIterator<Stringmap>(stringmap_properties_);
Chris Masonea8a2c252011-06-27 22:16:30 -0700290}
291
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800292ReadablePropertyConstIterator<Stringmaps>
293PropertyStore::GetStringmapsPropertiesIter()
294 const {
295 return ReadablePropertyConstIterator<Stringmaps>(stringmaps_properties_);
296}
297
298ReadablePropertyConstIterator<Strings> PropertyStore::GetStringsPropertiesIter()
299 const {
300 return ReadablePropertyConstIterator<Strings>(strings_properties_);
301}
302
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800303ReadablePropertyConstIterator<uint8> PropertyStore::GetUint8PropertiesIter()
304 const {
305 return ReadablePropertyConstIterator<uint8>(uint8_properties_);
306}
307
308ReadablePropertyConstIterator<uint16> PropertyStore::GetUint16PropertiesIter()
309 const {
310 return ReadablePropertyConstIterator<uint16>(uint16_properties_);
311}
312
313ReadablePropertyConstIterator<uint32> PropertyStore::GetUint32PropertiesIter()
314 const {
315 return ReadablePropertyConstIterator<uint32>(uint32_properties_);
Chris Masonea8a2c252011-06-27 22:16:30 -0700316}
317
Paul Stewarte18c33b2012-07-10 20:48:44 -0700318ReadablePropertyConstIterator<uint64> PropertyStore::GetUint64PropertiesIter()
319 const {
320 return ReadablePropertyConstIterator<uint64>(uint64_properties_);
321}
322
Chris Masoneb925cc82011-06-22 15:39:57 -0700323void PropertyStore::RegisterBool(const string &name, bool *prop) {
mukesh agrawal4d260da2012-01-30 11:53:52 -0800324 DCHECK(!Contains(name) || ContainsKey(bool_properties_, name))
325 << "(Already registered " << name << ")";
Chris Masoneb925cc82011-06-22 15:39:57 -0700326 bool_properties_[name] = BoolAccessor(new PropertyAccessor<bool>(prop));
327}
328
329void PropertyStore::RegisterConstBool(const string &name, const bool *prop) {
mukesh agrawal4d260da2012-01-30 11:53:52 -0800330 DCHECK(!Contains(name) || ContainsKey(bool_properties_, name))
331 << "(Already registered " << name << ")";
Chris Masoneb925cc82011-06-22 15:39:57 -0700332 bool_properties_[name] = BoolAccessor(new ConstPropertyAccessor<bool>(prop));
333}
334
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800335void PropertyStore::RegisterWriteOnlyBool(const string &name, bool *prop) {
mukesh agrawal4d260da2012-01-30 11:53:52 -0800336 DCHECK(!Contains(name) || ContainsKey(bool_properties_, name))
337 << "(Already registered " << name << ")";
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800338 bool_properties_[name] = BoolAccessor(
339 new WriteOnlyPropertyAccessor<bool>(prop));
340}
341
Chris Masoneb925cc82011-06-22 15:39:57 -0700342void PropertyStore::RegisterInt16(const string &name, int16 *prop) {
mukesh agrawal4d260da2012-01-30 11:53:52 -0800343 DCHECK(!Contains(name) || ContainsKey(int16_properties_, name))
344 << "(Already registered " << name << ")";
Chris Masoneb925cc82011-06-22 15:39:57 -0700345 int16_properties_[name] = Int16Accessor(new PropertyAccessor<int16>(prop));
346}
347
348void PropertyStore::RegisterConstInt16(const string &name, const int16 *prop) {
mukesh agrawal4d260da2012-01-30 11:53:52 -0800349 DCHECK(!Contains(name) || ContainsKey(int16_properties_, name))
350 << "(Already registered " << name << ")";
Chris Masoneb925cc82011-06-22 15:39:57 -0700351 int16_properties_[name] =
352 Int16Accessor(new ConstPropertyAccessor<int16>(prop));
353}
354
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800355void PropertyStore::RegisterWriteOnlyInt16(const string &name, int16 *prop) {
mukesh agrawal4d260da2012-01-30 11:53:52 -0800356 DCHECK(!Contains(name) || ContainsKey(int16_properties_, name))
357 << "(Already registered " << name << ")";
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800358 int16_properties_[name] =
359 Int16Accessor(new WriteOnlyPropertyAccessor<int16>(prop));
360}
Chris Masoneb925cc82011-06-22 15:39:57 -0700361void PropertyStore::RegisterInt32(const string &name, int32 *prop) {
mukesh agrawal4d260da2012-01-30 11:53:52 -0800362 DCHECK(!Contains(name) || ContainsKey(int32_properties_, name))
363 << "(Already registered " << name << ")";
Chris Masoneb925cc82011-06-22 15:39:57 -0700364 int32_properties_[name] = Int32Accessor(new PropertyAccessor<int32>(prop));
365}
366
367void PropertyStore::RegisterConstInt32(const string &name, const int32 *prop) {
mukesh agrawal4d260da2012-01-30 11:53:52 -0800368 DCHECK(!Contains(name) || ContainsKey(int32_properties_, name))
369 << "(Already registered " << name << ")";
Chris Masoneb925cc82011-06-22 15:39:57 -0700370 int32_properties_[name] =
371 Int32Accessor(new ConstPropertyAccessor<int32>(prop));
372}
373
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800374void PropertyStore::RegisterWriteOnlyInt32(const string &name, int32 *prop) {
mukesh agrawal4d260da2012-01-30 11:53:52 -0800375 DCHECK(!Contains(name) || ContainsKey(int32_properties_, name))
376 << "(Already registered " << name << ")";
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800377 int32_properties_[name] =
378 Int32Accessor(new WriteOnlyPropertyAccessor<int32>(prop));
379}
380
Chris Masoneb925cc82011-06-22 15:39:57 -0700381void PropertyStore::RegisterString(const string &name, string *prop) {
mukesh agrawal4d260da2012-01-30 11:53:52 -0800382 DCHECK(!Contains(name) || ContainsKey(string_properties_, name))
383 << "(Already registered " << name << ")";
Chris Masoneb925cc82011-06-22 15:39:57 -0700384 string_properties_[name] = StringAccessor(new PropertyAccessor<string>(prop));
385}
386
387void PropertyStore::RegisterConstString(const string &name,
388 const string *prop) {
mukesh agrawal4d260da2012-01-30 11:53:52 -0800389 DCHECK(!Contains(name) || ContainsKey(string_properties_, name))
390 << "(Already registered " << name << ")";
Chris Masoneb925cc82011-06-22 15:39:57 -0700391 string_properties_[name] =
392 StringAccessor(new ConstPropertyAccessor<string>(prop));
393}
394
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800395void PropertyStore::RegisterWriteOnlyString(const string &name, string *prop) {
mukesh agrawal4d260da2012-01-30 11:53:52 -0800396 DCHECK(!Contains(name) || ContainsKey(string_properties_, name))
397 << "(Already registered " << name << ")";
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800398 string_properties_[name] =
399 StringAccessor(new WriteOnlyPropertyAccessor<string>(prop));
400}
401
Chris Masone43b48a12011-07-01 13:37:07 -0700402void PropertyStore::RegisterStringmap(const string &name, Stringmap *prop) {
mukesh agrawal4d260da2012-01-30 11:53:52 -0800403 DCHECK(!Contains(name) || ContainsKey(stringmap_properties_, name))
404 << "(Already registered " << name << ")";
Chris Masoneb925cc82011-06-22 15:39:57 -0700405 stringmap_properties_[name] =
Chris Masone43b48a12011-07-01 13:37:07 -0700406 StringmapAccessor(new PropertyAccessor<Stringmap>(prop));
Chris Masoneb925cc82011-06-22 15:39:57 -0700407}
408
409void PropertyStore::RegisterConstStringmap(const string &name,
Chris Masone43b48a12011-07-01 13:37:07 -0700410 const Stringmap *prop) {
mukesh agrawal4d260da2012-01-30 11:53:52 -0800411 DCHECK(!Contains(name) || ContainsKey(stringmap_properties_, name))
412 << "(Already registered " << name << ")";
Chris Masoneb925cc82011-06-22 15:39:57 -0700413 stringmap_properties_[name] =
Chris Masone43b48a12011-07-01 13:37:07 -0700414 StringmapAccessor(new ConstPropertyAccessor<Stringmap>(prop));
415}
416
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800417void PropertyStore::RegisterWriteOnlyStringmap(const string &name,
418 Stringmap *prop) {
mukesh agrawal4d260da2012-01-30 11:53:52 -0800419 DCHECK(!Contains(name) || ContainsKey(stringmap_properties_, name))
420 << "(Already registered " << name << ")";
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800421 stringmap_properties_[name] =
422 StringmapAccessor(new WriteOnlyPropertyAccessor<Stringmap>(prop));
423}
424
Darin Petkovc0865312011-09-16 15:31:20 -0700425void PropertyStore::RegisterStringmaps(const string &name, Stringmaps *prop) {
mukesh agrawal4d260da2012-01-30 11:53:52 -0800426 DCHECK(!Contains(name) || ContainsKey(stringmaps_properties_, name))
427 << "(Already registered " << name << ")";
Darin Petkovc0865312011-09-16 15:31:20 -0700428 stringmaps_properties_[name] =
429 StringmapsAccessor(new PropertyAccessor<Stringmaps>(prop));
430}
431
432void PropertyStore::RegisterConstStringmaps(const string &name,
433 const Stringmaps *prop) {
mukesh agrawal4d260da2012-01-30 11:53:52 -0800434 DCHECK(!Contains(name) || ContainsKey(stringmaps_properties_, name))
435 << "(Already registered " << name << ")";
Darin Petkovc0865312011-09-16 15:31:20 -0700436 stringmaps_properties_[name] =
437 StringmapsAccessor(new ConstPropertyAccessor<Stringmaps>(prop));
438}
439
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800440void PropertyStore::RegisterWriteOnlyStringmaps(const string &name,
441 Stringmaps *prop) {
mukesh agrawal4d260da2012-01-30 11:53:52 -0800442 DCHECK(!Contains(name) || ContainsKey(stringmaps_properties_, name))
443 << "(Already registered " << name << ")";
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800444 stringmaps_properties_[name] =
445 StringmapsAccessor(new WriteOnlyPropertyAccessor<Stringmaps>(prop));
446}
447
Chris Masone43b48a12011-07-01 13:37:07 -0700448void PropertyStore::RegisterStrings(const string &name, Strings *prop) {
mukesh agrawal4d260da2012-01-30 11:53:52 -0800449 DCHECK(!Contains(name) || ContainsKey(strings_properties_, name))
450 << "(Already registered " << name << ")";
Chris Masone43b48a12011-07-01 13:37:07 -0700451 strings_properties_[name] =
452 StringsAccessor(new PropertyAccessor<Strings>(prop));
Chris Masoneb925cc82011-06-22 15:39:57 -0700453}
454
Chris Masone889666b2011-07-03 12:58:50 -0700455void PropertyStore::RegisterConstStrings(const string &name,
456 const Strings *prop) {
mukesh agrawal4d260da2012-01-30 11:53:52 -0800457 DCHECK(!Contains(name) || ContainsKey(strings_properties_, name))
458 << "(Already registered " << name << ")";
Chris Masone889666b2011-07-03 12:58:50 -0700459 strings_properties_[name] =
460 StringsAccessor(new ConstPropertyAccessor<Strings>(prop));
461}
462
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800463void PropertyStore::RegisterWriteOnlyStrings(const string &name,
464 Strings *prop) {
mukesh agrawal4d260da2012-01-30 11:53:52 -0800465 DCHECK(!Contains(name) || ContainsKey(strings_properties_, name))
466 << "(Already registered " << name << ")";
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800467 strings_properties_[name] =
468 StringsAccessor(new WriteOnlyPropertyAccessor<Strings>(prop));
469}
470
Chris Masone889666b2011-07-03 12:58:50 -0700471void PropertyStore::RegisterUint8(const string &name, uint8 *prop) {
mukesh agrawal4d260da2012-01-30 11:53:52 -0800472 DCHECK(!Contains(name) || ContainsKey(uint8_properties_, name))
473 << "(Already registered " << name << ")";
Chris Masone889666b2011-07-03 12:58:50 -0700474 uint8_properties_[name] = Uint8Accessor(new PropertyAccessor<uint8>(prop));
475}
476
Chris Masoneb925cc82011-06-22 15:39:57 -0700477void PropertyStore::RegisterConstUint8(const string &name, const uint8 *prop) {
mukesh agrawal4d260da2012-01-30 11:53:52 -0800478 DCHECK(!Contains(name) || ContainsKey(uint8_properties_, name))
479 << "(Already registered " << name << ")";
Chris Masoneb925cc82011-06-22 15:39:57 -0700480 uint8_properties_[name] =
481 Uint8Accessor(new ConstPropertyAccessor<uint8>(prop));
482}
483
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800484void PropertyStore::RegisterWriteOnlyUint8(const string &name, uint8 *prop) {
mukesh agrawal4d260da2012-01-30 11:53:52 -0800485 DCHECK(!Contains(name) || ContainsKey(uint8_properties_, name))
486 << "(Already registered " << name << ")";
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800487 uint8_properties_[name] =
488 Uint8Accessor(new WriteOnlyPropertyAccessor<uint8>(prop));
489}
490
mukesh agrawal66b0aca2012-01-30 15:28:28 -0800491void PropertyStore::RegisterUint16(const string &name, uint16 *prop) {
mukesh agrawal4d260da2012-01-30 11:53:52 -0800492 DCHECK(!Contains(name) || ContainsKey(uint16_properties_, name))
493 << "(Already registered " << name << ")";
Chris Masoneb925cc82011-06-22 15:39:57 -0700494 uint16_properties_[name] = Uint16Accessor(new PropertyAccessor<uint16>(prop));
495}
496
mukesh agrawal4d260da2012-01-30 11:53:52 -0800497void PropertyStore::RegisterUint32(const std::string &name, uint32 *prop) {
498 DCHECK(!Contains(name) || ContainsKey(uint32_properties_, name))
499 << "(Already registered " << name << ")";
500 uint32_properties_[name] = Uint32Accessor(new PropertyAccessor<uint32>(prop));
501}
502
Chris Masoneb925cc82011-06-22 15:39:57 -0700503void PropertyStore::RegisterConstUint16(const string &name,
504 const uint16 *prop) {
mukesh agrawal4d260da2012-01-30 11:53:52 -0800505 DCHECK(!Contains(name) || ContainsKey(uint16_properties_, name))
506 << "(Already registered " << name << ")";
Chris Masoneb925cc82011-06-22 15:39:57 -0700507 uint16_properties_[name] =
508 Uint16Accessor(new ConstPropertyAccessor<uint16>(prop));
509}
510
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800511void PropertyStore::RegisterWriteOnlyUint16(const string &name, uint16 *prop) {
mukesh agrawal4d260da2012-01-30 11:53:52 -0800512 DCHECK(!Contains(name) || ContainsKey(uint16_properties_, name))
513 << "(Already registered " << name << ")";
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800514 uint16_properties_[name] =
515 Uint16Accessor(new WriteOnlyPropertyAccessor<uint16>(prop));
516}
517
mukesh agrawal66b0aca2012-01-30 15:28:28 -0800518void PropertyStore::RegisterDerivedBool(const string &name,
Chris Masone27c4aa52011-07-02 13:10:14 -0700519 const BoolAccessor &accessor) {
mukesh agrawal4d260da2012-01-30 11:53:52 -0800520 DCHECK(!Contains(name) || ContainsKey(bool_properties_, name))
521 << "(Already registered " << name << ")";
Chris Masone27c4aa52011-07-02 13:10:14 -0700522 bool_properties_[name] = accessor;
523}
524
mukesh agrawal66b0aca2012-01-30 15:28:28 -0800525void PropertyStore::RegisterDerivedInt32(const string &name,
mukesh agrawal4d0401c2012-01-06 16:05:31 -0800526 const Int32Accessor &accessor) {
mukesh agrawal4d260da2012-01-30 11:53:52 -0800527 DCHECK(!Contains(name) || ContainsKey(int32_properties_, name))
528 << "(Already registered " << name << ")";
mukesh agrawal4d0401c2012-01-06 16:05:31 -0800529 int32_properties_[name] = accessor;
530}
531
Darin Petkov63138a92012-02-06 14:09:15 +0100532void PropertyStore::RegisterDerivedKeyValueStore(
mukesh agrawal66b0aca2012-01-30 15:28:28 -0800533 const string &name,
Darin Petkov63138a92012-02-06 14:09:15 +0100534 const KeyValueStoreAccessor &acc) {
mukesh agrawal4d260da2012-01-30 11:53:52 -0800535 DCHECK(!Contains(name) || ContainsKey(key_value_store_properties_, name))
536 << "(Already registered " << name << ")";
Darin Petkov63138a92012-02-06 14:09:15 +0100537 key_value_store_properties_[name] = acc;
538}
539
Jason Glasgowacdc11f2012-03-30 14:12:22 -0400540void PropertyStore::RegisterDerivedRpcIdentifier(
541 const string &name,
542 const RpcIdentifierAccessor &acc) {
543 DCHECK(!Contains(name) || ContainsKey(rpc_identifier_properties_, name))
544 << "(Already registered " << name << ")";
545 rpc_identifier_properties_[name] = acc;
546}
547
mukesh agrawal2366eed2012-03-20 18:21:50 -0700548void PropertyStore::RegisterDerivedRpcIdentifiers(
549 const string &name,
550 const RpcIdentifiersAccessor &accessor) {
551 DCHECK(!Contains(name) || ContainsKey(rpc_identifiers_properties_, name))
552 << "(Already registered " << name << ")";
553 rpc_identifiers_properties_[name] = accessor;
554}
555
mukesh agrawal66b0aca2012-01-30 15:28:28 -0800556void PropertyStore::RegisterDerivedString(const string &name,
Chris Masone27c4aa52011-07-02 13:10:14 -0700557 const StringAccessor &accessor) {
mukesh agrawal4d260da2012-01-30 11:53:52 -0800558 DCHECK(!Contains(name) || ContainsKey(string_properties_, name))
559 << "(Already registered " << name << ")";
Chris Masone27c4aa52011-07-02 13:10:14 -0700560 string_properties_[name] = accessor;
561}
562
mukesh agrawal66b0aca2012-01-30 15:28:28 -0800563void PropertyStore::RegisterDerivedStrings(const string &name,
Chris Masone27c4aa52011-07-02 13:10:14 -0700564 const StringsAccessor &accessor) {
mukesh agrawal4d260da2012-01-30 11:53:52 -0800565 DCHECK(!Contains(name) || ContainsKey(strings_properties_, name))
566 << "(Already registered " << name << ")";
Chris Masone27c4aa52011-07-02 13:10:14 -0700567 strings_properties_[name] = accessor;
Chris Masoneb925cc82011-06-22 15:39:57 -0700568}
569
Eric Shienbrood30bc0ec2012-03-21 18:19:46 -0400570void PropertyStore::RegisterDerivedStringmap(const string &name,
571 const StringmapAccessor &acc) {
572 DCHECK(!Contains(name) || ContainsKey(stringmap_properties_, name))
573 << "(Already registered " << name << ")";
574 stringmap_properties_[name] = acc;
575}
576
mukesh agrawal66b0aca2012-01-30 15:28:28 -0800577void PropertyStore::RegisterDerivedStringmaps(const string &name,
Chris Masone889666b2011-07-03 12:58:50 -0700578 const StringmapsAccessor &acc) {
mukesh agrawal4d260da2012-01-30 11:53:52 -0800579 DCHECK(!Contains(name) || ContainsKey(stringmaps_properties_, name))
580 << "(Already registered " << name << ")";
Chris Masone889666b2011-07-03 12:58:50 -0700581 stringmaps_properties_[name] = acc;
582}
583
mukesh agrawal66b0aca2012-01-30 15:28:28 -0800584void PropertyStore::RegisterDerivedUint16(const string &name,
Paul Stewartbe5f5b32011-12-07 17:11:11 -0800585 const Uint16Accessor &acc) {
mukesh agrawal4d260da2012-01-30 11:53:52 -0800586 DCHECK(!Contains(name) || ContainsKey(uint16_properties_, name))
587 << "(Already registered " << name << ")";
Paul Stewartbe5f5b32011-12-07 17:11:11 -0800588 uint16_properties_[name] = acc;
589}
590
Paul Stewarte18c33b2012-07-10 20:48:44 -0700591void PropertyStore::RegisterDerivedUint64(const string &name,
592 const Uint64Accessor &acc) {
593 DCHECK(!Contains(name) || ContainsKey(uint64_properties_, name))
594 << "(Already registered " << name << ")";
595 uint64_properties_[name] = acc;
596}
597
mukesh agrawal66b0aca2012-01-30 15:28:28 -0800598// private methods
599
mukesh agrawalffa3d042011-10-06 15:26:10 -0700600template <class V>
Paul Stewarte6e8e492013-01-17 11:00:50 -0800601bool PropertyStore::GetProperty(
602 const string &name,
603 V *value,
604 Error *error,
605 const map< string, std::tr1::shared_ptr<
606 AccessorInterface<V> > >&collection,
607 const string &value_type_english) const {
608 SLOG(Property, 2) << "Getting " << name << " as " << value_type_english
609 << ".";
610 typename map< string, std::tr1::shared_ptr<
611 AccessorInterface<V> > >::const_iterator it = collection.find(name);
612 if (it != collection.end()) {
613 V val = it->second->Get(error);
614 if (error->IsSuccess()) {
615 *value = val;
616 }
617 } else {
618 if (Contains(name)) {
619 error->Populate(
620 Error::kInvalidArguments,
621 "Property " + name + " is not " + value_type_english + ".");
622 } else {
623 error->Populate(
624 Error::kInvalidProperty, "Property " + name + " does not exist.");
625 }
626 }
627 return error->IsSuccess();
628};
629
630template <class V>
mukesh agrawalffa3d042011-10-06 15:26:10 -0700631bool PropertyStore::SetProperty(
632 const string &name,
633 const V &value,
634 Error *error,
635 map< string, std::tr1::shared_ptr< AccessorInterface<V> > >&collection,
636 const string &value_type_english) {
mukesh agrawalbebf1b82013-04-23 15:06:33 -0700637 bool ret = false;
Ben Chanfad4a0b2012-04-18 15:49:59 -0700638 SLOG(Property, 2) << "Setting " << name << " as " << value_type_english
639 << ".";
mukesh agrawalffa3d042011-10-06 15:26:10 -0700640 if (ContainsKey(collection, name)) {
mukesh agrawalbebf1b82013-04-23 15:06:33 -0700641 ret = collection[name]->Set(value, error);
642 if (ret) {
643 if (!property_changed_callback_.is_null()) {
644 property_changed_callback_.Run(name);
645 }
646 }
mukesh agrawalffa3d042011-10-06 15:26:10 -0700647 } else {
648 if (Contains(name)) {
649 error->Populate(
650 Error::kInvalidArguments,
651 "Property " + name + " is not " + value_type_english + ".");
652 } else {
653 error->Populate(
654 Error::kInvalidProperty, "Property " + name + " does not exist.");
655 }
656 }
mukesh agrawalbebf1b82013-04-23 15:06:33 -0700657 return ret;
mukesh agrawalffa3d042011-10-06 15:26:10 -0700658};
659
Chris Masoneb925cc82011-06-22 15:39:57 -0700660} // namespace shill