Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 1 | // 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 Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 13 | #include <base/stl_util-inl.h> |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 14 | |
| 15 | #include "shill/error.h" |
| 16 | #include "shill/property_accessor.h" |
| 17 | |
| 18 | using std::map; |
| 19 | using std::string; |
| 20 | using std::vector; |
| 21 | |
| 22 | namespace shill { |
| 23 | |
| 24 | PropertyStore::PropertyStore() {} |
| 25 | |
| 26 | PropertyStore::~PropertyStore() {} |
| 27 | |
mukesh agrawal | de29fa8 | 2011-09-16 16:16:36 -0700 | [diff] [blame] | 28 | bool PropertyStore::Contains(const std::string &prop) const { |
Chris Masone | 889666b | 2011-07-03 12:58:50 -0700 | [diff] [blame] | 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 Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | bool PropertyStore::SetBoolProperty(const std::string& name, |
| 43 | bool value, |
| 44 | Error *error) { |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 45 | 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 Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | bool PropertyStore::SetInt16Property(const std::string& name, |
| 54 | int16 value, |
| 55 | Error *error) { |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 56 | 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 Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | bool PropertyStore::SetInt32Property(const std::string& name, |
| 65 | int32 value, |
| 66 | Error *error) { |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 67 | 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 Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | bool PropertyStore::SetStringProperty(const std::string& name, |
| 76 | const std::string& value, |
| 77 | Error *error) { |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 78 | 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 Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | bool PropertyStore::SetStringmapProperty( |
| 87 | const std::string& name, |
| 88 | const std::map<std::string, std::string>& values, |
| 89 | Error *error) { |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 90 | 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 Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | bool PropertyStore::SetStringsProperty(const std::string& name, |
| 99 | const std::vector<std::string>& values, |
| 100 | Error *error) { |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 101 | 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 Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | bool PropertyStore::SetUint8Property(const std::string& name, |
| 110 | uint8 value, |
| 111 | Error *error) { |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 112 | 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 Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | bool PropertyStore::SetUint16Property(const std::string& name, |
| 121 | uint16 value, |
| 122 | Error *error) { |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 123 | 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 Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | bool PropertyStore::SetUint32Property(const std::string& name, |
| 132 | uint32 value, |
| 133 | Error *error) { |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 134 | 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 Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 140 | } |
| 141 | |
mukesh agrawal | de29fa8 | 2011-09-16 16:16:36 -0700 | [diff] [blame] | 142 | PropertyConstIterator<bool> PropertyStore::GetBoolPropertiesIter() const { |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 143 | return PropertyConstIterator<bool>(bool_properties_); |
| 144 | } |
| 145 | |
mukesh agrawal | de29fa8 | 2011-09-16 16:16:36 -0700 | [diff] [blame] | 146 | PropertyConstIterator<int16> PropertyStore::GetInt16PropertiesIter() const { |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 147 | return PropertyConstIterator<int16>(int16_properties_); |
| 148 | } |
| 149 | |
mukesh agrawal | de29fa8 | 2011-09-16 16:16:36 -0700 | [diff] [blame] | 150 | PropertyConstIterator<int32> PropertyStore::GetInt32PropertiesIter() const { |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 151 | return PropertyConstIterator<int32>(int32_properties_); |
| 152 | } |
| 153 | |
mukesh agrawal | de29fa8 | 2011-09-16 16:16:36 -0700 | [diff] [blame] | 154 | PropertyConstIterator<std::string> PropertyStore::GetStringPropertiesIter() |
| 155 | const { |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 156 | return PropertyConstIterator<std::string>(string_properties_); |
| 157 | } |
| 158 | |
mukesh agrawal | de29fa8 | 2011-09-16 16:16:36 -0700 | [diff] [blame] | 159 | PropertyConstIterator<Stringmap> PropertyStore::GetStringmapPropertiesIter() |
| 160 | const { |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 161 | return PropertyConstIterator<Stringmap>(stringmap_properties_); |
| 162 | } |
| 163 | |
mukesh agrawal | de29fa8 | 2011-09-16 16:16:36 -0700 | [diff] [blame] | 164 | PropertyConstIterator<Strings> PropertyStore::GetStringsPropertiesIter() const { |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 165 | return PropertyConstIterator<Strings>(strings_properties_); |
| 166 | } |
| 167 | |
mukesh agrawal | de29fa8 | 2011-09-16 16:16:36 -0700 | [diff] [blame] | 168 | PropertyConstIterator<StrIntPair> PropertyStore::GetStrIntPairPropertiesIter() |
| 169 | const { |
Chris Masone | 889666b | 2011-07-03 12:58:50 -0700 | [diff] [blame] | 170 | return PropertyConstIterator<StrIntPair>(strintpair_properties_); |
| 171 | } |
| 172 | |
mukesh agrawal | de29fa8 | 2011-09-16 16:16:36 -0700 | [diff] [blame] | 173 | PropertyConstIterator<uint8> PropertyStore::GetUint8PropertiesIter() const { |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 174 | return PropertyConstIterator<uint8>(uint8_properties_); |
| 175 | } |
| 176 | |
mukesh agrawal | de29fa8 | 2011-09-16 16:16:36 -0700 | [diff] [blame] | 177 | PropertyConstIterator<uint16> PropertyStore::GetUint16PropertiesIter() const { |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 178 | return PropertyConstIterator<uint16>(uint16_properties_); |
| 179 | } |
| 180 | |
mukesh agrawal | de29fa8 | 2011-09-16 16:16:36 -0700 | [diff] [blame] | 181 | PropertyConstIterator<uint32> PropertyStore::GetUint32PropertiesIter() const { |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 182 | return PropertyConstIterator<uint32>(uint32_properties_); |
| 183 | } |
| 184 | |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 185 | void PropertyStore::RegisterBool(const string &name, bool *prop) { |
| 186 | bool_properties_[name] = BoolAccessor(new PropertyAccessor<bool>(prop)); |
| 187 | } |
| 188 | |
| 189 | void PropertyStore::RegisterConstBool(const string &name, const bool *prop) { |
| 190 | bool_properties_[name] = BoolAccessor(new ConstPropertyAccessor<bool>(prop)); |
| 191 | } |
| 192 | |
| 193 | void PropertyStore::RegisterInt16(const string &name, int16 *prop) { |
| 194 | int16_properties_[name] = Int16Accessor(new PropertyAccessor<int16>(prop)); |
| 195 | } |
| 196 | |
| 197 | void PropertyStore::RegisterConstInt16(const string &name, const int16 *prop) { |
| 198 | int16_properties_[name] = |
| 199 | Int16Accessor(new ConstPropertyAccessor<int16>(prop)); |
| 200 | } |
| 201 | |
| 202 | void PropertyStore::RegisterInt32(const string &name, int32 *prop) { |
| 203 | int32_properties_[name] = Int32Accessor(new PropertyAccessor<int32>(prop)); |
| 204 | } |
| 205 | |
| 206 | void PropertyStore::RegisterConstInt32(const string &name, const int32 *prop) { |
| 207 | int32_properties_[name] = |
| 208 | Int32Accessor(new ConstPropertyAccessor<int32>(prop)); |
| 209 | } |
| 210 | |
| 211 | void PropertyStore::RegisterString(const string &name, string *prop) { |
| 212 | string_properties_[name] = StringAccessor(new PropertyAccessor<string>(prop)); |
| 213 | } |
| 214 | |
| 215 | void PropertyStore::RegisterConstString(const string &name, |
| 216 | const string *prop) { |
| 217 | string_properties_[name] = |
| 218 | StringAccessor(new ConstPropertyAccessor<string>(prop)); |
| 219 | } |
| 220 | |
Chris Masone | 43b48a1 | 2011-07-01 13:37:07 -0700 | [diff] [blame] | 221 | void PropertyStore::RegisterStringmap(const string &name, Stringmap *prop) { |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 222 | stringmap_properties_[name] = |
Chris Masone | 43b48a1 | 2011-07-01 13:37:07 -0700 | [diff] [blame] | 223 | StringmapAccessor(new PropertyAccessor<Stringmap>(prop)); |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | void PropertyStore::RegisterConstStringmap(const string &name, |
Chris Masone | 43b48a1 | 2011-07-01 13:37:07 -0700 | [diff] [blame] | 227 | const Stringmap *prop) { |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 228 | stringmap_properties_[name] = |
Chris Masone | 43b48a1 | 2011-07-01 13:37:07 -0700 | [diff] [blame] | 229 | StringmapAccessor(new ConstPropertyAccessor<Stringmap>(prop)); |
| 230 | } |
| 231 | |
Darin Petkov | c086531 | 2011-09-16 15:31:20 -0700 | [diff] [blame] | 232 | void PropertyStore::RegisterStringmaps(const string &name, Stringmaps *prop) { |
| 233 | stringmaps_properties_[name] = |
| 234 | StringmapsAccessor(new PropertyAccessor<Stringmaps>(prop)); |
| 235 | } |
| 236 | |
| 237 | void PropertyStore::RegisterConstStringmaps(const string &name, |
| 238 | const Stringmaps *prop) { |
| 239 | stringmaps_properties_[name] = |
| 240 | StringmapsAccessor(new ConstPropertyAccessor<Stringmaps>(prop)); |
| 241 | } |
| 242 | |
Chris Masone | 43b48a1 | 2011-07-01 13:37:07 -0700 | [diff] [blame] | 243 | void PropertyStore::RegisterStrings(const string &name, Strings *prop) { |
| 244 | strings_properties_[name] = |
| 245 | StringsAccessor(new PropertyAccessor<Strings>(prop)); |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 246 | } |
| 247 | |
Chris Masone | 889666b | 2011-07-03 12:58:50 -0700 | [diff] [blame] | 248 | void PropertyStore::RegisterConstStrings(const string &name, |
| 249 | const Strings *prop) { |
| 250 | strings_properties_[name] = |
| 251 | StringsAccessor(new ConstPropertyAccessor<Strings>(prop)); |
| 252 | } |
| 253 | |
| 254 | void PropertyStore::RegisterUint8(const string &name, uint8 *prop) { |
| 255 | uint8_properties_[name] = Uint8Accessor(new PropertyAccessor<uint8>(prop)); |
| 256 | } |
| 257 | |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 258 | void PropertyStore::RegisterConstUint8(const string &name, const uint8 *prop) { |
| 259 | uint8_properties_[name] = |
| 260 | Uint8Accessor(new ConstPropertyAccessor<uint8>(prop)); |
| 261 | } |
| 262 | |
| 263 | void PropertyStore::RegisterUint16(const std::string &name, uint16 *prop) { |
| 264 | uint16_properties_[name] = Uint16Accessor(new PropertyAccessor<uint16>(prop)); |
| 265 | } |
| 266 | |
| 267 | void PropertyStore::RegisterConstUint16(const string &name, |
| 268 | const uint16 *prop) { |
| 269 | uint16_properties_[name] = |
| 270 | Uint16Accessor(new ConstPropertyAccessor<uint16>(prop)); |
| 271 | } |
| 272 | |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 273 | void PropertyStore::RegisterDerivedBool(const std::string &name, |
| 274 | const BoolAccessor &accessor) { |
| 275 | bool_properties_[name] = accessor; |
| 276 | } |
| 277 | |
| 278 | void PropertyStore::RegisterDerivedString(const std::string &name, |
| 279 | const StringAccessor &accessor) { |
| 280 | string_properties_[name] = accessor; |
| 281 | } |
| 282 | |
| 283 | void PropertyStore::RegisterDerivedStrings(const std::string &name, |
| 284 | const StringsAccessor &accessor) { |
| 285 | strings_properties_[name] = accessor; |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 286 | } |
| 287 | |
Chris Masone | 889666b | 2011-07-03 12:58:50 -0700 | [diff] [blame] | 288 | void PropertyStore::RegisterDerivedStringmaps(const std::string &name, |
| 289 | const StringmapsAccessor &acc) { |
| 290 | stringmaps_properties_[name] = acc; |
| 291 | } |
| 292 | |
| 293 | void PropertyStore::RegisterDerivedStrIntPair(const std::string &name, |
| 294 | const StrIntPairAccessor &acc) { |
| 295 | strintpair_properties_[name] = acc; |
| 296 | } |
| 297 | |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 298 | } // namespace shill |