mukesh agrawal | 292dc0f | 2012-01-26 18:02:46 -0800 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -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_ACCESSOR_ |
| 6 | #define SHILL_PROPERTY_ACCESSOR_ |
| 7 | |
| 8 | #include <base/basictypes.h> |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 9 | #include <gtest/gtest_prod.h> // for FRIEND_TEST. |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 10 | |
| 11 | #include "shill/accessor_interface.h" |
mukesh agrawal | ffa3d04 | 2011-10-06 15:26:10 -0700 | [diff] [blame] | 12 | #include "shill/error.h" |
Christopher Wiley | d778352 | 2012-08-10 14:21:47 -0700 | [diff] [blame] | 13 | #include "shill/logging.h" |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 14 | |
| 15 | namespace shill { |
| 16 | |
| 17 | // Templated implementations of AccessorInterface<>. |
mukesh agrawal | 292dc0f | 2012-01-26 18:02:46 -0800 | [diff] [blame] | 18 | // |
| 19 | // PropertyAccessor<>, ConstPropertyAccessor<>, and |
| 20 | // WriteOnlyPropertyAccessor<> provide R/W, R/O, and W/O access |
| 21 | // (respectively) to the value pointed to by |property|. |
| 22 | // |
| 23 | // This allows a class to easily map strings to member variables, so that |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 24 | // pieces of state stored in the class can be queried or updated by name. |
| 25 | // |
| 26 | // bool foo = true; |
| 27 | // map<string, BoolAccessor> accessors; |
| 28 | // accessors["foo"] = BoolAccessor(new PropertyAccessor<bool>(&foo)); |
| 29 | // bool new_foo = accessors["foo"]->Get(); // new_foo == true |
| 30 | // accessors["foo"]->Set(false); // returns true, because setting is allowed. |
| 31 | // // foo == false, new_foo == true |
| 32 | // new_foo = accessors["foo"]->Get(); // new_foo == false |
mukesh agrawal | 292dc0f | 2012-01-26 18:02:46 -0800 | [diff] [blame] | 33 | // // Clear resets |foo| to its value when the PropertyAccessor was created. |
| 34 | // accessors["foo"]->Clear(); // foo == true |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 35 | // |
| 36 | // Generic accessors that provide write capability will check that the |
| 37 | // new value differs from the present one. If the old and new values |
| 38 | // are the same, the setter will not invoke the assignment operator, and |
| 39 | // will return false. |
| 40 | // |
| 41 | // Custom accessors are responsible for handling set-to-same-value |
| 42 | // themselves. It is not possible to handle that here, because some |
| 43 | // custom getters return default values, rather than the actual |
| 44 | // value. (I'm looking at you, WiFi::GetBgscanMethod.) |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 45 | template <class T> |
| 46 | class PropertyAccessor : public AccessorInterface<T> { |
| 47 | public: |
mukesh agrawal | 292dc0f | 2012-01-26 18:02:46 -0800 | [diff] [blame] | 48 | explicit PropertyAccessor(T *property) |
| 49 | : property_(property), default_value_(*property) { |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 50 | DCHECK(property); |
| 51 | } |
| 52 | virtual ~PropertyAccessor() {} |
| 53 | |
mukesh agrawal | 292dc0f | 2012-01-26 18:02:46 -0800 | [diff] [blame] | 54 | void Clear(Error *error) { Set(default_value_, error); } |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 55 | T Get(Error */*error*/) { return *property_; } |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 56 | bool Set(const T &value, Error */*error*/) { |
| 57 | if (*property_ == value) { |
| 58 | return false; |
| 59 | } |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 60 | *property_ = value; |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 61 | return true; |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | private: |
| 65 | T * const property_; |
mukesh agrawal | 292dc0f | 2012-01-26 18:02:46 -0800 | [diff] [blame] | 66 | const T default_value_; |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 67 | DISALLOW_COPY_AND_ASSIGN(PropertyAccessor); |
| 68 | }; |
| 69 | |
| 70 | template <class T> |
| 71 | class ConstPropertyAccessor : public AccessorInterface<T> { |
| 72 | public: |
| 73 | explicit ConstPropertyAccessor(const T *property) : property_(property) { |
| 74 | DCHECK(property); |
| 75 | } |
| 76 | virtual ~ConstPropertyAccessor() {} |
| 77 | |
mukesh agrawal | 292dc0f | 2012-01-26 18:02:46 -0800 | [diff] [blame] | 78 | void Clear(Error *error) { |
| 79 | // TODO(quiche): check if this is the right error. |
| 80 | // (maybe Error::kInvalidProperty instead?) |
| 81 | error->Populate(Error::kInvalidArguments, "Property is read-only"); |
| 82 | } |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 83 | T Get(Error */*error*/) { return *property_; } |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 84 | bool Set(const T &/*value*/, Error *error) { |
mukesh agrawal | ffa3d04 | 2011-10-06 15:26:10 -0700 | [diff] [blame] | 85 | // TODO(quiche): check if this is the right error. |
| 86 | // (maybe Error::kPermissionDenied instead?) |
| 87 | error->Populate(Error::kInvalidArguments, "Property is read-only"); |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 88 | return false; |
mukesh agrawal | ffa3d04 | 2011-10-06 15:26:10 -0700 | [diff] [blame] | 89 | } |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 90 | |
| 91 | private: |
| 92 | const T * const property_; |
| 93 | DISALLOW_COPY_AND_ASSIGN(ConstPropertyAccessor); |
| 94 | }; |
| 95 | |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 96 | template <class T> |
| 97 | class WriteOnlyPropertyAccessor : public AccessorInterface<T> { |
| 98 | public: |
mukesh agrawal | 292dc0f | 2012-01-26 18:02:46 -0800 | [diff] [blame] | 99 | explicit WriteOnlyPropertyAccessor(T *property) |
| 100 | : property_(property), default_value_(*property) { |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 101 | DCHECK(property); |
| 102 | } |
| 103 | virtual ~WriteOnlyPropertyAccessor() {} |
| 104 | |
mukesh agrawal | 292dc0f | 2012-01-26 18:02:46 -0800 | [diff] [blame] | 105 | void Clear(Error *error) { Set(default_value_, error); } |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 106 | T Get(Error *error) { |
| 107 | error->Populate(Error::kPermissionDenied, "Property is write-only"); |
| 108 | return T(); |
| 109 | } |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 110 | bool Set(const T &value, Error */*error*/) { |
| 111 | if (*property_ == value) { |
| 112 | return false; |
| 113 | } |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 114 | *property_ = value; |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 115 | return true; |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | private: |
| 119 | FRIEND_TEST(PropertyAccessorTest, SignedIntCorrectness); |
| 120 | FRIEND_TEST(PropertyAccessorTest, UnsignedIntCorrectness); |
| 121 | FRIEND_TEST(PropertyAccessorTest, StringCorrectness); |
| 122 | |
| 123 | T * const property_; |
mukesh agrawal | 292dc0f | 2012-01-26 18:02:46 -0800 | [diff] [blame] | 124 | const T default_value_; |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 125 | DISALLOW_COPY_AND_ASSIGN(WriteOnlyPropertyAccessor); |
| 126 | }; |
| 127 | |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 128 | // CustomAccessor<> allows custom getter and setter methods to be provided. |
mukesh agrawal | 292dc0f | 2012-01-26 18:02:46 -0800 | [diff] [blame] | 129 | // Thus, if the state to be returned is to be derived on-demand, or if |
| 130 | // setting the property requires validation, we can still fit it into the |
| 131 | // AccessorInterface<> framework. |
| 132 | // |
| 133 | // If the property is write-only, use CustomWriteOnlyAccessor instead. |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 134 | template<class C, class T> |
| 135 | class CustomAccessor : public AccessorInterface<T> { |
| 136 | public: |
| 137 | // |target| is the object on which to call the methods |getter| and |setter| |
| 138 | // |setter| is allowed to be NULL, in which case we will simply reject |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 139 | // attempts to set via the accessor. |setter| should return true if the |
| 140 | // value was changed, and false otherwise. |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 141 | // It is an error to pass NULL for either of the other two arguments. |
mukesh agrawal | ffa3d04 | 2011-10-06 15:26:10 -0700 | [diff] [blame] | 142 | CustomAccessor(C *target, |
Hristo Stefanov | ed2c28c | 2011-11-29 15:37:30 -0800 | [diff] [blame] | 143 | T(C::*getter)(Error *error), |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 144 | bool(C::*setter)(const T &value, Error *error)) |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 145 | : target_(target), |
mukesh agrawal | 292dc0f | 2012-01-26 18:02:46 -0800 | [diff] [blame] | 146 | default_value_(), |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 147 | getter_(getter), |
| 148 | setter_(setter) { |
| 149 | DCHECK(target); |
mukesh agrawal | 292dc0f | 2012-01-26 18:02:46 -0800 | [diff] [blame] | 150 | DCHECK(getter); // otherwise, use CustomWriteOnlyAccessor |
| 151 | if (setter_) { |
| 152 | Error e; |
| 153 | default_value_ = Get(&e); |
| 154 | } |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 155 | } |
| 156 | virtual ~CustomAccessor() {} |
| 157 | |
mukesh agrawal | 292dc0f | 2012-01-26 18:02:46 -0800 | [diff] [blame] | 158 | void Clear(Error *error) { Set(default_value_, error); } |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 159 | T Get(Error *error) { |
mukesh agrawal | 292dc0f | 2012-01-26 18:02:46 -0800 | [diff] [blame] | 160 | return (target_->*getter_)(error); |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 161 | } |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 162 | bool Set(const T &value, Error *error) { |
mukesh agrawal | ffa3d04 | 2011-10-06 15:26:10 -0700 | [diff] [blame] | 163 | if (setter_) { |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 164 | return (target_->*setter_)(value, error); |
mukesh agrawal | ffa3d04 | 2011-10-06 15:26:10 -0700 | [diff] [blame] | 165 | } else { |
| 166 | error->Populate(Error::kInvalidArguments, "Property is read-only"); |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 167 | return false; |
mukesh agrawal | ffa3d04 | 2011-10-06 15:26:10 -0700 | [diff] [blame] | 168 | } |
| 169 | } |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 170 | |
| 171 | private: |
mukesh agrawal | 292dc0f | 2012-01-26 18:02:46 -0800 | [diff] [blame] | 172 | C *const target_; |
| 173 | // |default_value_| is non-const because it can't be initialized in |
| 174 | // the initializer list. |
| 175 | T default_value_; |
| 176 | T(C::*const getter_)(Error *error); |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 177 | bool(C::*const setter_)(const T &value, Error *error); |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 178 | DISALLOW_COPY_AND_ASSIGN(CustomAccessor); |
| 179 | }; |
| 180 | |
mukesh agrawal | 292dc0f | 2012-01-26 18:02:46 -0800 | [diff] [blame] | 181 | // CustomWriteOnlyAccessor<> allows a custom writer method to be provided. |
| 182 | // Get returns an error automatically. Clear resets the value to a |
| 183 | // default value. |
| 184 | template<class C, class T> |
| 185 | class CustomWriteOnlyAccessor : public AccessorInterface<T> { |
| 186 | public: |
| 187 | // |target| is the object on which to call |setter| and |clearer|. |
| 188 | // |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 189 | // |target| and |setter| must be non-NULL. |setter| should return true |
| 190 | // if the value was changed, and false otherwise. |
mukesh agrawal | 292dc0f | 2012-01-26 18:02:46 -0800 | [diff] [blame] | 191 | // |
| 192 | // Either |clearer| or |default_value|, but not both, must be non-NULL. |
| 193 | // Whichever is non-NULL is used to clear the property. |
| 194 | CustomWriteOnlyAccessor(C *target, |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 195 | bool(C::*setter)(const T &value, Error *error), |
mukesh agrawal | 292dc0f | 2012-01-26 18:02:46 -0800 | [diff] [blame] | 196 | void(C::*clearer)(Error *error), |
| 197 | const T *default_value) |
| 198 | : target_(target), |
| 199 | setter_(setter), |
| 200 | clearer_(clearer), |
| 201 | default_value_() { |
| 202 | DCHECK(target); |
| 203 | DCHECK(setter); |
| 204 | DCHECK(clearer || default_value); |
| 205 | DCHECK(!clearer || !default_value); |
| 206 | if (default_value) { |
| 207 | default_value_ = *default_value; |
| 208 | } |
| 209 | } |
| 210 | virtual ~CustomWriteOnlyAccessor() {} |
| 211 | |
| 212 | void Clear(Error *error) { |
| 213 | if (clearer_) { |
| 214 | (target_->*clearer_)(error); |
| 215 | } else { |
| 216 | Set(default_value_, error); |
| 217 | } |
| 218 | } |
| 219 | T Get(Error *error) { |
| 220 | error->Populate(Error::kPermissionDenied, "Property is write-only"); |
| 221 | return T(); |
| 222 | } |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 223 | bool Set(const T &value, Error *error) { |
| 224 | return (target_->*setter_)(value, error); |
mukesh agrawal | 292dc0f | 2012-01-26 18:02:46 -0800 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | private: |
| 228 | C *const target_; |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 229 | bool(C::*const setter_)(const T &value, Error *error); |
mukesh agrawal | 292dc0f | 2012-01-26 18:02:46 -0800 | [diff] [blame] | 230 | void(C::*const clearer_)(Error *error); |
| 231 | // |default_value_| is non-const because it can't be initialized in |
| 232 | // the initializer list. |
| 233 | T default_value_; |
| 234 | DISALLOW_COPY_AND_ASSIGN(CustomWriteOnlyAccessor); |
| 235 | }; |
| 236 | |
Paul Stewart | a61593e | 2012-03-23 13:06:21 -0700 | [diff] [blame] | 237 | // CustomMappedAccessor<> passes an argument to the getter and setter |
| 238 | // so that a generic method can be used, for example one that accesses the |
| 239 | // property in a map. |
| 240 | template<class C, class T, class A> |
| 241 | class CustomMappedAccessor : public AccessorInterface<T> { |
| 242 | public: |
| 243 | // |target| is the object on which to call the methods |getter| and |setter|. |
| 244 | // |setter| is allowed to be NULL, in which case we will simply reject |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 245 | // attempts to set via the accessor. |setter| should return true if the |
| 246 | // value was changed, and false otherwise. |
Paul Stewart | a61593e | 2012-03-23 13:06:21 -0700 | [diff] [blame] | 247 | // |argument| is passed to the getter and setter methods to disambiguate |
| 248 | // between different properties in |target|. |
| 249 | // It is an error to pass NULL for any of |target|, |clearer| or |getter|. |
| 250 | CustomMappedAccessor(C *target, |
| 251 | void(C::*clearer)(const A &argument, Error *error), |
| 252 | T(C::*getter)(const A &argument, Error *error), |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 253 | bool(C::*setter)(const A &argument, const T &value, |
Paul Stewart | a61593e | 2012-03-23 13:06:21 -0700 | [diff] [blame] | 254 | Error *error), |
| 255 | const A &argument) |
| 256 | : target_(target), |
| 257 | clearer_(clearer), |
| 258 | getter_(getter), |
| 259 | setter_(setter), |
| 260 | argument_(argument) { |
| 261 | DCHECK(clearer); |
| 262 | DCHECK(target); |
| 263 | DCHECK(getter); |
| 264 | } |
| 265 | virtual ~CustomMappedAccessor() {} |
| 266 | |
| 267 | void Clear(Error *error) { |
| 268 | (target_->*clearer_)(argument_, error); |
| 269 | } |
| 270 | T Get(Error *error) { |
| 271 | return (target_->*getter_)(argument_, error); |
| 272 | } |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 273 | bool Set(const T &value, Error *error) { |
Paul Stewart | a61593e | 2012-03-23 13:06:21 -0700 | [diff] [blame] | 274 | if (setter_) { |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 275 | return (target_->*setter_)(argument_, value, error); |
Paul Stewart | a61593e | 2012-03-23 13:06:21 -0700 | [diff] [blame] | 276 | } else { |
| 277 | error->Populate(Error::kInvalidArguments, "Property is read-only"); |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 278 | return false; |
Paul Stewart | a61593e | 2012-03-23 13:06:21 -0700 | [diff] [blame] | 279 | } |
| 280 | } |
| 281 | |
| 282 | private: |
| 283 | C *const target_; |
| 284 | void(C::*const clearer_)(const A &argument, Error *error); |
| 285 | T(C::*const getter_)(const A &argument, Error *error); |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 286 | bool(C::*const setter_)(const A &argument, const T &value, Error *error); |
Paul Stewart | a61593e | 2012-03-23 13:06:21 -0700 | [diff] [blame] | 287 | A argument_; |
| 288 | DISALLOW_COPY_AND_ASSIGN(CustomMappedAccessor); |
| 289 | }; |
| 290 | |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 291 | } // namespace shill |
| 292 | |
| 293 | #endif // SHILL_PROPERTY_ACCESSOR_ |