blob: 9fb61ba4411d47668e5a90c1328aab29fb0bbd48 [file] [log] [blame]
Chris Masoneb925cc82011-06-22 15:39:57 -07001// Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
2// 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) {
Chris Masone27c4aa52011-07-02 13:10:14 -070045 VLOG(2) << "Setting " << name << " as a bool.";
46 bool set = (ContainsKey(bool_properties_, name) &&
47 bool_properties_[name]->Set(value));
48 if (!set && error)
49 error->Populate(Error::kInvalidArguments, name + " is not a R/W bool.");
50 return set;
Chris Masoneb925cc82011-06-22 15:39:57 -070051}
52
53bool PropertyStore::SetInt16Property(const std::string& name,
54 int16 value,
55 Error *error) {
Chris Masone27c4aa52011-07-02 13:10:14 -070056 VLOG(2) << "Setting " << name << " as an int16.";
57 bool set = (ContainsKey(int16_properties_, name) &&
58 int16_properties_[name]->Set(value));
59 if (!set && error)
60 error->Populate(Error::kInvalidArguments, name + " is not a R/W int16.");
61 return set;
Chris Masoneb925cc82011-06-22 15:39:57 -070062}
63
64bool PropertyStore::SetInt32Property(const std::string& name,
65 int32 value,
66 Error *error) {
Chris Masone27c4aa52011-07-02 13:10:14 -070067 VLOG(2) << "Setting " << name << " as an int32.";
68 bool set = (ContainsKey(int32_properties_, name) &&
69 int32_properties_[name]->Set(value));
70 if (!set && error)
71 error->Populate(Error::kInvalidArguments, name + " is not a R/W int32.");
72 return set;
Chris Masoneb925cc82011-06-22 15:39:57 -070073}
74
75bool PropertyStore::SetStringProperty(const std::string& name,
76 const std::string& value,
77 Error *error) {
Chris Masone27c4aa52011-07-02 13:10:14 -070078 VLOG(2) << "Setting " << name << " as a string.";
79 bool set = (ContainsKey(string_properties_, name) &&
80 string_properties_[name]->Set(value));
81 if (!set && error)
82 error->Populate(Error::kInvalidArguments, name + " is not a R/W string.");
83 return set;
Chris Masoneb925cc82011-06-22 15:39:57 -070084}
85
86bool PropertyStore::SetStringmapProperty(
87 const std::string& name,
88 const std::map<std::string, std::string>& values,
89 Error *error) {
Chris Masone27c4aa52011-07-02 13:10:14 -070090 VLOG(2) << "Setting " << name << " as a string map.";
91 bool set = (ContainsKey(stringmap_properties_, name) &&
92 stringmap_properties_[name]->Set(values));
93 if (!set && error)
94 error->Populate(Error::kInvalidArguments, name + " is not a R/W strmap.");
95 return set;
Chris Masoneb925cc82011-06-22 15:39:57 -070096}
97
98bool PropertyStore::SetStringsProperty(const std::string& name,
99 const std::vector<std::string>& values,
100 Error *error) {
Chris Masone27c4aa52011-07-02 13:10:14 -0700101 VLOG(2) << "Setting " << name << " as a string list.";
102 bool set = (ContainsKey(strings_properties_, name) &&
103 strings_properties_[name]->Set(values));
104 if (!set && error)
105 error->Populate(Error::kInvalidArguments, name + " is not a R/W str list.");
106 return set;
Chris Masoneb925cc82011-06-22 15:39:57 -0700107}
108
109bool PropertyStore::SetUint8Property(const std::string& name,
110 uint8 value,
111 Error *error) {
Chris Masone27c4aa52011-07-02 13:10:14 -0700112 VLOG(2) << "Setting " << name << " as a uint8.";
113 bool set = (ContainsKey(uint8_properties_, name) &&
114 uint8_properties_[name]->Set(value));
115 if (!set && error)
116 error->Populate(Error::kInvalidArguments, name + " is not a R/W uint8.");
117 return set;
Chris Masoneb925cc82011-06-22 15:39:57 -0700118}
119
120bool PropertyStore::SetUint16Property(const std::string& name,
121 uint16 value,
122 Error *error) {
Chris Masone27c4aa52011-07-02 13:10:14 -0700123 VLOG(2) << "Setting " << name << " as a uint16.";
124 bool set = (ContainsKey(uint16_properties_, name) &&
125 uint16_properties_[name]->Set(value));
126 if (!set && error)
127 error->Populate(Error::kInvalidArguments, name + " is not a R/W uint16.");
128 return set;
Chris Masoneb925cc82011-06-22 15:39:57 -0700129}
130
131bool PropertyStore::SetUint32Property(const std::string& name,
132 uint32 value,
133 Error *error) {
Chris Masone27c4aa52011-07-02 13:10:14 -0700134 VLOG(2) << "Setting " << name << " as a uint32.";
135 bool set = (ContainsKey(uint32_properties_, name) &&
136 uint32_properties_[name]->Set(value));
137 if (!set && error)
138 error->Populate(Error::kInvalidArguments, name + " is not a R/W uint32.");
139 return set;
Chris Masoneb925cc82011-06-22 15:39:57 -0700140}
141
mukesh agrawalde29fa82011-09-16 16:16:36 -0700142PropertyConstIterator<bool> PropertyStore::GetBoolPropertiesIter() const {
Chris Masonea8a2c252011-06-27 22:16:30 -0700143 return PropertyConstIterator<bool>(bool_properties_);
144}
145
mukesh agrawalde29fa82011-09-16 16:16:36 -0700146PropertyConstIterator<int16> PropertyStore::GetInt16PropertiesIter() const {
Chris Masonea8a2c252011-06-27 22:16:30 -0700147 return PropertyConstIterator<int16>(int16_properties_);
148}
149
mukesh agrawalde29fa82011-09-16 16:16:36 -0700150PropertyConstIterator<int32> PropertyStore::GetInt32PropertiesIter() const {
Chris Masonea8a2c252011-06-27 22:16:30 -0700151 return PropertyConstIterator<int32>(int32_properties_);
152}
153
mukesh agrawalde29fa82011-09-16 16:16:36 -0700154PropertyConstIterator<std::string> PropertyStore::GetStringPropertiesIter()
155 const {
Chris Masonea8a2c252011-06-27 22:16:30 -0700156 return PropertyConstIterator<std::string>(string_properties_);
157}
158
mukesh agrawalde29fa82011-09-16 16:16:36 -0700159PropertyConstIterator<Stringmap> PropertyStore::GetStringmapPropertiesIter()
160 const {
Chris Masonea8a2c252011-06-27 22:16:30 -0700161 return PropertyConstIterator<Stringmap>(stringmap_properties_);
162}
163
mukesh agrawalde29fa82011-09-16 16:16:36 -0700164PropertyConstIterator<Strings> PropertyStore::GetStringsPropertiesIter() const {
Chris Masonea8a2c252011-06-27 22:16:30 -0700165 return PropertyConstIterator<Strings>(strings_properties_);
166}
167
mukesh agrawalde29fa82011-09-16 16:16:36 -0700168PropertyConstIterator<StrIntPair> PropertyStore::GetStrIntPairPropertiesIter()
169 const {
Chris Masone889666b2011-07-03 12:58:50 -0700170 return PropertyConstIterator<StrIntPair>(strintpair_properties_);
171}
172
mukesh agrawalde29fa82011-09-16 16:16:36 -0700173PropertyConstIterator<uint8> PropertyStore::GetUint8PropertiesIter() const {
Chris Masonea8a2c252011-06-27 22:16:30 -0700174 return PropertyConstIterator<uint8>(uint8_properties_);
175}
176
mukesh agrawalde29fa82011-09-16 16:16:36 -0700177PropertyConstIterator<uint16> PropertyStore::GetUint16PropertiesIter() const {
Chris Masonea8a2c252011-06-27 22:16:30 -0700178 return PropertyConstIterator<uint16>(uint16_properties_);
179}
180
mukesh agrawalde29fa82011-09-16 16:16:36 -0700181PropertyConstIterator<uint32> PropertyStore::GetUint32PropertiesIter() const {
Chris Masonea8a2c252011-06-27 22:16:30 -0700182 return PropertyConstIterator<uint32>(uint32_properties_);
183}
184
Chris Masoneb925cc82011-06-22 15:39:57 -0700185void PropertyStore::RegisterBool(const string &name, bool *prop) {
186 bool_properties_[name] = BoolAccessor(new PropertyAccessor<bool>(prop));
187}
188
189void PropertyStore::RegisterConstBool(const string &name, const bool *prop) {
190 bool_properties_[name] = BoolAccessor(new ConstPropertyAccessor<bool>(prop));
191}
192
193void PropertyStore::RegisterInt16(const string &name, int16 *prop) {
194 int16_properties_[name] = Int16Accessor(new PropertyAccessor<int16>(prop));
195}
196
197void PropertyStore::RegisterConstInt16(const string &name, const int16 *prop) {
198 int16_properties_[name] =
199 Int16Accessor(new ConstPropertyAccessor<int16>(prop));
200}
201
202void PropertyStore::RegisterInt32(const string &name, int32 *prop) {
203 int32_properties_[name] = Int32Accessor(new PropertyAccessor<int32>(prop));
204}
205
206void PropertyStore::RegisterConstInt32(const string &name, const int32 *prop) {
207 int32_properties_[name] =
208 Int32Accessor(new ConstPropertyAccessor<int32>(prop));
209}
210
211void PropertyStore::RegisterString(const string &name, string *prop) {
212 string_properties_[name] = StringAccessor(new PropertyAccessor<string>(prop));
213}
214
215void PropertyStore::RegisterConstString(const string &name,
216 const string *prop) {
217 string_properties_[name] =
218 StringAccessor(new ConstPropertyAccessor<string>(prop));
219}
220
Chris Masone43b48a12011-07-01 13:37:07 -0700221void PropertyStore::RegisterStringmap(const string &name, Stringmap *prop) {
Chris Masoneb925cc82011-06-22 15:39:57 -0700222 stringmap_properties_[name] =
Chris Masone43b48a12011-07-01 13:37:07 -0700223 StringmapAccessor(new PropertyAccessor<Stringmap>(prop));
Chris Masoneb925cc82011-06-22 15:39:57 -0700224}
225
226void PropertyStore::RegisterConstStringmap(const string &name,
Chris Masone43b48a12011-07-01 13:37:07 -0700227 const Stringmap *prop) {
Chris Masoneb925cc82011-06-22 15:39:57 -0700228 stringmap_properties_[name] =
Chris Masone43b48a12011-07-01 13:37:07 -0700229 StringmapAccessor(new ConstPropertyAccessor<Stringmap>(prop));
230}
231
Darin Petkovc0865312011-09-16 15:31:20 -0700232void PropertyStore::RegisterStringmaps(const string &name, Stringmaps *prop) {
233 stringmaps_properties_[name] =
234 StringmapsAccessor(new PropertyAccessor<Stringmaps>(prop));
235}
236
237void PropertyStore::RegisterConstStringmaps(const string &name,
238 const Stringmaps *prop) {
239 stringmaps_properties_[name] =
240 StringmapsAccessor(new ConstPropertyAccessor<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
254void PropertyStore::RegisterUint8(const string &name, uint8 *prop) {
255 uint8_properties_[name] = Uint8Accessor(new PropertyAccessor<uint8>(prop));
256}
257
Chris Masoneb925cc82011-06-22 15:39:57 -0700258void PropertyStore::RegisterConstUint8(const string &name, const uint8 *prop) {
259 uint8_properties_[name] =
260 Uint8Accessor(new ConstPropertyAccessor<uint8>(prop));
261}
262
263void PropertyStore::RegisterUint16(const std::string &name, uint16 *prop) {
264 uint16_properties_[name] = Uint16Accessor(new PropertyAccessor<uint16>(prop));
265}
266
267void PropertyStore::RegisterConstUint16(const string &name,
268 const uint16 *prop) {
269 uint16_properties_[name] =
270 Uint16Accessor(new ConstPropertyAccessor<uint16>(prop));
271}
272
Chris Masone27c4aa52011-07-02 13:10:14 -0700273void PropertyStore::RegisterDerivedBool(const std::string &name,
274 const BoolAccessor &accessor) {
275 bool_properties_[name] = accessor;
276}
277
278void PropertyStore::RegisterDerivedString(const std::string &name,
279 const StringAccessor &accessor) {
280 string_properties_[name] = accessor;
281}
282
283void PropertyStore::RegisterDerivedStrings(const std::string &name,
284 const StringsAccessor &accessor) {
285 strings_properties_[name] = accessor;
Chris Masoneb925cc82011-06-22 15:39:57 -0700286}
287
Chris Masone889666b2011-07-03 12:58:50 -0700288void PropertyStore::RegisterDerivedStringmaps(const std::string &name,
289 const StringmapsAccessor &acc) {
290 stringmaps_properties_[name] = acc;
291}
292
293void PropertyStore::RegisterDerivedStrIntPair(const std::string &name,
294 const StrIntPairAccessor &acc) {
295 strintpair_properties_[name] = acc;
296}
297
Chris Masoneb925cc82011-06-22 15:39:57 -0700298} // namespace shill