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