blob: f00b2194354474dc3c2986e26f73f99ad8b37400 [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,
Ben Chan7fab8972014-08-10 17:14:46 -070056 int16_t *value,
Paul Stewarte6e8e492013-01-17 11:00:50 -080057 Error *error) const {
Ben Chan7fab8972014-08-10 17:14:46 -070058 return GetProperty(name, value, error, int16_properties_, "an int16_t");
Paul Stewarte6e8e492013-01-17 11:00:50 -080059}
60
61bool PropertyStore::GetInt32Property(const string &name,
Ben Chan7fab8972014-08-10 17:14:46 -070062 int32_t *value,
Paul Stewarte6e8e492013-01-17 11:00:50 -080063 Error *error) const {
Ben Chan7fab8972014-08-10 17:14:46 -070064 return GetProperty(name, value, error, int32_properties_, "an int32_t");
Paul Stewarte6e8e492013-01-17 11:00:50 -080065}
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,
Ben Chan7fab8972014-08-10 17:14:46 -0700108 uint8_t *value,
Paul Stewarte6e8e492013-01-17 11:00:50 -0800109 Error *error) const {
Ben Chan7fab8972014-08-10 17:14:46 -0700110 return GetProperty(name, value, error, uint8_properties_, "a uint8_t");
Paul Stewarte6e8e492013-01-17 11:00:50 -0800111}
112
113bool PropertyStore::GetUint16Property(const string &name,
Ben Chan7fab8972014-08-10 17:14:46 -0700114 uint16_t *value,
Paul Stewarte6e8e492013-01-17 11:00:50 -0800115 Error *error) const {
Ben Chan7fab8972014-08-10 17:14:46 -0700116 return GetProperty(name, value, error, uint16_properties_, "a uint16_t");
Paul Stewarte6e8e492013-01-17 11:00:50 -0800117}
118
mukesh agrawale7c7e652013-06-18 17:19:39 -0700119bool PropertyStore::GetUint16sProperty(const string &name,
120 Uint16s *value,
121 Error *error) const {
Ben Chan7fab8972014-08-10 17:14:46 -0700122 return GetProperty(name, value, error, uint16s_properties_,
123 "a uint16_t list");
mukesh agrawale7c7e652013-06-18 17:19:39 -0700124}
125
Paul Stewarte6e8e492013-01-17 11:00:50 -0800126bool PropertyStore::GetUint32Property(const string &name,
Ben Chan7fab8972014-08-10 17:14:46 -0700127 uint32_t *value,
Paul Stewarte6e8e492013-01-17 11:00:50 -0800128 Error *error) const {
Ben Chan7fab8972014-08-10 17:14:46 -0700129 return GetProperty(name, value, error, uint32_properties_, "a uint32_t");
Paul Stewarte6e8e492013-01-17 11:00:50 -0800130}
131
132bool PropertyStore::GetUint64Property(const string &name,
Ben Chan7fab8972014-08-10 17:14:46 -0700133 uint64_t *value,
Paul Stewarte6e8e492013-01-17 11:00:50 -0800134 Error *error) const {
Ben Chan7fab8972014-08-10 17:14:46 -0700135 return GetProperty(name, value, error, uint64_properties_, "a uint64_t");
Paul Stewarte6e8e492013-01-17 11:00:50 -0800136}
137
mukesh agrawal66b0aca2012-01-30 15:28:28 -0800138bool PropertyStore::SetBoolProperty(const string &name,
Chris Masoneb925cc82011-06-22 15:39:57 -0700139 bool value,
140 Error *error) {
Alex Vakulenko8a532292014-06-16 17:18:44 -0700141 return SetProperty(name, value, error, &bool_properties_, "a bool");
Chris Masoneb925cc82011-06-22 15:39:57 -0700142}
143
mukesh agrawal66b0aca2012-01-30 15:28:28 -0800144bool PropertyStore::SetInt16Property(const string &name,
Ben Chan7fab8972014-08-10 17:14:46 -0700145 int16_t value,
Chris Masoneb925cc82011-06-22 15:39:57 -0700146 Error *error) {
Ben Chan7fab8972014-08-10 17:14:46 -0700147 return SetProperty(name, value, error, &int16_properties_, "an int16_t");
Chris Masoneb925cc82011-06-22 15:39:57 -0700148}
149
mukesh agrawal66b0aca2012-01-30 15:28:28 -0800150bool PropertyStore::SetInt32Property(const string &name,
Ben Chan7fab8972014-08-10 17:14:46 -0700151 int32_t value,
Chris Masoneb925cc82011-06-22 15:39:57 -0700152 Error *error) {
Ben Chan7fab8972014-08-10 17:14:46 -0700153 return SetProperty(name, value, error, &int32_properties_, "an int32_t.");
Chris Masoneb925cc82011-06-22 15:39:57 -0700154}
155
mukesh agrawal66b0aca2012-01-30 15:28:28 -0800156bool PropertyStore::SetStringProperty(const string &name,
157 const string &value,
Chris Masoneb925cc82011-06-22 15:39:57 -0700158 Error *error) {
Alex Vakulenko8a532292014-06-16 17:18:44 -0700159 return SetProperty(name, value, error, &string_properties_, "a string");
Chris Masoneb925cc82011-06-22 15:39:57 -0700160}
161
mukesh agrawal66b0aca2012-01-30 15:28:28 -0800162bool PropertyStore::SetStringmapProperty(const string &name,
163 const map<string, string> &values,
164 Error *error) {
Alex Vakulenko8a532292014-06-16 17:18:44 -0700165 return SetProperty(name, values, error, &stringmap_properties_,
mukesh agrawalffa3d042011-10-06 15:26:10 -0700166 "a string map");
Chris Masoneb925cc82011-06-22 15:39:57 -0700167}
168
mukesh agrawalbebf1b82013-04-23 15:06:33 -0700169bool PropertyStore::SetStringmapsProperty(
170 const string &name,
171 const vector<map<string, string> > &values,
172 Error *error) {
Alex Vakulenko8a532292014-06-16 17:18:44 -0700173 return SetProperty(name, values, error, &stringmaps_properties_,
mukesh agrawalbebf1b82013-04-23 15:06:33 -0700174 "a stringmaps");
175}
176
mukesh agrawal66b0aca2012-01-30 15:28:28 -0800177bool PropertyStore::SetStringsProperty(const string &name,
178 const vector<string> &values,
Chris Masoneb925cc82011-06-22 15:39:57 -0700179 Error *error) {
Alex Vakulenko8a532292014-06-16 17:18:44 -0700180 return SetProperty(name, values, error, &strings_properties_,
181 "a string list");
Chris Masoneb925cc82011-06-22 15:39:57 -0700182}
183
mukesh agrawal66b0aca2012-01-30 15:28:28 -0800184bool PropertyStore::SetUint8Property(const string &name,
Ben Chan7fab8972014-08-10 17:14:46 -0700185 uint8_t value,
Chris Masoneb925cc82011-06-22 15:39:57 -0700186 Error *error) {
Ben Chan7fab8972014-08-10 17:14:46 -0700187 return SetProperty(name, value, error, &uint8_properties_, "a uint8_t");
Chris Masoneb925cc82011-06-22 15:39:57 -0700188}
189
mukesh agrawal66b0aca2012-01-30 15:28:28 -0800190bool PropertyStore::SetUint16Property(const string &name,
Ben Chan7fab8972014-08-10 17:14:46 -0700191 uint16_t value,
Chris Masoneb925cc82011-06-22 15:39:57 -0700192 Error *error) {
Ben Chan7fab8972014-08-10 17:14:46 -0700193 return SetProperty(name, value, error, &uint16_properties_, "a uint16_t");
Chris Masoneb925cc82011-06-22 15:39:57 -0700194}
195
mukesh agrawale7c7e652013-06-18 17:19:39 -0700196bool PropertyStore::SetUint16sProperty(const string &name,
Ben Chan7fab8972014-08-10 17:14:46 -0700197 const vector<uint16_t> &value,
mukesh agrawale7c7e652013-06-18 17:19:39 -0700198 Error *error) {
Ben Chan7fab8972014-08-10 17:14:46 -0700199 return SetProperty(name, value, error, &uint16s_properties_,
200 "a uint16_t list");
mukesh agrawale7c7e652013-06-18 17:19:39 -0700201}
202
mukesh agrawal66b0aca2012-01-30 15:28:28 -0800203bool PropertyStore::SetUint32Property(const string &name,
Ben Chan7fab8972014-08-10 17:14:46 -0700204 uint32_t value,
Chris Masoneb925cc82011-06-22 15:39:57 -0700205 Error *error) {
Ben Chan7fab8972014-08-10 17:14:46 -0700206 return SetProperty(name, value, error, &uint32_properties_, "a uint32_t");
Chris Masoneb925cc82011-06-22 15:39:57 -0700207}
208
Paul Stewarte18c33b2012-07-10 20:48:44 -0700209bool PropertyStore::SetUint64Property(const string &name,
Ben Chan7fab8972014-08-10 17:14:46 -0700210 uint64_t value,
Paul Stewarte18c33b2012-07-10 20:48:44 -0700211 Error *error) {
Ben Chan7fab8972014-08-10 17:14:46 -0700212 return SetProperty(name, value, error, &uint64_properties_, "a uint64_t");
Paul Stewarte18c33b2012-07-10 20:48:44 -0700213}
214
Jason Glasgowacdc11f2012-03-30 14:12:22 -0400215bool PropertyStore::SetRpcIdentifierProperty(const string &name,
216 const RpcIdentifier &value,
217 Error *error) {
Alex Vakulenko8a532292014-06-16 17:18:44 -0700218 return SetProperty(name, value, error, &rpc_identifier_properties_,
Jason Glasgowacdc11f2012-03-30 14:12:22 -0400219 "an rpc_identifier");
220}
221
mukesh agrawal4d260da2012-01-30 11:53:52 -0800222bool PropertyStore::ClearProperty(const string &name, Error *error) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700223 SLOG(Property, 2) << "Clearing " << name << ".";
mukesh agrawal4d260da2012-01-30 11:53:52 -0800224
225 if (ContainsKey(bool_properties_, name)) {
226 bool_properties_[name]->Clear(error);
227 } else if (ContainsKey(int16_properties_, name)) {
228 int16_properties_[name]->Clear(error);
229 } else if (ContainsKey(int32_properties_, name)) {
230 int32_properties_[name]->Clear(error);
231 } else if (ContainsKey(key_value_store_properties_, name)) {
232 key_value_store_properties_[name]->Clear(error);
233 } else if (ContainsKey(string_properties_, name)) {
234 string_properties_[name]->Clear(error);
235 } else if (ContainsKey(stringmap_properties_, name)) {
236 stringmap_properties_[name]->Clear(error);
237 } else if (ContainsKey(stringmaps_properties_, name)) {
238 stringmaps_properties_[name]->Clear(error);
239 } else if (ContainsKey(strings_properties_, name)) {
240 strings_properties_[name]->Clear(error);
241 } else if (ContainsKey(uint8_properties_, name)) {
242 uint8_properties_[name]->Clear(error);
243 } else if (ContainsKey(uint16_properties_, name)) {
244 uint16_properties_[name]->Clear(error);
mukesh agrawale7c7e652013-06-18 17:19:39 -0700245 } else if (ContainsKey(uint16s_properties_, name)) {
246 uint16s_properties_[name]->Clear(error);
mukesh agrawal4d260da2012-01-30 11:53:52 -0800247 } else if (ContainsKey(uint32_properties_, name)) {
248 uint32_properties_[name]->Clear(error);
Paul Stewarte18c33b2012-07-10 20:48:44 -0700249 } else if (ContainsKey(uint64_properties_, name)) {
250 uint64_properties_[name]->Clear(error);
Jason Glasgowacdc11f2012-03-30 14:12:22 -0400251 } else if (ContainsKey(rpc_identifier_properties_, name)) {
252 rpc_identifier_properties_[name]->Clear(error);
253 } else if (ContainsKey(rpc_identifiers_properties_, name)) {
254 rpc_identifiers_properties_[name]->Clear(error);
mukesh agrawal4d260da2012-01-30 11:53:52 -0800255 } else {
256 error->Populate(
257 Error::kInvalidProperty, "Property " + name + " does not exist.");
258 }
mukesh agrawalbebf1b82013-04-23 15:06:33 -0700259 if (error->IsSuccess()) {
260 if (!property_changed_callback_.is_null()) {
261 property_changed_callback_.Run(name);
262 }
263 }
mukesh agrawal4d260da2012-01-30 11:53:52 -0800264 return error->IsSuccess();
265}
266
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800267ReadablePropertyConstIterator<bool> PropertyStore::GetBoolPropertiesIter()
mukesh agrawalde29fa82011-09-16 16:16:36 -0700268 const {
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800269 return ReadablePropertyConstIterator<bool>(bool_properties_);
Chris Masonea8a2c252011-06-27 22:16:30 -0700270}
271
Ben Chan7fab8972014-08-10 17:14:46 -0700272ReadablePropertyConstIterator<int16_t> PropertyStore::GetInt16PropertiesIter()
mukesh agrawalde29fa82011-09-16 16:16:36 -0700273 const {
Ben Chan7fab8972014-08-10 17:14:46 -0700274 return ReadablePropertyConstIterator<int16_t>(int16_properties_);
Chris Masonea8a2c252011-06-27 22:16:30 -0700275}
276
Ben Chan7fab8972014-08-10 17:14:46 -0700277ReadablePropertyConstIterator<int32_t> PropertyStore::GetInt32PropertiesIter()
mukesh agrawalde29fa82011-09-16 16:16:36 -0700278 const {
Ben Chan7fab8972014-08-10 17:14:46 -0700279 return ReadablePropertyConstIterator<int32_t>(int32_properties_);
Chris Masone889666b2011-07-03 12:58:50 -0700280}
281
Darin Petkov63138a92012-02-06 14:09:15 +0100282ReadablePropertyConstIterator<KeyValueStore>
283PropertyStore::GetKeyValueStorePropertiesIter() const {
284 return
285 ReadablePropertyConstIterator<KeyValueStore>(key_value_store_properties_);
286}
287
Jason Glasgowacdc11f2012-03-30 14:12:22 -0400288ReadablePropertyConstIterator<RpcIdentifier>
289PropertyStore::GetRpcIdentifierPropertiesIter() const {
290 return ReadablePropertyConstIterator<RpcIdentifier>(
291 rpc_identifier_properties_);
292}
293
mukesh agrawal2366eed2012-03-20 18:21:50 -0700294ReadablePropertyConstIterator<RpcIdentifiers>
295PropertyStore::GetRpcIdentifiersPropertiesIter() const {
296 return ReadablePropertyConstIterator<RpcIdentifiers>(
297 rpc_identifiers_properties_);
298}
299
mukesh agrawal66b0aca2012-01-30 15:28:28 -0800300ReadablePropertyConstIterator<string>
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800301PropertyStore::GetStringPropertiesIter() const {
mukesh agrawal66b0aca2012-01-30 15:28:28 -0800302 return ReadablePropertyConstIterator<string>(string_properties_);
Chris Masonea8a2c252011-06-27 22:16:30 -0700303}
304
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800305ReadablePropertyConstIterator<Stringmap>
306PropertyStore::GetStringmapPropertiesIter() const {
307 return ReadablePropertyConstIterator<Stringmap>(stringmap_properties_);
Chris Masonea8a2c252011-06-27 22:16:30 -0700308}
309
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800310ReadablePropertyConstIterator<Stringmaps>
311PropertyStore::GetStringmapsPropertiesIter()
312 const {
313 return ReadablePropertyConstIterator<Stringmaps>(stringmaps_properties_);
314}
315
316ReadablePropertyConstIterator<Strings> PropertyStore::GetStringsPropertiesIter()
317 const {
318 return ReadablePropertyConstIterator<Strings>(strings_properties_);
319}
320
Ben Chan7fab8972014-08-10 17:14:46 -0700321ReadablePropertyConstIterator<uint8_t> PropertyStore::GetUint8PropertiesIter()
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800322 const {
Ben Chan7fab8972014-08-10 17:14:46 -0700323 return ReadablePropertyConstIterator<uint8_t>(uint8_properties_);
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800324}
325
Ben Chan7fab8972014-08-10 17:14:46 -0700326ReadablePropertyConstIterator<uint16_t> PropertyStore::GetUint16PropertiesIter()
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800327 const {
Ben Chan7fab8972014-08-10 17:14:46 -0700328 return ReadablePropertyConstIterator<uint16_t>(uint16_properties_);
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800329}
330
mukesh agrawale7c7e652013-06-18 17:19:39 -0700331ReadablePropertyConstIterator<Uint16s> PropertyStore::GetUint16sPropertiesIter()
332 const {
333 return ReadablePropertyConstIterator<Uint16s>(uint16s_properties_);
334}
335
Ben Chan7fab8972014-08-10 17:14:46 -0700336ReadablePropertyConstIterator<uint32_t> PropertyStore::GetUint32PropertiesIter()
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800337 const {
Ben Chan7fab8972014-08-10 17:14:46 -0700338 return ReadablePropertyConstIterator<uint32_t>(uint32_properties_);
Chris Masonea8a2c252011-06-27 22:16:30 -0700339}
340
Ben Chan7fab8972014-08-10 17:14:46 -0700341ReadablePropertyConstIterator<uint64_t> PropertyStore::GetUint64PropertiesIter()
Paul Stewarte18c33b2012-07-10 20:48:44 -0700342 const {
Ben Chan7fab8972014-08-10 17:14:46 -0700343 return ReadablePropertyConstIterator<uint64_t>(uint64_properties_);
Paul Stewarte18c33b2012-07-10 20:48:44 -0700344}
345
Chris Masoneb925cc82011-06-22 15:39:57 -0700346void PropertyStore::RegisterBool(const string &name, bool *prop) {
mukesh agrawal4d260da2012-01-30 11:53:52 -0800347 DCHECK(!Contains(name) || ContainsKey(bool_properties_, name))
348 << "(Already registered " << name << ")";
Chris Masoneb925cc82011-06-22 15:39:57 -0700349 bool_properties_[name] = BoolAccessor(new PropertyAccessor<bool>(prop));
350}
351
352void PropertyStore::RegisterConstBool(const string &name, const bool *prop) {
mukesh agrawal4d260da2012-01-30 11:53:52 -0800353 DCHECK(!Contains(name) || ContainsKey(bool_properties_, name))
354 << "(Already registered " << name << ")";
Chris Masoneb925cc82011-06-22 15:39:57 -0700355 bool_properties_[name] = BoolAccessor(new ConstPropertyAccessor<bool>(prop));
356}
357
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800358void PropertyStore::RegisterWriteOnlyBool(const string &name, bool *prop) {
mukesh agrawal4d260da2012-01-30 11:53:52 -0800359 DCHECK(!Contains(name) || ContainsKey(bool_properties_, name))
360 << "(Already registered " << name << ")";
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800361 bool_properties_[name] = BoolAccessor(
362 new WriteOnlyPropertyAccessor<bool>(prop));
363}
364
Ben Chan7fab8972014-08-10 17:14:46 -0700365void PropertyStore::RegisterInt16(const string &name, int16_t *prop) {
mukesh agrawal4d260da2012-01-30 11:53:52 -0800366 DCHECK(!Contains(name) || ContainsKey(int16_properties_, name))
367 << "(Already registered " << name << ")";
Ben Chan7fab8972014-08-10 17:14:46 -0700368 int16_properties_[name] = Int16Accessor(new PropertyAccessor<int16_t>(prop));
Chris Masoneb925cc82011-06-22 15:39:57 -0700369}
370
Ben Chan7fab8972014-08-10 17:14:46 -0700371void PropertyStore::RegisterConstInt16(const string &name,
372 const int16_t *prop) {
mukesh agrawal4d260da2012-01-30 11:53:52 -0800373 DCHECK(!Contains(name) || ContainsKey(int16_properties_, name))
374 << "(Already registered " << name << ")";
Chris Masoneb925cc82011-06-22 15:39:57 -0700375 int16_properties_[name] =
Ben Chan7fab8972014-08-10 17:14:46 -0700376 Int16Accessor(new ConstPropertyAccessor<int16_t>(prop));
Chris Masoneb925cc82011-06-22 15:39:57 -0700377}
378
Ben Chan7fab8972014-08-10 17:14:46 -0700379void PropertyStore::RegisterWriteOnlyInt16(const string &name, int16_t *prop) {
mukesh agrawal4d260da2012-01-30 11:53:52 -0800380 DCHECK(!Contains(name) || ContainsKey(int16_properties_, name))
381 << "(Already registered " << name << ")";
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800382 int16_properties_[name] =
Ben Chan7fab8972014-08-10 17:14:46 -0700383 Int16Accessor(new WriteOnlyPropertyAccessor<int16_t>(prop));
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800384}
Ben Chan7fab8972014-08-10 17:14:46 -0700385void PropertyStore::RegisterInt32(const string &name, int32_t *prop) {
mukesh agrawal4d260da2012-01-30 11:53:52 -0800386 DCHECK(!Contains(name) || ContainsKey(int32_properties_, name))
387 << "(Already registered " << name << ")";
Ben Chan7fab8972014-08-10 17:14:46 -0700388 int32_properties_[name] = Int32Accessor(new PropertyAccessor<int32_t>(prop));
Chris Masoneb925cc82011-06-22 15:39:57 -0700389}
390
Ben Chan7fab8972014-08-10 17:14:46 -0700391void PropertyStore::RegisterConstInt32(const string &name,
392 const int32_t *prop) {
mukesh agrawal4d260da2012-01-30 11:53:52 -0800393 DCHECK(!Contains(name) || ContainsKey(int32_properties_, name))
394 << "(Already registered " << name << ")";
Chris Masoneb925cc82011-06-22 15:39:57 -0700395 int32_properties_[name] =
Ben Chan7fab8972014-08-10 17:14:46 -0700396 Int32Accessor(new ConstPropertyAccessor<int32_t>(prop));
Chris Masoneb925cc82011-06-22 15:39:57 -0700397}
398
Ben Chan7fab8972014-08-10 17:14:46 -0700399void PropertyStore::RegisterWriteOnlyInt32(const string &name, int32_t *prop) {
mukesh agrawal4d260da2012-01-30 11:53:52 -0800400 DCHECK(!Contains(name) || ContainsKey(int32_properties_, name))
401 << "(Already registered " << name << ")";
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800402 int32_properties_[name] =
Ben Chan7fab8972014-08-10 17:14:46 -0700403 Int32Accessor(new WriteOnlyPropertyAccessor<int32_t>(prop));
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800404}
405
Chris Masoneb925cc82011-06-22 15:39:57 -0700406void PropertyStore::RegisterString(const string &name, string *prop) {
mukesh agrawal4d260da2012-01-30 11:53:52 -0800407 DCHECK(!Contains(name) || ContainsKey(string_properties_, name))
408 << "(Already registered " << name << ")";
Chris Masoneb925cc82011-06-22 15:39:57 -0700409 string_properties_[name] = StringAccessor(new PropertyAccessor<string>(prop));
410}
411
412void PropertyStore::RegisterConstString(const string &name,
413 const string *prop) {
mukesh agrawal4d260da2012-01-30 11:53:52 -0800414 DCHECK(!Contains(name) || ContainsKey(string_properties_, name))
415 << "(Already registered " << name << ")";
Chris Masoneb925cc82011-06-22 15:39:57 -0700416 string_properties_[name] =
417 StringAccessor(new ConstPropertyAccessor<string>(prop));
418}
419
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800420void PropertyStore::RegisterWriteOnlyString(const string &name, string *prop) {
mukesh agrawal4d260da2012-01-30 11:53:52 -0800421 DCHECK(!Contains(name) || ContainsKey(string_properties_, name))
422 << "(Already registered " << name << ")";
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800423 string_properties_[name] =
424 StringAccessor(new WriteOnlyPropertyAccessor<string>(prop));
425}
426
Chris Masone43b48a12011-07-01 13:37:07 -0700427void PropertyStore::RegisterStringmap(const string &name, Stringmap *prop) {
mukesh agrawal4d260da2012-01-30 11:53:52 -0800428 DCHECK(!Contains(name) || ContainsKey(stringmap_properties_, name))
429 << "(Already registered " << name << ")";
Chris Masoneb925cc82011-06-22 15:39:57 -0700430 stringmap_properties_[name] =
Chris Masone43b48a12011-07-01 13:37:07 -0700431 StringmapAccessor(new PropertyAccessor<Stringmap>(prop));
Chris Masoneb925cc82011-06-22 15:39:57 -0700432}
433
434void PropertyStore::RegisterConstStringmap(const string &name,
Chris Masone43b48a12011-07-01 13:37:07 -0700435 const Stringmap *prop) {
mukesh agrawal4d260da2012-01-30 11:53:52 -0800436 DCHECK(!Contains(name) || ContainsKey(stringmap_properties_, name))
437 << "(Already registered " << name << ")";
Chris Masoneb925cc82011-06-22 15:39:57 -0700438 stringmap_properties_[name] =
Chris Masone43b48a12011-07-01 13:37:07 -0700439 StringmapAccessor(new ConstPropertyAccessor<Stringmap>(prop));
440}
441
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800442void PropertyStore::RegisterWriteOnlyStringmap(const string &name,
443 Stringmap *prop) {
mukesh agrawal4d260da2012-01-30 11:53:52 -0800444 DCHECK(!Contains(name) || ContainsKey(stringmap_properties_, name))
445 << "(Already registered " << name << ")";
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800446 stringmap_properties_[name] =
447 StringmapAccessor(new WriteOnlyPropertyAccessor<Stringmap>(prop));
448}
449
Darin Petkovc0865312011-09-16 15:31:20 -0700450void PropertyStore::RegisterStringmaps(const string &name, Stringmaps *prop) {
mukesh agrawal4d260da2012-01-30 11:53:52 -0800451 DCHECK(!Contains(name) || ContainsKey(stringmaps_properties_, name))
452 << "(Already registered " << name << ")";
Darin Petkovc0865312011-09-16 15:31:20 -0700453 stringmaps_properties_[name] =
454 StringmapsAccessor(new PropertyAccessor<Stringmaps>(prop));
455}
456
457void PropertyStore::RegisterConstStringmaps(const string &name,
458 const Stringmaps *prop) {
mukesh agrawal4d260da2012-01-30 11:53:52 -0800459 DCHECK(!Contains(name) || ContainsKey(stringmaps_properties_, name))
460 << "(Already registered " << name << ")";
Darin Petkovc0865312011-09-16 15:31:20 -0700461 stringmaps_properties_[name] =
462 StringmapsAccessor(new ConstPropertyAccessor<Stringmaps>(prop));
463}
464
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800465void PropertyStore::RegisterWriteOnlyStringmaps(const string &name,
466 Stringmaps *prop) {
mukesh agrawal4d260da2012-01-30 11:53:52 -0800467 DCHECK(!Contains(name) || ContainsKey(stringmaps_properties_, name))
468 << "(Already registered " << name << ")";
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800469 stringmaps_properties_[name] =
470 StringmapsAccessor(new WriteOnlyPropertyAccessor<Stringmaps>(prop));
471}
472
Chris Masone43b48a12011-07-01 13:37:07 -0700473void PropertyStore::RegisterStrings(const string &name, Strings *prop) {
mukesh agrawal4d260da2012-01-30 11:53:52 -0800474 DCHECK(!Contains(name) || ContainsKey(strings_properties_, name))
475 << "(Already registered " << name << ")";
Chris Masone43b48a12011-07-01 13:37:07 -0700476 strings_properties_[name] =
477 StringsAccessor(new PropertyAccessor<Strings>(prop));
Chris Masoneb925cc82011-06-22 15:39:57 -0700478}
479
Chris Masone889666b2011-07-03 12:58:50 -0700480void PropertyStore::RegisterConstStrings(const string &name,
481 const Strings *prop) {
mukesh agrawal4d260da2012-01-30 11:53:52 -0800482 DCHECK(!Contains(name) || ContainsKey(strings_properties_, name))
483 << "(Already registered " << name << ")";
Chris Masone889666b2011-07-03 12:58:50 -0700484 strings_properties_[name] =
485 StringsAccessor(new ConstPropertyAccessor<Strings>(prop));
486}
487
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800488void PropertyStore::RegisterWriteOnlyStrings(const string &name,
489 Strings *prop) {
mukesh agrawal4d260da2012-01-30 11:53:52 -0800490 DCHECK(!Contains(name) || ContainsKey(strings_properties_, name))
491 << "(Already registered " << name << ")";
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800492 strings_properties_[name] =
493 StringsAccessor(new WriteOnlyPropertyAccessor<Strings>(prop));
494}
495
Ben Chan7fab8972014-08-10 17:14:46 -0700496void PropertyStore::RegisterUint8(const string &name, uint8_t *prop) {
mukesh agrawal4d260da2012-01-30 11:53:52 -0800497 DCHECK(!Contains(name) || ContainsKey(uint8_properties_, name))
498 << "(Already registered " << name << ")";
Ben Chan7fab8972014-08-10 17:14:46 -0700499 uint8_properties_[name] = Uint8Accessor(new PropertyAccessor<uint8_t>(prop));
Chris Masone889666b2011-07-03 12:58:50 -0700500}
501
Ben Chan7fab8972014-08-10 17:14:46 -0700502void PropertyStore::RegisterConstUint8(const string &name,
503 const uint8_t *prop) {
mukesh agrawal4d260da2012-01-30 11:53:52 -0800504 DCHECK(!Contains(name) || ContainsKey(uint8_properties_, name))
505 << "(Already registered " << name << ")";
Chris Masoneb925cc82011-06-22 15:39:57 -0700506 uint8_properties_[name] =
Ben Chan7fab8972014-08-10 17:14:46 -0700507 Uint8Accessor(new ConstPropertyAccessor<uint8_t>(prop));
Chris Masoneb925cc82011-06-22 15:39:57 -0700508}
509
Ben Chan7fab8972014-08-10 17:14:46 -0700510void PropertyStore::RegisterWriteOnlyUint8(const string &name, uint8_t *prop) {
mukesh agrawal4d260da2012-01-30 11:53:52 -0800511 DCHECK(!Contains(name) || ContainsKey(uint8_properties_, name))
512 << "(Already registered " << name << ")";
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800513 uint8_properties_[name] =
Ben Chan7fab8972014-08-10 17:14:46 -0700514 Uint8Accessor(new WriteOnlyPropertyAccessor<uint8_t>(prop));
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800515}
516
Ben Chan7fab8972014-08-10 17:14:46 -0700517void PropertyStore::RegisterUint16(const string &name, uint16_t *prop) {
mukesh agrawal4d260da2012-01-30 11:53:52 -0800518 DCHECK(!Contains(name) || ContainsKey(uint16_properties_, name))
519 << "(Already registered " << name << ")";
Ben Chan7fab8972014-08-10 17:14:46 -0700520 uint16_properties_[name] =
521 Uint16Accessor(new PropertyAccessor<uint16_t>(prop));
Chris Masoneb925cc82011-06-22 15:39:57 -0700522}
523
mukesh agrawale7c7e652013-06-18 17:19:39 -0700524void PropertyStore::RegisterUint16s(const string &name, Uint16s *prop) {
525 DCHECK(!Contains(name) || ContainsKey(uint16s_properties_, name))
526 << "(Already registered " << name << ")";
527 uint16s_properties_[name] =
528 Uint16sAccessor(new PropertyAccessor<Uint16s>(prop));
529}
530
Ben Chan7fab8972014-08-10 17:14:46 -0700531void PropertyStore::RegisterUint32(const std::string &name, uint32_t *prop) {
mukesh agrawal4d260da2012-01-30 11:53:52 -0800532 DCHECK(!Contains(name) || ContainsKey(uint32_properties_, name))
533 << "(Already registered " << name << ")";
Ben Chan7fab8972014-08-10 17:14:46 -0700534 uint32_properties_[name] =
535 Uint32Accessor(new PropertyAccessor<uint32_t>(prop));
mukesh agrawal4d260da2012-01-30 11:53:52 -0800536}
537
Chris Masoneb925cc82011-06-22 15:39:57 -0700538void PropertyStore::RegisterConstUint16(const string &name,
Ben Chan7fab8972014-08-10 17:14:46 -0700539 const uint16_t *prop) {
mukesh agrawal4d260da2012-01-30 11:53:52 -0800540 DCHECK(!Contains(name) || ContainsKey(uint16_properties_, name))
541 << "(Already registered " << name << ")";
Chris Masoneb925cc82011-06-22 15:39:57 -0700542 uint16_properties_[name] =
Ben Chan7fab8972014-08-10 17:14:46 -0700543 Uint16Accessor(new ConstPropertyAccessor<uint16_t>(prop));
Chris Masoneb925cc82011-06-22 15:39:57 -0700544}
545
mukesh agrawale7c7e652013-06-18 17:19:39 -0700546void PropertyStore::RegisterConstUint16s(const string &name,
547 const Uint16s *prop) {
548 DCHECK(!Contains(name) || ContainsKey(uint16s_properties_, name))
549 << "(Already registered " << name << ")";
550 uint16s_properties_[name] =
551 Uint16sAccessor(new ConstPropertyAccessor<Uint16s>(prop));
552}
553
Ben Chan7fab8972014-08-10 17:14:46 -0700554void PropertyStore::RegisterWriteOnlyUint16(const string &name,
555 uint16_t *prop) {
mukesh agrawal4d260da2012-01-30 11:53:52 -0800556 DCHECK(!Contains(name) || ContainsKey(uint16_properties_, name))
557 << "(Already registered " << name << ")";
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800558 uint16_properties_[name] =
Ben Chan7fab8972014-08-10 17:14:46 -0700559 Uint16Accessor(new WriteOnlyPropertyAccessor<uint16_t>(prop));
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800560}
561
mukesh agrawal66b0aca2012-01-30 15:28:28 -0800562void PropertyStore::RegisterDerivedBool(const string &name,
Chris Masone27c4aa52011-07-02 13:10:14 -0700563 const BoolAccessor &accessor) {
mukesh agrawal4d260da2012-01-30 11:53:52 -0800564 DCHECK(!Contains(name) || ContainsKey(bool_properties_, name))
565 << "(Already registered " << name << ")";
Chris Masone27c4aa52011-07-02 13:10:14 -0700566 bool_properties_[name] = accessor;
567}
568
mukesh agrawal66b0aca2012-01-30 15:28:28 -0800569void PropertyStore::RegisterDerivedInt32(const string &name,
mukesh agrawal4d0401c2012-01-06 16:05:31 -0800570 const Int32Accessor &accessor) {
mukesh agrawal4d260da2012-01-30 11:53:52 -0800571 DCHECK(!Contains(name) || ContainsKey(int32_properties_, name))
572 << "(Already registered " << name << ")";
mukesh agrawal4d0401c2012-01-06 16:05:31 -0800573 int32_properties_[name] = accessor;
574}
575
Darin Petkov63138a92012-02-06 14:09:15 +0100576void PropertyStore::RegisterDerivedKeyValueStore(
mukesh agrawal66b0aca2012-01-30 15:28:28 -0800577 const string &name,
Darin Petkov63138a92012-02-06 14:09:15 +0100578 const KeyValueStoreAccessor &acc) {
mukesh agrawal4d260da2012-01-30 11:53:52 -0800579 DCHECK(!Contains(name) || ContainsKey(key_value_store_properties_, name))
580 << "(Already registered " << name << ")";
Darin Petkov63138a92012-02-06 14:09:15 +0100581 key_value_store_properties_[name] = acc;
582}
583
Jason Glasgowacdc11f2012-03-30 14:12:22 -0400584void PropertyStore::RegisterDerivedRpcIdentifier(
585 const string &name,
586 const RpcIdentifierAccessor &acc) {
587 DCHECK(!Contains(name) || ContainsKey(rpc_identifier_properties_, name))
588 << "(Already registered " << name << ")";
589 rpc_identifier_properties_[name] = acc;
590}
591
mukesh agrawal2366eed2012-03-20 18:21:50 -0700592void PropertyStore::RegisterDerivedRpcIdentifiers(
593 const string &name,
594 const RpcIdentifiersAccessor &accessor) {
595 DCHECK(!Contains(name) || ContainsKey(rpc_identifiers_properties_, name))
596 << "(Already registered " << name << ")";
597 rpc_identifiers_properties_[name] = accessor;
598}
599
mukesh agrawal66b0aca2012-01-30 15:28:28 -0800600void PropertyStore::RegisterDerivedString(const string &name,
Chris Masone27c4aa52011-07-02 13:10:14 -0700601 const StringAccessor &accessor) {
mukesh agrawal4d260da2012-01-30 11:53:52 -0800602 DCHECK(!Contains(name) || ContainsKey(string_properties_, name))
603 << "(Already registered " << name << ")";
Chris Masone27c4aa52011-07-02 13:10:14 -0700604 string_properties_[name] = accessor;
605}
606
mukesh agrawal66b0aca2012-01-30 15:28:28 -0800607void PropertyStore::RegisterDerivedStrings(const string &name,
Chris Masone27c4aa52011-07-02 13:10:14 -0700608 const StringsAccessor &accessor) {
mukesh agrawal4d260da2012-01-30 11:53:52 -0800609 DCHECK(!Contains(name) || ContainsKey(strings_properties_, name))
610 << "(Already registered " << name << ")";
Chris Masone27c4aa52011-07-02 13:10:14 -0700611 strings_properties_[name] = accessor;
Chris Masoneb925cc82011-06-22 15:39:57 -0700612}
613
Eric Shienbrood30bc0ec2012-03-21 18:19:46 -0400614void PropertyStore::RegisterDerivedStringmap(const string &name,
615 const StringmapAccessor &acc) {
616 DCHECK(!Contains(name) || ContainsKey(stringmap_properties_, name))
617 << "(Already registered " << name << ")";
618 stringmap_properties_[name] = acc;
619}
620
mukesh agrawal66b0aca2012-01-30 15:28:28 -0800621void PropertyStore::RegisterDerivedStringmaps(const string &name,
Chris Masone889666b2011-07-03 12:58:50 -0700622 const StringmapsAccessor &acc) {
mukesh agrawal4d260da2012-01-30 11:53:52 -0800623 DCHECK(!Contains(name) || ContainsKey(stringmaps_properties_, name))
624 << "(Already registered " << name << ")";
Chris Masone889666b2011-07-03 12:58:50 -0700625 stringmaps_properties_[name] = acc;
626}
627
mukesh agrawal66b0aca2012-01-30 15:28:28 -0800628void PropertyStore::RegisterDerivedUint16(const string &name,
Paul Stewartbe5f5b32011-12-07 17:11:11 -0800629 const Uint16Accessor &acc) {
mukesh agrawal4d260da2012-01-30 11:53:52 -0800630 DCHECK(!Contains(name) || ContainsKey(uint16_properties_, name))
631 << "(Already registered " << name << ")";
Paul Stewartbe5f5b32011-12-07 17:11:11 -0800632 uint16_properties_[name] = acc;
633}
634
Paul Stewarte18c33b2012-07-10 20:48:44 -0700635void PropertyStore::RegisterDerivedUint64(const string &name,
636 const Uint64Accessor &acc) {
637 DCHECK(!Contains(name) || ContainsKey(uint64_properties_, name))
638 << "(Already registered " << name << ")";
639 uint64_properties_[name] = acc;
640}
641
mukesh agrawal66b0aca2012-01-30 15:28:28 -0800642// private methods
643
mukesh agrawalffa3d042011-10-06 15:26:10 -0700644template <class V>
Paul Stewarte6e8e492013-01-17 11:00:50 -0800645bool PropertyStore::GetProperty(
646 const string &name,
647 V *value,
648 Error *error,
Alex Vakulenko8a532292014-06-16 17:18:44 -0700649 const map< string, std::shared_ptr<
Paul Stewarte6e8e492013-01-17 11:00:50 -0800650 AccessorInterface<V> > >&collection,
651 const string &value_type_english) const {
652 SLOG(Property, 2) << "Getting " << name << " as " << value_type_english
653 << ".";
Alex Vakulenko8a532292014-06-16 17:18:44 -0700654 typename map< string, std::shared_ptr<
Paul Stewarte6e8e492013-01-17 11:00:50 -0800655 AccessorInterface<V> > >::const_iterator it = collection.find(name);
656 if (it != collection.end()) {
657 V val = it->second->Get(error);
658 if (error->IsSuccess()) {
659 *value = val;
660 }
661 } else {
662 if (Contains(name)) {
663 error->Populate(
664 Error::kInvalidArguments,
665 "Property " + name + " is not " + value_type_english + ".");
666 } else {
667 error->Populate(
668 Error::kInvalidProperty, "Property " + name + " does not exist.");
669 }
670 }
671 return error->IsSuccess();
Alex Vakulenko8a532292014-06-16 17:18:44 -0700672}
Paul Stewarte6e8e492013-01-17 11:00:50 -0800673
674template <class V>
mukesh agrawalffa3d042011-10-06 15:26:10 -0700675bool PropertyStore::SetProperty(
676 const string &name,
677 const V &value,
678 Error *error,
Alex Vakulenko8a532292014-06-16 17:18:44 -0700679 map< string, std::shared_ptr< AccessorInterface<V> > >* collection,
mukesh agrawalffa3d042011-10-06 15:26:10 -0700680 const string &value_type_english) {
mukesh agrawalbebf1b82013-04-23 15:06:33 -0700681 bool ret = false;
Ben Chanfad4a0b2012-04-18 15:49:59 -0700682 SLOG(Property, 2) << "Setting " << name << " as " << value_type_english
683 << ".";
Alex Vakulenko8a532292014-06-16 17:18:44 -0700684 if (ContainsKey(*collection, name)) {
685 ret = (*collection)[name]->Set(value, error);
mukesh agrawalbebf1b82013-04-23 15:06:33 -0700686 if (ret) {
687 if (!property_changed_callback_.is_null()) {
688 property_changed_callback_.Run(name);
689 }
690 }
mukesh agrawalffa3d042011-10-06 15:26:10 -0700691 } else {
692 if (Contains(name)) {
693 error->Populate(
694 Error::kInvalidArguments,
695 "Property " + name + " is not " + value_type_english + ".");
696 } else {
697 error->Populate(
698 Error::kInvalidProperty, "Property " + name + " does not exist.");
699 }
700 }
mukesh agrawalbebf1b82013-04-23 15:06:33 -0700701 return ret;
Alex Vakulenko8a532292014-06-16 17:18:44 -0700702}
mukesh agrawalffa3d042011-10-06 15:26:10 -0700703
Chris Masoneb925cc82011-06-22 15:39:57 -0700704} // namespace shill