blob: 9b8757b4c604f47c399f30147b4c37e06377f7ed [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>
12#include <base/logging.h>
Chris Masone27c4aa52011-07-02 13:10:14 -070013#include <base/stl_util-inl.h>
Chris Masoneb925cc82011-06-22 15:39:57 -070014
15#include "shill/error.h"
16#include "shill/property_accessor.h"
17
18using std::map;
19using std::string;
20using std::vector;
21
22namespace shill {
23
24PropertyStore::PropertyStore() {}
25
26PropertyStore::~PropertyStore() {}
27
mukesh agrawalde29fa82011-09-16 16:16:36 -070028bool PropertyStore::Contains(const std::string &prop) const {
Chris Masone889666b2011-07-03 12:58:50 -070029 return (bool_properties_.find(prop) != bool_properties_.end() ||
30 int16_properties_.find(prop) != int16_properties_.end() ||
31 int32_properties_.find(prop) != int32_properties_.end() ||
32 string_properties_.find(prop) != string_properties_.end() ||
33 stringmap_properties_.find(prop) != stringmap_properties_.end() ||
34 stringmaps_properties_.find(prop) != stringmaps_properties_.end() ||
35 strintpair_properties_.find(prop) != strintpair_properties_.end() ||
36 strings_properties_.find(prop) != strings_properties_.end() ||
37 uint8_properties_.find(prop) != uint8_properties_.end() ||
38 uint16_properties_.find(prop) != uint16_properties_.end() ||
39 uint32_properties_.find(prop) != uint32_properties_.end());
Chris Masoneb925cc82011-06-22 15:39:57 -070040}
41
42bool PropertyStore::SetBoolProperty(const std::string& name,
43 bool value,
44 Error *error) {
mukesh agrawalffa3d042011-10-06 15:26:10 -070045 return SetProperty(name, value, error, bool_properties_, "a bool");
Chris Masoneb925cc82011-06-22 15:39:57 -070046}
47
48bool PropertyStore::SetInt16Property(const std::string& name,
49 int16 value,
50 Error *error) {
mukesh agrawalffa3d042011-10-06 15:26:10 -070051 return SetProperty(name, value, error, int16_properties_, "an int16");
Chris Masoneb925cc82011-06-22 15:39:57 -070052}
53
54bool PropertyStore::SetInt32Property(const std::string& name,
55 int32 value,
56 Error *error) {
mukesh agrawalffa3d042011-10-06 15:26:10 -070057 return SetProperty(name, value, error, int32_properties_, "an int32.");
Chris Masoneb925cc82011-06-22 15:39:57 -070058}
59
60bool PropertyStore::SetStringProperty(const std::string& name,
61 const std::string& value,
62 Error *error) {
mukesh agrawalffa3d042011-10-06 15:26:10 -070063 return SetProperty(name, value, error, string_properties_, "a string");
Chris Masoneb925cc82011-06-22 15:39:57 -070064}
65
66bool PropertyStore::SetStringmapProperty(
67 const std::string& name,
68 const std::map<std::string, std::string>& values,
69 Error *error) {
mukesh agrawalffa3d042011-10-06 15:26:10 -070070 return SetProperty(name, values, error, stringmap_properties_,
71 "a string map");
Chris Masoneb925cc82011-06-22 15:39:57 -070072}
73
74bool PropertyStore::SetStringsProperty(const std::string& name,
75 const std::vector<std::string>& values,
76 Error *error) {
mukesh agrawalffa3d042011-10-06 15:26:10 -070077 return SetProperty(name, values, error, strings_properties_, "a string list");
Chris Masoneb925cc82011-06-22 15:39:57 -070078}
79
80bool PropertyStore::SetUint8Property(const std::string& name,
81 uint8 value,
82 Error *error) {
mukesh agrawalffa3d042011-10-06 15:26:10 -070083 return SetProperty(name, value, error, uint8_properties_, "a uint8");
Chris Masoneb925cc82011-06-22 15:39:57 -070084}
85
86bool PropertyStore::SetUint16Property(const std::string& name,
87 uint16 value,
88 Error *error) {
mukesh agrawalffa3d042011-10-06 15:26:10 -070089 return SetProperty(name, value, error, uint16_properties_, "a uint16");
Chris Masoneb925cc82011-06-22 15:39:57 -070090}
91
92bool PropertyStore::SetUint32Property(const std::string& name,
93 uint32 value,
94 Error *error) {
mukesh agrawalffa3d042011-10-06 15:26:10 -070095 return SetProperty(name, value, error, uint32_properties_, "a uint32");
Chris Masoneb925cc82011-06-22 15:39:57 -070096}
97
Gaurav Shah1b7a6162011-11-09 11:41:01 -080098ReadablePropertyConstIterator<bool> PropertyStore::GetBoolPropertiesIter()
mukesh agrawalde29fa82011-09-16 16:16:36 -070099 const {
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800100 return ReadablePropertyConstIterator<bool>(bool_properties_);
Chris Masonea8a2c252011-06-27 22:16:30 -0700101}
102
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800103ReadablePropertyConstIterator<int16> PropertyStore::GetInt16PropertiesIter()
mukesh agrawalde29fa82011-09-16 16:16:36 -0700104 const {
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800105 return ReadablePropertyConstIterator<int16>(int16_properties_);
Chris Masonea8a2c252011-06-27 22:16:30 -0700106}
107
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800108ReadablePropertyConstIterator<int32> PropertyStore::GetInt32PropertiesIter()
mukesh agrawalde29fa82011-09-16 16:16:36 -0700109 const {
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800110 return ReadablePropertyConstIterator<int32>(int32_properties_);
Chris Masone889666b2011-07-03 12:58:50 -0700111}
112
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800113ReadablePropertyConstIterator<std::string>
114PropertyStore::GetStringPropertiesIter() const {
115 return ReadablePropertyConstIterator<std::string>(string_properties_);
Chris Masonea8a2c252011-06-27 22:16:30 -0700116}
117
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800118ReadablePropertyConstIterator<Stringmap>
119PropertyStore::GetStringmapPropertiesIter() const {
120 return ReadablePropertyConstIterator<Stringmap>(stringmap_properties_);
Chris Masonea8a2c252011-06-27 22:16:30 -0700121}
122
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800123ReadablePropertyConstIterator<Stringmaps>
124PropertyStore::GetStringmapsPropertiesIter()
125 const {
126 return ReadablePropertyConstIterator<Stringmaps>(stringmaps_properties_);
127}
128
129ReadablePropertyConstIterator<Strings> PropertyStore::GetStringsPropertiesIter()
130 const {
131 return ReadablePropertyConstIterator<Strings>(strings_properties_);
132}
133
134ReadablePropertyConstIterator<StrIntPair>
135PropertyStore::GetStrIntPairPropertiesIter() const {
136 return ReadablePropertyConstIterator<StrIntPair>(strintpair_properties_);
137}
138
139ReadablePropertyConstIterator<uint8> PropertyStore::GetUint8PropertiesIter()
140 const {
141 return ReadablePropertyConstIterator<uint8>(uint8_properties_);
142}
143
144ReadablePropertyConstIterator<uint16> PropertyStore::GetUint16PropertiesIter()
145 const {
146 return ReadablePropertyConstIterator<uint16>(uint16_properties_);
147}
148
149ReadablePropertyConstIterator<uint32> PropertyStore::GetUint32PropertiesIter()
150 const {
151 return ReadablePropertyConstIterator<uint32>(uint32_properties_);
Chris Masonea8a2c252011-06-27 22:16:30 -0700152}
153
Chris Masoneb925cc82011-06-22 15:39:57 -0700154void PropertyStore::RegisterBool(const string &name, bool *prop) {
155 bool_properties_[name] = BoolAccessor(new PropertyAccessor<bool>(prop));
156}
157
158void PropertyStore::RegisterConstBool(const string &name, const bool *prop) {
159 bool_properties_[name] = BoolAccessor(new ConstPropertyAccessor<bool>(prop));
160}
161
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800162void PropertyStore::RegisterWriteOnlyBool(const string &name, bool *prop) {
163 bool_properties_[name] = BoolAccessor(
164 new WriteOnlyPropertyAccessor<bool>(prop));
165}
166
Chris Masoneb925cc82011-06-22 15:39:57 -0700167void PropertyStore::RegisterInt16(const string &name, int16 *prop) {
168 int16_properties_[name] = Int16Accessor(new PropertyAccessor<int16>(prop));
169}
170
171void PropertyStore::RegisterConstInt16(const string &name, const int16 *prop) {
172 int16_properties_[name] =
173 Int16Accessor(new ConstPropertyAccessor<int16>(prop));
174}
175
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800176void PropertyStore::RegisterWriteOnlyInt16(const string &name, int16 *prop) {
177 int16_properties_[name] =
178 Int16Accessor(new WriteOnlyPropertyAccessor<int16>(prop));
179}
Chris Masoneb925cc82011-06-22 15:39:57 -0700180void PropertyStore::RegisterInt32(const string &name, int32 *prop) {
181 int32_properties_[name] = Int32Accessor(new PropertyAccessor<int32>(prop));
182}
183
184void PropertyStore::RegisterConstInt32(const string &name, const int32 *prop) {
185 int32_properties_[name] =
186 Int32Accessor(new ConstPropertyAccessor<int32>(prop));
187}
188
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800189void PropertyStore::RegisterWriteOnlyInt32(const string &name, int32 *prop) {
190 int32_properties_[name] =
191 Int32Accessor(new WriteOnlyPropertyAccessor<int32>(prop));
192}
193
Chris Masoneb925cc82011-06-22 15:39:57 -0700194void PropertyStore::RegisterString(const string &name, string *prop) {
195 string_properties_[name] = StringAccessor(new PropertyAccessor<string>(prop));
196}
197
198void PropertyStore::RegisterConstString(const string &name,
199 const string *prop) {
200 string_properties_[name] =
201 StringAccessor(new ConstPropertyAccessor<string>(prop));
202}
203
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800204void PropertyStore::RegisterWriteOnlyString(const string &name, string *prop) {
205 string_properties_[name] =
206 StringAccessor(new WriteOnlyPropertyAccessor<string>(prop));
207}
208
Chris Masone43b48a12011-07-01 13:37:07 -0700209void PropertyStore::RegisterStringmap(const string &name, Stringmap *prop) {
Chris Masoneb925cc82011-06-22 15:39:57 -0700210 stringmap_properties_[name] =
Chris Masone43b48a12011-07-01 13:37:07 -0700211 StringmapAccessor(new PropertyAccessor<Stringmap>(prop));
Chris Masoneb925cc82011-06-22 15:39:57 -0700212}
213
214void PropertyStore::RegisterConstStringmap(const string &name,
Chris Masone43b48a12011-07-01 13:37:07 -0700215 const Stringmap *prop) {
Chris Masoneb925cc82011-06-22 15:39:57 -0700216 stringmap_properties_[name] =
Chris Masone43b48a12011-07-01 13:37:07 -0700217 StringmapAccessor(new ConstPropertyAccessor<Stringmap>(prop));
218}
219
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800220void PropertyStore::RegisterWriteOnlyStringmap(const string &name,
221 Stringmap *prop) {
222 stringmap_properties_[name] =
223 StringmapAccessor(new WriteOnlyPropertyAccessor<Stringmap>(prop));
224}
225
Darin Petkovc0865312011-09-16 15:31:20 -0700226void PropertyStore::RegisterStringmaps(const string &name, Stringmaps *prop) {
227 stringmaps_properties_[name] =
228 StringmapsAccessor(new PropertyAccessor<Stringmaps>(prop));
229}
230
231void PropertyStore::RegisterConstStringmaps(const string &name,
232 const Stringmaps *prop) {
233 stringmaps_properties_[name] =
234 StringmapsAccessor(new ConstPropertyAccessor<Stringmaps>(prop));
235}
236
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800237void PropertyStore::RegisterWriteOnlyStringmaps(const string &name,
238 Stringmaps *prop) {
239 stringmaps_properties_[name] =
240 StringmapsAccessor(new WriteOnlyPropertyAccessor<Stringmaps>(prop));
241}
242
Chris Masone43b48a12011-07-01 13:37:07 -0700243void PropertyStore::RegisterStrings(const string &name, Strings *prop) {
244 strings_properties_[name] =
245 StringsAccessor(new PropertyAccessor<Strings>(prop));
Chris Masoneb925cc82011-06-22 15:39:57 -0700246}
247
Chris Masone889666b2011-07-03 12:58:50 -0700248void PropertyStore::RegisterConstStrings(const string &name,
249 const Strings *prop) {
250 strings_properties_[name] =
251 StringsAccessor(new ConstPropertyAccessor<Strings>(prop));
252}
253
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800254void PropertyStore::RegisterWriteOnlyStrings(const string &name,
255 Strings *prop) {
256 strings_properties_[name] =
257 StringsAccessor(new WriteOnlyPropertyAccessor<Strings>(prop));
258}
259
Chris Masone889666b2011-07-03 12:58:50 -0700260void PropertyStore::RegisterUint8(const string &name, uint8 *prop) {
261 uint8_properties_[name] = Uint8Accessor(new PropertyAccessor<uint8>(prop));
262}
263
Chris Masoneb925cc82011-06-22 15:39:57 -0700264void PropertyStore::RegisterConstUint8(const string &name, const uint8 *prop) {
265 uint8_properties_[name] =
266 Uint8Accessor(new ConstPropertyAccessor<uint8>(prop));
267}
268
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800269void PropertyStore::RegisterWriteOnlyUint8(const string &name, uint8 *prop) {
270 uint8_properties_[name] =
271 Uint8Accessor(new WriteOnlyPropertyAccessor<uint8>(prop));
272}
273
Chris Masoneb925cc82011-06-22 15:39:57 -0700274void PropertyStore::RegisterUint16(const std::string &name, uint16 *prop) {
275 uint16_properties_[name] = Uint16Accessor(new PropertyAccessor<uint16>(prop));
276}
277
278void PropertyStore::RegisterConstUint16(const string &name,
279 const uint16 *prop) {
280 uint16_properties_[name] =
281 Uint16Accessor(new ConstPropertyAccessor<uint16>(prop));
282}
283
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800284void PropertyStore::RegisterWriteOnlyUint16(const string &name, uint16 *prop) {
285 uint16_properties_[name] =
286 Uint16Accessor(new WriteOnlyPropertyAccessor<uint16>(prop));
287}
288
Chris Masone27c4aa52011-07-02 13:10:14 -0700289void PropertyStore::RegisterDerivedBool(const std::string &name,
290 const BoolAccessor &accessor) {
291 bool_properties_[name] = accessor;
292}
293
mukesh agrawal4d0401c2012-01-06 16:05:31 -0800294void PropertyStore::RegisterDerivedInt32(const std::string &name,
295 const Int32Accessor &accessor) {
296 int32_properties_[name] = accessor;
297}
298
Chris Masone27c4aa52011-07-02 13:10:14 -0700299void PropertyStore::RegisterDerivedString(const std::string &name,
300 const StringAccessor &accessor) {
301 string_properties_[name] = accessor;
302}
303
304void PropertyStore::RegisterDerivedStrings(const std::string &name,
305 const StringsAccessor &accessor) {
306 strings_properties_[name] = accessor;
Chris Masoneb925cc82011-06-22 15:39:57 -0700307}
308
Chris Masone889666b2011-07-03 12:58:50 -0700309void PropertyStore::RegisterDerivedStringmaps(const std::string &name,
310 const StringmapsAccessor &acc) {
311 stringmaps_properties_[name] = acc;
312}
313
314void PropertyStore::RegisterDerivedStrIntPair(const std::string &name,
315 const StrIntPairAccessor &acc) {
316 strintpair_properties_[name] = acc;
317}
318
Paul Stewartbe5f5b32011-12-07 17:11:11 -0800319void PropertyStore::RegisterDerivedUint16(const std::string &name,
320 const Uint16Accessor &acc) {
321 uint16_properties_[name] = acc;
322}
323
mukesh agrawalffa3d042011-10-06 15:26:10 -0700324// private
325template <class V>
326bool PropertyStore::SetProperty(
327 const string &name,
328 const V &value,
329 Error *error,
330 map< string, std::tr1::shared_ptr< AccessorInterface<V> > >&collection,
331 const string &value_type_english) {
332 VLOG(2) << "Setting " << name << " as " << value_type_english << ".";
333 if (ContainsKey(collection, name)) {
334 collection[name]->Set(value, error);
335 } else {
336 if (Contains(name)) {
337 error->Populate(
338 Error::kInvalidArguments,
339 "Property " + name + " is not " + value_type_english + ".");
340 } else {
341 error->Populate(
342 Error::kInvalidProperty, "Property " + name + " does not exist.");
343 }
344 }
345 return error->IsSuccess();
346};
347
Chris Masoneb925cc82011-06-22 15:39:57 -0700348} // namespace shill