blob: 4254f1a0b7753c4c343b34049255cc2738dec4aa [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
Chris Masone889666b2011-07-03 12:58:50 -070028bool PropertyStore::Contains(const std::string &prop) {
29 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
Chris Masonea8a2c252011-06-27 22:16:30 -0700142PropertyConstIterator<bool> PropertyStore::GetBoolPropertiesIter() {
143 return PropertyConstIterator<bool>(bool_properties_);
144}
145
146PropertyConstIterator<int16> PropertyStore::GetInt16PropertiesIter() {
147 return PropertyConstIterator<int16>(int16_properties_);
148}
149
150PropertyConstIterator<int32> PropertyStore::GetInt32PropertiesIter() {
151 return PropertyConstIterator<int32>(int32_properties_);
152}
153
154PropertyConstIterator<std::string> PropertyStore::GetStringPropertiesIter() {
155 return PropertyConstIterator<std::string>(string_properties_);
156}
157
158PropertyConstIterator<Stringmap> PropertyStore::GetStringmapPropertiesIter() {
159 return PropertyConstIterator<Stringmap>(stringmap_properties_);
160}
161
162PropertyConstIterator<Strings> PropertyStore::GetStringsPropertiesIter() {
163 return PropertyConstIterator<Strings>(strings_properties_);
164}
165
Chris Masone889666b2011-07-03 12:58:50 -0700166PropertyConstIterator<StrIntPair> PropertyStore::GetStrIntPairPropertiesIter() {
167 return PropertyConstIterator<StrIntPair>(strintpair_properties_);
168}
169
Chris Masonea8a2c252011-06-27 22:16:30 -0700170PropertyConstIterator<uint8> PropertyStore::GetUint8PropertiesIter() {
171 return PropertyConstIterator<uint8>(uint8_properties_);
172}
173
174PropertyConstIterator<uint16> PropertyStore::GetUint16PropertiesIter() {
175 return PropertyConstIterator<uint16>(uint16_properties_);
176}
177
178PropertyConstIterator<uint32> PropertyStore::GetUint32PropertiesIter() {
179 return PropertyConstIterator<uint32>(uint32_properties_);
180}
181
Chris Masoneb925cc82011-06-22 15:39:57 -0700182void PropertyStore::RegisterBool(const string &name, bool *prop) {
183 bool_properties_[name] = BoolAccessor(new PropertyAccessor<bool>(prop));
184}
185
186void PropertyStore::RegisterConstBool(const string &name, const bool *prop) {
187 bool_properties_[name] = BoolAccessor(new ConstPropertyAccessor<bool>(prop));
188}
189
190void PropertyStore::RegisterInt16(const string &name, int16 *prop) {
191 int16_properties_[name] = Int16Accessor(new PropertyAccessor<int16>(prop));
192}
193
194void PropertyStore::RegisterConstInt16(const string &name, const int16 *prop) {
195 int16_properties_[name] =
196 Int16Accessor(new ConstPropertyAccessor<int16>(prop));
197}
198
199void PropertyStore::RegisterInt32(const string &name, int32 *prop) {
200 int32_properties_[name] = Int32Accessor(new PropertyAccessor<int32>(prop));
201}
202
203void PropertyStore::RegisterConstInt32(const string &name, const int32 *prop) {
204 int32_properties_[name] =
205 Int32Accessor(new ConstPropertyAccessor<int32>(prop));
206}
207
208void PropertyStore::RegisterString(const string &name, string *prop) {
209 string_properties_[name] = StringAccessor(new PropertyAccessor<string>(prop));
210}
211
212void PropertyStore::RegisterConstString(const string &name,
213 const string *prop) {
214 string_properties_[name] =
215 StringAccessor(new ConstPropertyAccessor<string>(prop));
216}
217
Chris Masone43b48a12011-07-01 13:37:07 -0700218void PropertyStore::RegisterStringmap(const string &name, Stringmap *prop) {
Chris Masoneb925cc82011-06-22 15:39:57 -0700219 stringmap_properties_[name] =
Chris Masone43b48a12011-07-01 13:37:07 -0700220 StringmapAccessor(new PropertyAccessor<Stringmap>(prop));
Chris Masoneb925cc82011-06-22 15:39:57 -0700221}
222
223void PropertyStore::RegisterConstStringmap(const string &name,
Chris Masone43b48a12011-07-01 13:37:07 -0700224 const Stringmap *prop) {
Chris Masoneb925cc82011-06-22 15:39:57 -0700225 stringmap_properties_[name] =
Chris Masone43b48a12011-07-01 13:37:07 -0700226 StringmapAccessor(new ConstPropertyAccessor<Stringmap>(prop));
227}
228
229void PropertyStore::RegisterStrings(const string &name, Strings *prop) {
230 strings_properties_[name] =
231 StringsAccessor(new PropertyAccessor<Strings>(prop));
Chris Masoneb925cc82011-06-22 15:39:57 -0700232}
233
Chris Masone889666b2011-07-03 12:58:50 -0700234void PropertyStore::RegisterConstStrings(const string &name,
235 const Strings *prop) {
236 strings_properties_[name] =
237 StringsAccessor(new ConstPropertyAccessor<Strings>(prop));
238}
239
240void PropertyStore::RegisterUint8(const string &name, uint8 *prop) {
241 uint8_properties_[name] = Uint8Accessor(new PropertyAccessor<uint8>(prop));
242}
243
Chris Masoneb925cc82011-06-22 15:39:57 -0700244void PropertyStore::RegisterConstUint8(const string &name, const uint8 *prop) {
245 uint8_properties_[name] =
246 Uint8Accessor(new ConstPropertyAccessor<uint8>(prop));
247}
248
249void PropertyStore::RegisterUint16(const std::string &name, uint16 *prop) {
250 uint16_properties_[name] = Uint16Accessor(new PropertyAccessor<uint16>(prop));
251}
252
253void PropertyStore::RegisterConstUint16(const string &name,
254 const uint16 *prop) {
255 uint16_properties_[name] =
256 Uint16Accessor(new ConstPropertyAccessor<uint16>(prop));
257}
258
Chris Masone27c4aa52011-07-02 13:10:14 -0700259void PropertyStore::RegisterDerivedBool(const std::string &name,
260 const BoolAccessor &accessor) {
261 bool_properties_[name] = accessor;
262}
263
264void PropertyStore::RegisterDerivedString(const std::string &name,
265 const StringAccessor &accessor) {
266 string_properties_[name] = accessor;
267}
268
269void PropertyStore::RegisterDerivedStrings(const std::string &name,
270 const StringsAccessor &accessor) {
271 strings_properties_[name] = accessor;
Chris Masoneb925cc82011-06-22 15:39:57 -0700272}
273
Chris Masone889666b2011-07-03 12:58:50 -0700274void PropertyStore::RegisterDerivedStringmaps(const std::string &name,
275 const StringmapsAccessor &acc) {
276 stringmaps_properties_[name] = acc;
277}
278
279void PropertyStore::RegisterDerivedStrIntPair(const std::string &name,
280 const StrIntPairAccessor &acc) {
281 strintpair_properties_[name] = acc;
282}
283
Chris Masoneb925cc82011-06-22 15:39:57 -0700284} // namespace shill