blob: 5620e6d1a9b77de444b3b50e652fab030a393d44 [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) ||
mukesh agrawale7c7e652013-06-18 17:19:39 -070042 ContainsKey(uint16s_properties_, prop) ||
Jason Glasgowacdc11f2012-03-30 14:12:22 -040043 ContainsKey(uint32_properties_, prop) ||
Paul Stewarte18c33b2012-07-10 20:48:44 -070044 ContainsKey(uint64_properties_, prop) ||
Jason Glasgowacdc11f2012-03-30 14:12:22 -040045 ContainsKey(rpc_identifier_properties_, prop) ||
46 ContainsKey(rpc_identifiers_properties_, prop));
Chris Masoneb925cc82011-06-22 15:39:57 -070047}
48
Paul Stewarte6e8e492013-01-17 11:00:50 -080049bool PropertyStore::GetBoolProperty(const string &name,
50 bool *value,
51 Error *error) const {
52 return GetProperty(name, value, error, bool_properties_, "a bool");
53}
54
55bool PropertyStore::GetInt16Property(const string &name,
56 int16 *value,
57 Error *error) const {
58 return GetProperty(name, value, error, int16_properties_, "an int16");
59}
60
61bool PropertyStore::GetInt32Property(const string &name,
62 int32 *value,
63 Error *error) const {
64 return GetProperty(name, value, error, int32_properties_, "an int32");
65}
66
67bool PropertyStore::GetKeyValueStoreProperty(const string &name,
68 KeyValueStore *value,
69 Error *error) const {
70 return GetProperty(name, value, error, key_value_store_properties_,
71 "a key value store");
72}
73
74bool PropertyStore::GetRpcIdentifierProperty(const string &name,
75 RpcIdentifier *value,
76 Error *error) const {
77 return GetProperty(name, value, error, rpc_identifier_properties_,
78 "an rpc_identifier");
79}
80
81bool PropertyStore::GetStringProperty(const string &name,
82 string *value,
83 Error *error) const {
84 return GetProperty(name, value, error, string_properties_, "a string");
85}
86
87bool PropertyStore::GetStringmapProperty(const string &name,
88 Stringmap *values,
89 Error *error) const {
90 return GetProperty(name, values, error, stringmap_properties_,
91 "a string map");
92}
93
94bool PropertyStore::GetStringmapsProperty(const string &name,
95 Stringmaps *values,
96 Error *error) const {
97 return GetProperty(name, values, error, stringmaps_properties_,
98 "a string map list");
99}
100
101bool PropertyStore::GetStringsProperty(const string &name,
102 Strings *values,
103 Error *error) const {
104 return GetProperty(name, values, error, strings_properties_, "a string list");
105}
106
107bool PropertyStore::GetUint8Property(const string &name,
108 uint8 *value,
109 Error *error) const {
110 return GetProperty(name, value, error, uint8_properties_, "a uint8");
111}
112
113bool PropertyStore::GetUint16Property(const string &name,
114 uint16 *value,
115 Error *error) const {
116 return GetProperty(name, value, error, uint16_properties_, "a uint16");
117}
118
mukesh agrawale7c7e652013-06-18 17:19:39 -0700119bool PropertyStore::GetUint16sProperty(const string &name,
120 Uint16s *value,
121 Error *error) const {
122 return GetProperty(name, value, error, uint16s_properties_, "a uint16 list");
123}
124
Paul Stewarte6e8e492013-01-17 11:00:50 -0800125bool PropertyStore::GetUint32Property(const string &name,
126 uint32 *value,
127 Error *error) const {
128 return GetProperty(name, value, error, uint32_properties_, "a uint32");
129}
130
131bool PropertyStore::GetUint64Property(const string &name,
132 uint64 *value,
133 Error *error) const {
134 return GetProperty(name, value, error, uint64_properties_, "a uint64");
135}
136
mukesh agrawal66b0aca2012-01-30 15:28:28 -0800137bool PropertyStore::SetBoolProperty(const string &name,
Chris Masoneb925cc82011-06-22 15:39:57 -0700138 bool value,
139 Error *error) {
mukesh agrawalffa3d042011-10-06 15:26:10 -0700140 return SetProperty(name, value, error, bool_properties_, "a bool");
Chris Masoneb925cc82011-06-22 15:39:57 -0700141}
142
mukesh agrawal66b0aca2012-01-30 15:28:28 -0800143bool PropertyStore::SetInt16Property(const string &name,
Chris Masoneb925cc82011-06-22 15:39:57 -0700144 int16 value,
145 Error *error) {
mukesh agrawalffa3d042011-10-06 15:26:10 -0700146 return SetProperty(name, value, error, int16_properties_, "an int16");
Chris Masoneb925cc82011-06-22 15:39:57 -0700147}
148
mukesh agrawal66b0aca2012-01-30 15:28:28 -0800149bool PropertyStore::SetInt32Property(const string &name,
Chris Masoneb925cc82011-06-22 15:39:57 -0700150 int32 value,
151 Error *error) {
mukesh agrawalffa3d042011-10-06 15:26:10 -0700152 return SetProperty(name, value, error, int32_properties_, "an int32.");
Chris Masoneb925cc82011-06-22 15:39:57 -0700153}
154
mukesh agrawal66b0aca2012-01-30 15:28:28 -0800155bool PropertyStore::SetStringProperty(const string &name,
156 const string &value,
Chris Masoneb925cc82011-06-22 15:39:57 -0700157 Error *error) {
mukesh agrawalffa3d042011-10-06 15:26:10 -0700158 return SetProperty(name, value, error, string_properties_, "a string");
Chris Masoneb925cc82011-06-22 15:39:57 -0700159}
160
mukesh agrawal66b0aca2012-01-30 15:28:28 -0800161bool PropertyStore::SetStringmapProperty(const string &name,
162 const map<string, string> &values,
163 Error *error) {
mukesh agrawalffa3d042011-10-06 15:26:10 -0700164 return SetProperty(name, values, error, stringmap_properties_,
165 "a string map");
Chris Masoneb925cc82011-06-22 15:39:57 -0700166}
167
mukesh agrawalbebf1b82013-04-23 15:06:33 -0700168bool PropertyStore::SetStringmapsProperty(
169 const string &name,
170 const vector<map<string, string> > &values,
171 Error *error) {
172 return SetProperty(name, values, error, stringmaps_properties_,
173 "a stringmaps");
174}
175
mukesh agrawal66b0aca2012-01-30 15:28:28 -0800176bool PropertyStore::SetStringsProperty(const string &name,
177 const vector<string> &values,
Chris Masoneb925cc82011-06-22 15:39:57 -0700178 Error *error) {
mukesh agrawalffa3d042011-10-06 15:26:10 -0700179 return SetProperty(name, values, error, strings_properties_, "a string list");
Chris Masoneb925cc82011-06-22 15:39:57 -0700180}
181
mukesh agrawal66b0aca2012-01-30 15:28:28 -0800182bool PropertyStore::SetUint8Property(const string &name,
Chris Masoneb925cc82011-06-22 15:39:57 -0700183 uint8 value,
184 Error *error) {
mukesh agrawalffa3d042011-10-06 15:26:10 -0700185 return SetProperty(name, value, error, uint8_properties_, "a uint8");
Chris Masoneb925cc82011-06-22 15:39:57 -0700186}
187
mukesh agrawal66b0aca2012-01-30 15:28:28 -0800188bool PropertyStore::SetUint16Property(const string &name,
Chris Masoneb925cc82011-06-22 15:39:57 -0700189 uint16 value,
190 Error *error) {
mukesh agrawalffa3d042011-10-06 15:26:10 -0700191 return SetProperty(name, value, error, uint16_properties_, "a uint16");
Chris Masoneb925cc82011-06-22 15:39:57 -0700192}
193
mukesh agrawale7c7e652013-06-18 17:19:39 -0700194bool PropertyStore::SetUint16sProperty(const string &name,
195 const vector<uint16> &value,
196 Error *error) {
197 return SetProperty(name, value, error, uint16s_properties_, "a uint16 list");
198}
199
mukesh agrawal66b0aca2012-01-30 15:28:28 -0800200bool PropertyStore::SetUint32Property(const string &name,
Chris Masoneb925cc82011-06-22 15:39:57 -0700201 uint32 value,
202 Error *error) {
mukesh agrawalffa3d042011-10-06 15:26:10 -0700203 return SetProperty(name, value, error, uint32_properties_, "a uint32");
Chris Masoneb925cc82011-06-22 15:39:57 -0700204}
205
Paul Stewarte18c33b2012-07-10 20:48:44 -0700206bool PropertyStore::SetUint64Property(const string &name,
207 uint64 value,
208 Error *error) {
209 return SetProperty(name, value, error, uint64_properties_, "a uint64");
210}
211
Jason Glasgowacdc11f2012-03-30 14:12:22 -0400212bool PropertyStore::SetRpcIdentifierProperty(const string &name,
213 const RpcIdentifier &value,
214 Error *error) {
215 return SetProperty(name, value, error, rpc_identifier_properties_,
216 "an rpc_identifier");
217}
218
mukesh agrawal4d260da2012-01-30 11:53:52 -0800219bool PropertyStore::ClearProperty(const string &name, Error *error) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700220 SLOG(Property, 2) << "Clearing " << name << ".";
mukesh agrawal4d260da2012-01-30 11:53:52 -0800221
222 if (ContainsKey(bool_properties_, name)) {
223 bool_properties_[name]->Clear(error);
224 } else if (ContainsKey(int16_properties_, name)) {
225 int16_properties_[name]->Clear(error);
226 } else if (ContainsKey(int32_properties_, name)) {
227 int32_properties_[name]->Clear(error);
228 } else if (ContainsKey(key_value_store_properties_, name)) {
229 key_value_store_properties_[name]->Clear(error);
230 } else if (ContainsKey(string_properties_, name)) {
231 string_properties_[name]->Clear(error);
232 } else if (ContainsKey(stringmap_properties_, name)) {
233 stringmap_properties_[name]->Clear(error);
234 } else if (ContainsKey(stringmaps_properties_, name)) {
235 stringmaps_properties_[name]->Clear(error);
236 } else if (ContainsKey(strings_properties_, name)) {
237 strings_properties_[name]->Clear(error);
238 } else if (ContainsKey(uint8_properties_, name)) {
239 uint8_properties_[name]->Clear(error);
240 } else if (ContainsKey(uint16_properties_, name)) {
241 uint16_properties_[name]->Clear(error);
mukesh agrawale7c7e652013-06-18 17:19:39 -0700242 } else if (ContainsKey(uint16s_properties_, name)) {
243 uint16s_properties_[name]->Clear(error);
mukesh agrawal4d260da2012-01-30 11:53:52 -0800244 } else if (ContainsKey(uint32_properties_, name)) {
245 uint32_properties_[name]->Clear(error);
Paul Stewarte18c33b2012-07-10 20:48:44 -0700246 } else if (ContainsKey(uint64_properties_, name)) {
247 uint64_properties_[name]->Clear(error);
Jason Glasgowacdc11f2012-03-30 14:12:22 -0400248 } else if (ContainsKey(rpc_identifier_properties_, name)) {
249 rpc_identifier_properties_[name]->Clear(error);
250 } else if (ContainsKey(rpc_identifiers_properties_, name)) {
251 rpc_identifiers_properties_[name]->Clear(error);
mukesh agrawal4d260da2012-01-30 11:53:52 -0800252 } else {
253 error->Populate(
254 Error::kInvalidProperty, "Property " + name + " does not exist.");
255 }
mukesh agrawalbebf1b82013-04-23 15:06:33 -0700256 if (error->IsSuccess()) {
257 if (!property_changed_callback_.is_null()) {
258 property_changed_callback_.Run(name);
259 }
260 }
mukesh agrawal4d260da2012-01-30 11:53:52 -0800261 return error->IsSuccess();
262}
263
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800264ReadablePropertyConstIterator<bool> PropertyStore::GetBoolPropertiesIter()
mukesh agrawalde29fa82011-09-16 16:16:36 -0700265 const {
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800266 return ReadablePropertyConstIterator<bool>(bool_properties_);
Chris Masonea8a2c252011-06-27 22:16:30 -0700267}
268
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800269ReadablePropertyConstIterator<int16> PropertyStore::GetInt16PropertiesIter()
mukesh agrawalde29fa82011-09-16 16:16:36 -0700270 const {
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800271 return ReadablePropertyConstIterator<int16>(int16_properties_);
Chris Masonea8a2c252011-06-27 22:16:30 -0700272}
273
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800274ReadablePropertyConstIterator<int32> PropertyStore::GetInt32PropertiesIter()
mukesh agrawalde29fa82011-09-16 16:16:36 -0700275 const {
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800276 return ReadablePropertyConstIterator<int32>(int32_properties_);
Chris Masone889666b2011-07-03 12:58:50 -0700277}
278
Darin Petkov63138a92012-02-06 14:09:15 +0100279ReadablePropertyConstIterator<KeyValueStore>
280PropertyStore::GetKeyValueStorePropertiesIter() const {
281 return
282 ReadablePropertyConstIterator<KeyValueStore>(key_value_store_properties_);
283}
284
Jason Glasgowacdc11f2012-03-30 14:12:22 -0400285ReadablePropertyConstIterator<RpcIdentifier>
286PropertyStore::GetRpcIdentifierPropertiesIter() const {
287 return ReadablePropertyConstIterator<RpcIdentifier>(
288 rpc_identifier_properties_);
289}
290
mukesh agrawal2366eed2012-03-20 18:21:50 -0700291ReadablePropertyConstIterator<RpcIdentifiers>
292PropertyStore::GetRpcIdentifiersPropertiesIter() const {
293 return ReadablePropertyConstIterator<RpcIdentifiers>(
294 rpc_identifiers_properties_);
295}
296
mukesh agrawal66b0aca2012-01-30 15:28:28 -0800297ReadablePropertyConstIterator<string>
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800298PropertyStore::GetStringPropertiesIter() const {
mukesh agrawal66b0aca2012-01-30 15:28:28 -0800299 return ReadablePropertyConstIterator<string>(string_properties_);
Chris Masonea8a2c252011-06-27 22:16:30 -0700300}
301
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800302ReadablePropertyConstIterator<Stringmap>
303PropertyStore::GetStringmapPropertiesIter() const {
304 return ReadablePropertyConstIterator<Stringmap>(stringmap_properties_);
Chris Masonea8a2c252011-06-27 22:16:30 -0700305}
306
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800307ReadablePropertyConstIterator<Stringmaps>
308PropertyStore::GetStringmapsPropertiesIter()
309 const {
310 return ReadablePropertyConstIterator<Stringmaps>(stringmaps_properties_);
311}
312
313ReadablePropertyConstIterator<Strings> PropertyStore::GetStringsPropertiesIter()
314 const {
315 return ReadablePropertyConstIterator<Strings>(strings_properties_);
316}
317
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800318ReadablePropertyConstIterator<uint8> PropertyStore::GetUint8PropertiesIter()
319 const {
320 return ReadablePropertyConstIterator<uint8>(uint8_properties_);
321}
322
323ReadablePropertyConstIterator<uint16> PropertyStore::GetUint16PropertiesIter()
324 const {
325 return ReadablePropertyConstIterator<uint16>(uint16_properties_);
326}
327
mukesh agrawale7c7e652013-06-18 17:19:39 -0700328ReadablePropertyConstIterator<Uint16s> PropertyStore::GetUint16sPropertiesIter()
329 const {
330 return ReadablePropertyConstIterator<Uint16s>(uint16s_properties_);
331}
332
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800333ReadablePropertyConstIterator<uint32> PropertyStore::GetUint32PropertiesIter()
334 const {
335 return ReadablePropertyConstIterator<uint32>(uint32_properties_);
Chris Masonea8a2c252011-06-27 22:16:30 -0700336}
337
Paul Stewarte18c33b2012-07-10 20:48:44 -0700338ReadablePropertyConstIterator<uint64> PropertyStore::GetUint64PropertiesIter()
339 const {
340 return ReadablePropertyConstIterator<uint64>(uint64_properties_);
341}
342
Chris Masoneb925cc82011-06-22 15:39:57 -0700343void PropertyStore::RegisterBool(const string &name, bool *prop) {
mukesh agrawal4d260da2012-01-30 11:53:52 -0800344 DCHECK(!Contains(name) || ContainsKey(bool_properties_, name))
345 << "(Already registered " << name << ")";
Chris Masoneb925cc82011-06-22 15:39:57 -0700346 bool_properties_[name] = BoolAccessor(new PropertyAccessor<bool>(prop));
347}
348
349void PropertyStore::RegisterConstBool(const string &name, const bool *prop) {
mukesh agrawal4d260da2012-01-30 11:53:52 -0800350 DCHECK(!Contains(name) || ContainsKey(bool_properties_, name))
351 << "(Already registered " << name << ")";
Chris Masoneb925cc82011-06-22 15:39:57 -0700352 bool_properties_[name] = BoolAccessor(new ConstPropertyAccessor<bool>(prop));
353}
354
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800355void PropertyStore::RegisterWriteOnlyBool(const string &name, bool *prop) {
mukesh agrawal4d260da2012-01-30 11:53:52 -0800356 DCHECK(!Contains(name) || ContainsKey(bool_properties_, name))
357 << "(Already registered " << name << ")";
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800358 bool_properties_[name] = BoolAccessor(
359 new WriteOnlyPropertyAccessor<bool>(prop));
360}
361
Chris Masoneb925cc82011-06-22 15:39:57 -0700362void PropertyStore::RegisterInt16(const string &name, int16 *prop) {
mukesh agrawal4d260da2012-01-30 11:53:52 -0800363 DCHECK(!Contains(name) || ContainsKey(int16_properties_, name))
364 << "(Already registered " << name << ")";
Chris Masoneb925cc82011-06-22 15:39:57 -0700365 int16_properties_[name] = Int16Accessor(new PropertyAccessor<int16>(prop));
366}
367
368void PropertyStore::RegisterConstInt16(const string &name, const int16 *prop) {
mukesh agrawal4d260da2012-01-30 11:53:52 -0800369 DCHECK(!Contains(name) || ContainsKey(int16_properties_, name))
370 << "(Already registered " << name << ")";
Chris Masoneb925cc82011-06-22 15:39:57 -0700371 int16_properties_[name] =
372 Int16Accessor(new ConstPropertyAccessor<int16>(prop));
373}
374
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800375void PropertyStore::RegisterWriteOnlyInt16(const string &name, int16 *prop) {
mukesh agrawal4d260da2012-01-30 11:53:52 -0800376 DCHECK(!Contains(name) || ContainsKey(int16_properties_, name))
377 << "(Already registered " << name << ")";
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800378 int16_properties_[name] =
379 Int16Accessor(new WriteOnlyPropertyAccessor<int16>(prop));
380}
Chris Masoneb925cc82011-06-22 15:39:57 -0700381void PropertyStore::RegisterInt32(const string &name, int32 *prop) {
mukesh agrawal4d260da2012-01-30 11:53:52 -0800382 DCHECK(!Contains(name) || ContainsKey(int32_properties_, name))
383 << "(Already registered " << name << ")";
Chris Masoneb925cc82011-06-22 15:39:57 -0700384 int32_properties_[name] = Int32Accessor(new PropertyAccessor<int32>(prop));
385}
386
387void PropertyStore::RegisterConstInt32(const string &name, const int32 *prop) {
mukesh agrawal4d260da2012-01-30 11:53:52 -0800388 DCHECK(!Contains(name) || ContainsKey(int32_properties_, name))
389 << "(Already registered " << name << ")";
Chris Masoneb925cc82011-06-22 15:39:57 -0700390 int32_properties_[name] =
391 Int32Accessor(new ConstPropertyAccessor<int32>(prop));
392}
393
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800394void PropertyStore::RegisterWriteOnlyInt32(const string &name, int32 *prop) {
mukesh agrawal4d260da2012-01-30 11:53:52 -0800395 DCHECK(!Contains(name) || ContainsKey(int32_properties_, name))
396 << "(Already registered " << name << ")";
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800397 int32_properties_[name] =
398 Int32Accessor(new WriteOnlyPropertyAccessor<int32>(prop));
399}
400
Chris Masoneb925cc82011-06-22 15:39:57 -0700401void PropertyStore::RegisterString(const string &name, string *prop) {
mukesh agrawal4d260da2012-01-30 11:53:52 -0800402 DCHECK(!Contains(name) || ContainsKey(string_properties_, name))
403 << "(Already registered " << name << ")";
Chris Masoneb925cc82011-06-22 15:39:57 -0700404 string_properties_[name] = StringAccessor(new PropertyAccessor<string>(prop));
405}
406
407void PropertyStore::RegisterConstString(const string &name,
408 const string *prop) {
mukesh agrawal4d260da2012-01-30 11:53:52 -0800409 DCHECK(!Contains(name) || ContainsKey(string_properties_, name))
410 << "(Already registered " << name << ")";
Chris Masoneb925cc82011-06-22 15:39:57 -0700411 string_properties_[name] =
412 StringAccessor(new ConstPropertyAccessor<string>(prop));
413}
414
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800415void PropertyStore::RegisterWriteOnlyString(const string &name, string *prop) {
mukesh agrawal4d260da2012-01-30 11:53:52 -0800416 DCHECK(!Contains(name) || ContainsKey(string_properties_, name))
417 << "(Already registered " << name << ")";
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800418 string_properties_[name] =
419 StringAccessor(new WriteOnlyPropertyAccessor<string>(prop));
420}
421
Chris Masone43b48a12011-07-01 13:37:07 -0700422void PropertyStore::RegisterStringmap(const string &name, Stringmap *prop) {
mukesh agrawal4d260da2012-01-30 11:53:52 -0800423 DCHECK(!Contains(name) || ContainsKey(stringmap_properties_, name))
424 << "(Already registered " << name << ")";
Chris Masoneb925cc82011-06-22 15:39:57 -0700425 stringmap_properties_[name] =
Chris Masone43b48a12011-07-01 13:37:07 -0700426 StringmapAccessor(new PropertyAccessor<Stringmap>(prop));
Chris Masoneb925cc82011-06-22 15:39:57 -0700427}
428
429void PropertyStore::RegisterConstStringmap(const string &name,
Chris Masone43b48a12011-07-01 13:37:07 -0700430 const Stringmap *prop) {
mukesh agrawal4d260da2012-01-30 11:53:52 -0800431 DCHECK(!Contains(name) || ContainsKey(stringmap_properties_, name))
432 << "(Already registered " << name << ")";
Chris Masoneb925cc82011-06-22 15:39:57 -0700433 stringmap_properties_[name] =
Chris Masone43b48a12011-07-01 13:37:07 -0700434 StringmapAccessor(new ConstPropertyAccessor<Stringmap>(prop));
435}
436
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800437void PropertyStore::RegisterWriteOnlyStringmap(const string &name,
438 Stringmap *prop) {
mukesh agrawal4d260da2012-01-30 11:53:52 -0800439 DCHECK(!Contains(name) || ContainsKey(stringmap_properties_, name))
440 << "(Already registered " << name << ")";
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800441 stringmap_properties_[name] =
442 StringmapAccessor(new WriteOnlyPropertyAccessor<Stringmap>(prop));
443}
444
Darin Petkovc0865312011-09-16 15:31:20 -0700445void PropertyStore::RegisterStringmaps(const string &name, Stringmaps *prop) {
mukesh agrawal4d260da2012-01-30 11:53:52 -0800446 DCHECK(!Contains(name) || ContainsKey(stringmaps_properties_, name))
447 << "(Already registered " << name << ")";
Darin Petkovc0865312011-09-16 15:31:20 -0700448 stringmaps_properties_[name] =
449 StringmapsAccessor(new PropertyAccessor<Stringmaps>(prop));
450}
451
452void PropertyStore::RegisterConstStringmaps(const string &name,
453 const Stringmaps *prop) {
mukesh agrawal4d260da2012-01-30 11:53:52 -0800454 DCHECK(!Contains(name) || ContainsKey(stringmaps_properties_, name))
455 << "(Already registered " << name << ")";
Darin Petkovc0865312011-09-16 15:31:20 -0700456 stringmaps_properties_[name] =
457 StringmapsAccessor(new ConstPropertyAccessor<Stringmaps>(prop));
458}
459
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800460void PropertyStore::RegisterWriteOnlyStringmaps(const string &name,
461 Stringmaps *prop) {
mukesh agrawal4d260da2012-01-30 11:53:52 -0800462 DCHECK(!Contains(name) || ContainsKey(stringmaps_properties_, name))
463 << "(Already registered " << name << ")";
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800464 stringmaps_properties_[name] =
465 StringmapsAccessor(new WriteOnlyPropertyAccessor<Stringmaps>(prop));
466}
467
Chris Masone43b48a12011-07-01 13:37:07 -0700468void PropertyStore::RegisterStrings(const string &name, Strings *prop) {
mukesh agrawal4d260da2012-01-30 11:53:52 -0800469 DCHECK(!Contains(name) || ContainsKey(strings_properties_, name))
470 << "(Already registered " << name << ")";
Chris Masone43b48a12011-07-01 13:37:07 -0700471 strings_properties_[name] =
472 StringsAccessor(new PropertyAccessor<Strings>(prop));
Chris Masoneb925cc82011-06-22 15:39:57 -0700473}
474
Chris Masone889666b2011-07-03 12:58:50 -0700475void PropertyStore::RegisterConstStrings(const string &name,
476 const Strings *prop) {
mukesh agrawal4d260da2012-01-30 11:53:52 -0800477 DCHECK(!Contains(name) || ContainsKey(strings_properties_, name))
478 << "(Already registered " << name << ")";
Chris Masone889666b2011-07-03 12:58:50 -0700479 strings_properties_[name] =
480 StringsAccessor(new ConstPropertyAccessor<Strings>(prop));
481}
482
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800483void PropertyStore::RegisterWriteOnlyStrings(const string &name,
484 Strings *prop) {
mukesh agrawal4d260da2012-01-30 11:53:52 -0800485 DCHECK(!Contains(name) || ContainsKey(strings_properties_, name))
486 << "(Already registered " << name << ")";
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800487 strings_properties_[name] =
488 StringsAccessor(new WriteOnlyPropertyAccessor<Strings>(prop));
489}
490
Chris Masone889666b2011-07-03 12:58:50 -0700491void PropertyStore::RegisterUint8(const string &name, uint8 *prop) {
mukesh agrawal4d260da2012-01-30 11:53:52 -0800492 DCHECK(!Contains(name) || ContainsKey(uint8_properties_, name))
493 << "(Already registered " << name << ")";
Chris Masone889666b2011-07-03 12:58:50 -0700494 uint8_properties_[name] = Uint8Accessor(new PropertyAccessor<uint8>(prop));
495}
496
Chris Masoneb925cc82011-06-22 15:39:57 -0700497void PropertyStore::RegisterConstUint8(const string &name, const uint8 *prop) {
mukesh agrawal4d260da2012-01-30 11:53:52 -0800498 DCHECK(!Contains(name) || ContainsKey(uint8_properties_, name))
499 << "(Already registered " << name << ")";
Chris Masoneb925cc82011-06-22 15:39:57 -0700500 uint8_properties_[name] =
501 Uint8Accessor(new ConstPropertyAccessor<uint8>(prop));
502}
503
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800504void PropertyStore::RegisterWriteOnlyUint8(const string &name, uint8 *prop) {
mukesh agrawal4d260da2012-01-30 11:53:52 -0800505 DCHECK(!Contains(name) || ContainsKey(uint8_properties_, name))
506 << "(Already registered " << name << ")";
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800507 uint8_properties_[name] =
508 Uint8Accessor(new WriteOnlyPropertyAccessor<uint8>(prop));
509}
510
mukesh agrawal66b0aca2012-01-30 15:28:28 -0800511void PropertyStore::RegisterUint16(const string &name, uint16 *prop) {
mukesh agrawal4d260da2012-01-30 11:53:52 -0800512 DCHECK(!Contains(name) || ContainsKey(uint16_properties_, name))
513 << "(Already registered " << name << ")";
Chris Masoneb925cc82011-06-22 15:39:57 -0700514 uint16_properties_[name] = Uint16Accessor(new PropertyAccessor<uint16>(prop));
515}
516
mukesh agrawale7c7e652013-06-18 17:19:39 -0700517void PropertyStore::RegisterUint16s(const string &name, Uint16s *prop) {
518 DCHECK(!Contains(name) || ContainsKey(uint16s_properties_, name))
519 << "(Already registered " << name << ")";
520 uint16s_properties_[name] =
521 Uint16sAccessor(new PropertyAccessor<Uint16s>(prop));
522}
523
mukesh agrawal4d260da2012-01-30 11:53:52 -0800524void PropertyStore::RegisterUint32(const std::string &name, uint32 *prop) {
525 DCHECK(!Contains(name) || ContainsKey(uint32_properties_, name))
526 << "(Already registered " << name << ")";
527 uint32_properties_[name] = Uint32Accessor(new PropertyAccessor<uint32>(prop));
528}
529
Chris Masoneb925cc82011-06-22 15:39:57 -0700530void PropertyStore::RegisterConstUint16(const string &name,
531 const uint16 *prop) {
mukesh agrawal4d260da2012-01-30 11:53:52 -0800532 DCHECK(!Contains(name) || ContainsKey(uint16_properties_, name))
533 << "(Already registered " << name << ")";
Chris Masoneb925cc82011-06-22 15:39:57 -0700534 uint16_properties_[name] =
535 Uint16Accessor(new ConstPropertyAccessor<uint16>(prop));
536}
537
mukesh agrawale7c7e652013-06-18 17:19:39 -0700538void PropertyStore::RegisterConstUint16s(const string &name,
539 const Uint16s *prop) {
540 DCHECK(!Contains(name) || ContainsKey(uint16s_properties_, name))
541 << "(Already registered " << name << ")";
542 uint16s_properties_[name] =
543 Uint16sAccessor(new ConstPropertyAccessor<Uint16s>(prop));
544}
545
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800546void PropertyStore::RegisterWriteOnlyUint16(const string &name, uint16 *prop) {
mukesh agrawal4d260da2012-01-30 11:53:52 -0800547 DCHECK(!Contains(name) || ContainsKey(uint16_properties_, name))
548 << "(Already registered " << name << ")";
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800549 uint16_properties_[name] =
550 Uint16Accessor(new WriteOnlyPropertyAccessor<uint16>(prop));
551}
552
mukesh agrawal66b0aca2012-01-30 15:28:28 -0800553void PropertyStore::RegisterDerivedBool(const string &name,
Chris Masone27c4aa52011-07-02 13:10:14 -0700554 const BoolAccessor &accessor) {
mukesh agrawal4d260da2012-01-30 11:53:52 -0800555 DCHECK(!Contains(name) || ContainsKey(bool_properties_, name))
556 << "(Already registered " << name << ")";
Chris Masone27c4aa52011-07-02 13:10:14 -0700557 bool_properties_[name] = accessor;
558}
559
mukesh agrawal66b0aca2012-01-30 15:28:28 -0800560void PropertyStore::RegisterDerivedInt32(const string &name,
mukesh agrawal4d0401c2012-01-06 16:05:31 -0800561 const Int32Accessor &accessor) {
mukesh agrawal4d260da2012-01-30 11:53:52 -0800562 DCHECK(!Contains(name) || ContainsKey(int32_properties_, name))
563 << "(Already registered " << name << ")";
mukesh agrawal4d0401c2012-01-06 16:05:31 -0800564 int32_properties_[name] = accessor;
565}
566
Darin Petkov63138a92012-02-06 14:09:15 +0100567void PropertyStore::RegisterDerivedKeyValueStore(
mukesh agrawal66b0aca2012-01-30 15:28:28 -0800568 const string &name,
Darin Petkov63138a92012-02-06 14:09:15 +0100569 const KeyValueStoreAccessor &acc) {
mukesh agrawal4d260da2012-01-30 11:53:52 -0800570 DCHECK(!Contains(name) || ContainsKey(key_value_store_properties_, name))
571 << "(Already registered " << name << ")";
Darin Petkov63138a92012-02-06 14:09:15 +0100572 key_value_store_properties_[name] = acc;
573}
574
Jason Glasgowacdc11f2012-03-30 14:12:22 -0400575void PropertyStore::RegisterDerivedRpcIdentifier(
576 const string &name,
577 const RpcIdentifierAccessor &acc) {
578 DCHECK(!Contains(name) || ContainsKey(rpc_identifier_properties_, name))
579 << "(Already registered " << name << ")";
580 rpc_identifier_properties_[name] = acc;
581}
582
mukesh agrawal2366eed2012-03-20 18:21:50 -0700583void PropertyStore::RegisterDerivedRpcIdentifiers(
584 const string &name,
585 const RpcIdentifiersAccessor &accessor) {
586 DCHECK(!Contains(name) || ContainsKey(rpc_identifiers_properties_, name))
587 << "(Already registered " << name << ")";
588 rpc_identifiers_properties_[name] = accessor;
589}
590
mukesh agrawal66b0aca2012-01-30 15:28:28 -0800591void PropertyStore::RegisterDerivedString(const string &name,
Chris Masone27c4aa52011-07-02 13:10:14 -0700592 const StringAccessor &accessor) {
mukesh agrawal4d260da2012-01-30 11:53:52 -0800593 DCHECK(!Contains(name) || ContainsKey(string_properties_, name))
594 << "(Already registered " << name << ")";
Chris Masone27c4aa52011-07-02 13:10:14 -0700595 string_properties_[name] = accessor;
596}
597
mukesh agrawal66b0aca2012-01-30 15:28:28 -0800598void PropertyStore::RegisterDerivedStrings(const string &name,
Chris Masone27c4aa52011-07-02 13:10:14 -0700599 const StringsAccessor &accessor) {
mukesh agrawal4d260da2012-01-30 11:53:52 -0800600 DCHECK(!Contains(name) || ContainsKey(strings_properties_, name))
601 << "(Already registered " << name << ")";
Chris Masone27c4aa52011-07-02 13:10:14 -0700602 strings_properties_[name] = accessor;
Chris Masoneb925cc82011-06-22 15:39:57 -0700603}
604
Eric Shienbrood30bc0ec2012-03-21 18:19:46 -0400605void PropertyStore::RegisterDerivedStringmap(const string &name,
606 const StringmapAccessor &acc) {
607 DCHECK(!Contains(name) || ContainsKey(stringmap_properties_, name))
608 << "(Already registered " << name << ")";
609 stringmap_properties_[name] = acc;
610}
611
mukesh agrawal66b0aca2012-01-30 15:28:28 -0800612void PropertyStore::RegisterDerivedStringmaps(const string &name,
Chris Masone889666b2011-07-03 12:58:50 -0700613 const StringmapsAccessor &acc) {
mukesh agrawal4d260da2012-01-30 11:53:52 -0800614 DCHECK(!Contains(name) || ContainsKey(stringmaps_properties_, name))
615 << "(Already registered " << name << ")";
Chris Masone889666b2011-07-03 12:58:50 -0700616 stringmaps_properties_[name] = acc;
617}
618
mukesh agrawal66b0aca2012-01-30 15:28:28 -0800619void PropertyStore::RegisterDerivedUint16(const string &name,
Paul Stewartbe5f5b32011-12-07 17:11:11 -0800620 const Uint16Accessor &acc) {
mukesh agrawal4d260da2012-01-30 11:53:52 -0800621 DCHECK(!Contains(name) || ContainsKey(uint16_properties_, name))
622 << "(Already registered " << name << ")";
Paul Stewartbe5f5b32011-12-07 17:11:11 -0800623 uint16_properties_[name] = acc;
624}
625
Paul Stewarte18c33b2012-07-10 20:48:44 -0700626void PropertyStore::RegisterDerivedUint64(const string &name,
627 const Uint64Accessor &acc) {
628 DCHECK(!Contains(name) || ContainsKey(uint64_properties_, name))
629 << "(Already registered " << name << ")";
630 uint64_properties_[name] = acc;
631}
632
mukesh agrawal66b0aca2012-01-30 15:28:28 -0800633// private methods
634
mukesh agrawalffa3d042011-10-06 15:26:10 -0700635template <class V>
Paul Stewarte6e8e492013-01-17 11:00:50 -0800636bool PropertyStore::GetProperty(
637 const string &name,
638 V *value,
639 Error *error,
640 const map< string, std::tr1::shared_ptr<
641 AccessorInterface<V> > >&collection,
642 const string &value_type_english) const {
643 SLOG(Property, 2) << "Getting " << name << " as " << value_type_english
644 << ".";
645 typename map< string, std::tr1::shared_ptr<
646 AccessorInterface<V> > >::const_iterator it = collection.find(name);
647 if (it != collection.end()) {
648 V val = it->second->Get(error);
649 if (error->IsSuccess()) {
650 *value = val;
651 }
652 } else {
653 if (Contains(name)) {
654 error->Populate(
655 Error::kInvalidArguments,
656 "Property " + name + " is not " + value_type_english + ".");
657 } else {
658 error->Populate(
659 Error::kInvalidProperty, "Property " + name + " does not exist.");
660 }
661 }
662 return error->IsSuccess();
663};
664
665template <class V>
mukesh agrawalffa3d042011-10-06 15:26:10 -0700666bool PropertyStore::SetProperty(
667 const string &name,
668 const V &value,
669 Error *error,
670 map< string, std::tr1::shared_ptr< AccessorInterface<V> > >&collection,
671 const string &value_type_english) {
mukesh agrawalbebf1b82013-04-23 15:06:33 -0700672 bool ret = false;
Ben Chanfad4a0b2012-04-18 15:49:59 -0700673 SLOG(Property, 2) << "Setting " << name << " as " << value_type_english
674 << ".";
mukesh agrawalffa3d042011-10-06 15:26:10 -0700675 if (ContainsKey(collection, name)) {
mukesh agrawalbebf1b82013-04-23 15:06:33 -0700676 ret = collection[name]->Set(value, error);
677 if (ret) {
678 if (!property_changed_callback_.is_null()) {
679 property_changed_callback_.Run(name);
680 }
681 }
mukesh agrawalffa3d042011-10-06 15:26:10 -0700682 } else {
683 if (Contains(name)) {
684 error->Populate(
685 Error::kInvalidArguments,
686 "Property " + name + " is not " + value_type_english + ".");
687 } else {
688 error->Populate(
689 Error::kInvalidProperty, "Property " + name + " does not exist.");
690 }
691 }
mukesh agrawalbebf1b82013-04-23 15:06:33 -0700692 return ret;
mukesh agrawalffa3d042011-10-06 15:26:10 -0700693};
694
Chris Masoneb925cc82011-06-22 15:39:57 -0700695} // namespace shill