mukesh agrawal | 4d0401c | 2012-01-06 16:05:31 -0800 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef SHILL_PROPERTY_STORE_ |
| 6 | #define SHILL_PROPERTY_STORE_ |
| 7 | |
| 8 | #include <map> |
| 9 | #include <string> |
| 10 | #include <vector> |
| 11 | |
| 12 | #include <base/basictypes.h> |
| 13 | |
| 14 | #include "shill/accessor_interface.h" |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 15 | #include "shill/property_iterator.h" |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 16 | |
| 17 | namespace shill { |
| 18 | |
| 19 | class Error; |
| 20 | |
| 21 | class PropertyStore { |
| 22 | public: |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 23 | PropertyStore(); |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 24 | virtual ~PropertyStore(); |
| 25 | |
mukesh agrawal | de29fa8 | 2011-09-16 16:16:36 -0700 | [diff] [blame] | 26 | virtual bool Contains(const std::string& property) const; |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 27 | |
Paul Stewart | e6e8e49 | 2013-01-17 11:00:50 -0800 | [diff] [blame] | 28 | // Methods to allow the getting of properties stored in the referenced |
| 29 | // |store_| by name. Upon success, these methods return true and return the |
| 30 | // property value in |value|. Upon failure, they return false and |
| 31 | // leave |value| untouched. |
| 32 | bool GetBoolProperty(const std::string &name, bool *value, |
| 33 | Error *error) const; |
| 34 | bool GetInt16Property(const std::string &name, int16 *value, |
| 35 | Error *error) const; |
| 36 | bool GetInt32Property(const std::string &name, int32 *value, |
| 37 | Error *error) const; |
| 38 | bool GetKeyValueStoreProperty(const std::string &name, KeyValueStore *value, |
| 39 | Error *error) const; |
| 40 | bool GetStringProperty(const std::string &name, std::string *value, |
| 41 | Error *error) const; |
| 42 | bool GetStringmapProperty(const std::string &name, Stringmap *values, |
| 43 | Error *error) const; |
| 44 | bool GetStringmapsProperty(const std::string &name, Stringmaps *values, |
| 45 | Error *error) const; |
| 46 | bool GetStringsProperty(const std::string &name, Strings *values, |
| 47 | Error *error) const; |
| 48 | bool GetUint8Property(const std::string &name, uint8 *value, |
| 49 | Error *error) const; |
| 50 | bool GetUint16Property(const std::string &name, uint16 *value, |
| 51 | Error *error) const; |
| 52 | bool GetUint32Property(const std::string &name, uint32 *value, |
| 53 | Error *error) const; |
| 54 | bool GetUint64Property(const std::string &name, uint64 *value, |
| 55 | Error *error) const; |
| 56 | bool GetRpcIdentifierProperty(const std::string &name, RpcIdentifier *value, |
| 57 | Error *error) const; |
| 58 | |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 59 | // Methods to allow the setting, by name, of properties stored in this object. |
| 60 | // The property names are declared in chromeos/dbus/service_constants.h, |
| 61 | // so that they may be shared with libcros. |
| 62 | // Upon success, these methods return true and leave |error| untouched. |
| 63 | // Upon failure, they return false and set |error| appropriately, if it |
| 64 | // is non-NULL. |
mukesh agrawal | 66b0aca | 2012-01-30 15:28:28 -0800 | [diff] [blame] | 65 | virtual bool SetBoolProperty(const std::string &name, |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 66 | bool value, |
| 67 | Error *error); |
| 68 | |
mukesh agrawal | 66b0aca | 2012-01-30 15:28:28 -0800 | [diff] [blame] | 69 | virtual bool SetInt16Property(const std::string &name, |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 70 | int16 value, |
| 71 | Error *error); |
| 72 | |
mukesh agrawal | 66b0aca | 2012-01-30 15:28:28 -0800 | [diff] [blame] | 73 | virtual bool SetInt32Property(const std::string &name, |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 74 | int32 value, |
| 75 | Error *error); |
| 76 | |
mukesh agrawal | 66b0aca | 2012-01-30 15:28:28 -0800 | [diff] [blame] | 77 | virtual bool SetStringProperty(const std::string &name, |
| 78 | const std::string &value, |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 79 | Error *error); |
| 80 | |
| 81 | virtual bool SetStringmapProperty( |
mukesh agrawal | 66b0aca | 2012-01-30 15:28:28 -0800 | [diff] [blame] | 82 | const std::string &name, |
| 83 | const std::map<std::string, std::string> &values, |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 84 | Error *error); |
| 85 | |
mukesh agrawal | 66b0aca | 2012-01-30 15:28:28 -0800 | [diff] [blame] | 86 | virtual bool SetStringsProperty(const std::string &name, |
| 87 | const std::vector<std::string> &values, |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 88 | Error *error); |
| 89 | |
mukesh agrawal | 66b0aca | 2012-01-30 15:28:28 -0800 | [diff] [blame] | 90 | virtual bool SetUint8Property(const std::string &name, |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 91 | uint8 value, |
| 92 | Error *error); |
| 93 | |
mukesh agrawal | 66b0aca | 2012-01-30 15:28:28 -0800 | [diff] [blame] | 94 | virtual bool SetUint16Property(const std::string &name, |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 95 | uint16 value, |
| 96 | Error *error); |
| 97 | |
mukesh agrawal | 66b0aca | 2012-01-30 15:28:28 -0800 | [diff] [blame] | 98 | virtual bool SetUint32Property(const std::string &name, |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 99 | uint32 value, |
| 100 | Error *error); |
| 101 | |
Paul Stewart | e18c33b | 2012-07-10 20:48:44 -0700 | [diff] [blame] | 102 | virtual bool SetUint64Property(const std::string &name, |
| 103 | uint64 value, |
| 104 | Error *error); |
| 105 | |
Jason Glasgow | acdc11f | 2012-03-30 14:12:22 -0400 | [diff] [blame] | 106 | virtual bool SetRpcIdentifierProperty(const std::string &name, |
| 107 | const RpcIdentifier &value, |
| 108 | Error *error); |
| 109 | |
mukesh agrawal | 4d260da | 2012-01-30 11:53:52 -0800 | [diff] [blame] | 110 | // Clearing a property resets it to its "factory" value. This value |
| 111 | // is generally the value that it (the property) had when it was |
| 112 | // registered with PropertyStore. |
| 113 | // |
| 114 | // The exception to this rule is write-only derived properties. For |
| 115 | // such properties, the property owner explicitly provides a |
| 116 | // "factory" value at registration time. This is necessary because |
| 117 | // PropertyStore can't read the current value at registration time. |
| 118 | // |
| 119 | // |name| is the key used to access the property. If the property |
| 120 | // cannot be cleared, |error| is set, and the method returns false. |
| 121 | // Otherwrise, |error| is unchanged, and the method returns true. |
| 122 | virtual bool ClearProperty(const std::string &name, Error *error); |
| 123 | |
mukesh agrawal | ffa3d04 | 2011-10-06 15:26:10 -0700 | [diff] [blame] | 124 | // Accessors for iterators over property maps. Useful for dumping all |
| 125 | // properties. |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 126 | ReadablePropertyConstIterator<bool> GetBoolPropertiesIter() const; |
| 127 | ReadablePropertyConstIterator<int16> GetInt16PropertiesIter() const; |
| 128 | ReadablePropertyConstIterator<int32> GetInt32PropertiesIter() const; |
Darin Petkov | 63138a9 | 2012-02-06 14:09:15 +0100 | [diff] [blame] | 129 | ReadablePropertyConstIterator<KeyValueStore> GetKeyValueStorePropertiesIter( |
| 130 | ) const; |
Jason Glasgow | acdc11f | 2012-03-30 14:12:22 -0400 | [diff] [blame] | 131 | ReadablePropertyConstIterator<RpcIdentifier> GetRpcIdentifierPropertiesIter( |
| 132 | ) const; |
mukesh agrawal | 2366eed | 2012-03-20 18:21:50 -0700 | [diff] [blame] | 133 | ReadablePropertyConstIterator<RpcIdentifiers> GetRpcIdentifiersPropertiesIter( |
| 134 | ) const; |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 135 | ReadablePropertyConstIterator<std::string> GetStringPropertiesIter() const; |
| 136 | ReadablePropertyConstIterator<Stringmap> GetStringmapPropertiesIter() const; |
| 137 | ReadablePropertyConstIterator<Stringmaps> GetStringmapsPropertiesIter() const; |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 138 | ReadablePropertyConstIterator<Strings> GetStringsPropertiesIter() const; |
| 139 | ReadablePropertyConstIterator<uint8> GetUint8PropertiesIter() const; |
| 140 | ReadablePropertyConstIterator<uint16> GetUint16PropertiesIter() const; |
| 141 | ReadablePropertyConstIterator<uint32> GetUint32PropertiesIter() const; |
Paul Stewart | e18c33b | 2012-07-10 20:48:44 -0700 | [diff] [blame] | 142 | ReadablePropertyConstIterator<uint64> GetUint64PropertiesIter() const; |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 143 | |
mukesh agrawal | 4d260da | 2012-01-30 11:53:52 -0800 | [diff] [blame] | 144 | // Methods for registering a property. |
| 145 | // |
| 146 | // It is permitted to re-register a property (in which case the old |
| 147 | // binding is forgotten). However, the newly bound object must be of |
| 148 | // the same type. |
| 149 | // |
| 150 | // Note that types do not encode read-write permission. Hence, it |
| 151 | // is possible to change permissions by rebinding a property to the |
| 152 | // same object. |
| 153 | // |
| 154 | // (Corollary of the rebinding-to-same-type restriction: a |
| 155 | // PropertyStore cannot hold two properties of the same name, but |
| 156 | // differing types.) |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 157 | void RegisterBool(const std::string &name, bool *prop); |
| 158 | void RegisterConstBool(const std::string &name, const bool *prop); |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 159 | void RegisterWriteOnlyBool(const std::string &name, bool *prop); |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 160 | void RegisterInt16(const std::string &name, int16 *prop); |
| 161 | void RegisterConstInt16(const std::string &name, const int16 *prop); |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 162 | void RegisterWriteOnlyInt16(const std::string &name, int16 *prop); |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 163 | void RegisterInt32(const std::string &name, int32 *prop); |
| 164 | void RegisterConstInt32(const std::string &name, const int32 *prop); |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 165 | void RegisterWriteOnlyInt32(const std::string &name, int32 *prop); |
mukesh agrawal | 4d260da | 2012-01-30 11:53:52 -0800 | [diff] [blame] | 166 | void RegisterUint32(const std::string &name, uint32 *prop); |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 167 | void RegisterString(const std::string &name, std::string *prop); |
| 168 | void RegisterConstString(const std::string &name, const std::string *prop); |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 169 | void RegisterWriteOnlyString(const std::string &name, std::string *prop); |
Chris Masone | 43b48a1 | 2011-07-01 13:37:07 -0700 | [diff] [blame] | 170 | void RegisterStringmap(const std::string &name, Stringmap *prop); |
| 171 | void RegisterConstStringmap(const std::string &name, const Stringmap *prop); |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 172 | void RegisterWriteOnlyStringmap(const std::string &name, Stringmap *prop); |
Darin Petkov | c086531 | 2011-09-16 15:31:20 -0700 | [diff] [blame] | 173 | void RegisterStringmaps(const std::string &name, Stringmaps *prop); |
| 174 | void RegisterConstStringmaps(const std::string &name, const Stringmaps *prop); |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 175 | void RegisterWriteOnlyStringmaps(const std::string &name, Stringmaps *prop); |
Chris Masone | 43b48a1 | 2011-07-01 13:37:07 -0700 | [diff] [blame] | 176 | void RegisterStrings(const std::string &name, Strings *prop); |
Chris Masone | 889666b | 2011-07-03 12:58:50 -0700 | [diff] [blame] | 177 | void RegisterConstStrings(const std::string &name, const Strings *prop); |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 178 | void RegisterWriteOnlyStrings(const std::string &name, Strings *prop); |
Chris Masone | 889666b | 2011-07-03 12:58:50 -0700 | [diff] [blame] | 179 | void RegisterUint8(const std::string &name, uint8 *prop); |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 180 | void RegisterConstUint8(const std::string &name, const uint8 *prop); |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 181 | void RegisterWriteOnlyUint8(const std::string &name, uint8 *prop); |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 182 | void RegisterUint16(const std::string &name, uint16 *prop); |
| 183 | void RegisterConstUint16(const std::string &name, const uint16 *prop); |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 184 | void RegisterWriteOnlyUint16(const std::string &name, uint16 *prop); |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 185 | |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 186 | void RegisterDerivedBool(const std::string &name, |
| 187 | const BoolAccessor &accessor); |
mukesh agrawal | 4d0401c | 2012-01-06 16:05:31 -0800 | [diff] [blame] | 188 | void RegisterDerivedInt32(const std::string &name, |
| 189 | const Int32Accessor &accessor); |
Darin Petkov | 63138a9 | 2012-02-06 14:09:15 +0100 | [diff] [blame] | 190 | void RegisterDerivedKeyValueStore(const std::string &name, |
| 191 | const KeyValueStoreAccessor &accessor); |
Jason Glasgow | acdc11f | 2012-03-30 14:12:22 -0400 | [diff] [blame] | 192 | void RegisterDerivedRpcIdentifier(const std::string &name, |
| 193 | const RpcIdentifierAccessor &acc); |
mukesh agrawal | 2366eed | 2012-03-20 18:21:50 -0700 | [diff] [blame] | 194 | void RegisterDerivedRpcIdentifiers(const std::string &name, |
| 195 | const RpcIdentifiersAccessor &accessor); |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 196 | void RegisterDerivedString(const std::string &name, |
| 197 | const StringAccessor &accessor); |
Eric Shienbrood | 30bc0ec | 2012-03-21 18:19:46 -0400 | [diff] [blame] | 198 | void RegisterDerivedStringmap(const std::string &name, |
| 199 | const StringmapAccessor &accessor); |
Chris Masone | 889666b | 2011-07-03 12:58:50 -0700 | [diff] [blame] | 200 | void RegisterDerivedStringmaps(const std::string &name, |
| 201 | const StringmapsAccessor &accessor); |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 202 | void RegisterDerivedStrings(const std::string &name, |
| 203 | const StringsAccessor &accessor); |
Paul Stewart | be5f5b3 | 2011-12-07 17:11:11 -0800 | [diff] [blame] | 204 | void RegisterDerivedUint16(const std::string &name, |
| 205 | const Uint16Accessor &accessor); |
Paul Stewart | e18c33b | 2012-07-10 20:48:44 -0700 | [diff] [blame] | 206 | void RegisterDerivedUint64(const std::string &name, |
| 207 | const Uint64Accessor &accessor); |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 208 | |
| 209 | private: |
mukesh agrawal | ffa3d04 | 2011-10-06 15:26:10 -0700 | [diff] [blame] | 210 | template <class V> |
Paul Stewart | e6e8e49 | 2013-01-17 11:00:50 -0800 | [diff] [blame] | 211 | bool GetProperty( |
| 212 | const std::string &name, |
| 213 | V *value, |
| 214 | Error *error, |
| 215 | const std::map< std::string, std::tr1::shared_ptr< |
| 216 | AccessorInterface<V> > > &collection, |
| 217 | const std::string &value_type_english) const; |
| 218 | |
| 219 | template <class V> |
mukesh agrawal | ffa3d04 | 2011-10-06 15:26:10 -0700 | [diff] [blame] | 220 | bool SetProperty( |
| 221 | const std::string &name, |
| 222 | const V &value, |
| 223 | Error *error, |
| 224 | std::map< std::string, std::tr1::shared_ptr< AccessorInterface<V> > > &, |
| 225 | const std::string &value_type_english); |
| 226 | |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 227 | // These are std::maps instead of something cooler because the common |
| 228 | // operation is iterating through them and returning all properties. |
| 229 | std::map<std::string, BoolAccessor> bool_properties_; |
| 230 | std::map<std::string, Int16Accessor> int16_properties_; |
| 231 | std::map<std::string, Int32Accessor> int32_properties_; |
Darin Petkov | 63138a9 | 2012-02-06 14:09:15 +0100 | [diff] [blame] | 232 | std::map<std::string, KeyValueStoreAccessor> key_value_store_properties_; |
Jason Glasgow | acdc11f | 2012-03-30 14:12:22 -0400 | [diff] [blame] | 233 | std::map<std::string, RpcIdentifierAccessor> rpc_identifier_properties_; |
mukesh agrawal | 2366eed | 2012-03-20 18:21:50 -0700 | [diff] [blame] | 234 | std::map<std::string, RpcIdentifiersAccessor> rpc_identifiers_properties_; |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 235 | std::map<std::string, StringAccessor> string_properties_; |
| 236 | std::map<std::string, StringmapAccessor> stringmap_properties_; |
Chris Masone | 889666b | 2011-07-03 12:58:50 -0700 | [diff] [blame] | 237 | std::map<std::string, StringmapsAccessor> stringmaps_properties_; |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 238 | std::map<std::string, StringsAccessor> strings_properties_; |
| 239 | std::map<std::string, Uint8Accessor> uint8_properties_; |
| 240 | std::map<std::string, Uint16Accessor> uint16_properties_; |
| 241 | std::map<std::string, Uint32Accessor> uint32_properties_; |
Paul Stewart | e18c33b | 2012-07-10 20:48:44 -0700 | [diff] [blame] | 242 | std::map<std::string, Uint64Accessor> uint64_properties_; |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 243 | |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 244 | DISALLOW_COPY_AND_ASSIGN(PropertyStore); |
| 245 | }; |
| 246 | |
| 247 | } // namespace shill |
| 248 | |
| 249 | #endif // SHILL_PROPERTY_STORE_ |