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: |
Paul Stewart | 985d7d5 | 2013-10-16 13:46:09 -0700 | [diff] [blame] | 137 | // |target| is the object on which to call the methods |getter|, |setter| |
| 138 | // and |clearer|. |setter| is allowed to be NULL, in which case we will |
| 139 | // simply reject attempts to set via the accessor. |setter| should return |
| 140 | // true if the value was changed, and false otherwise. |clearer| is allowed |
| 141 | // to be NULL (which is what happens if it is not passed to the constructor), |
| 142 | // in which case, |setter| is called is called with the default value. |
| 143 | // It is an error to pass NULL for either |target| or |getter|. |
mukesh agrawal | ffa3d04 | 2011-10-06 15:26:10 -0700 | [diff] [blame] | 144 | CustomAccessor(C *target, |
Hristo Stefanov | ed2c28c | 2011-11-29 15:37:30 -0800 | [diff] [blame] | 145 | T(C::*getter)(Error *error), |
Paul Stewart | 985d7d5 | 2013-10-16 13:46:09 -0700 | [diff] [blame] | 146 | bool(C::*setter)(const T &value, Error *error), |
| 147 | void(C::*clearer)(Error *error)) |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 148 | : target_(target), |
mukesh agrawal | 292dc0f | 2012-01-26 18:02:46 -0800 | [diff] [blame] | 149 | default_value_(), |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 150 | getter_(getter), |
Paul Stewart | 985d7d5 | 2013-10-16 13:46:09 -0700 | [diff] [blame] | 151 | setter_(setter), |
| 152 | clearer_(clearer) { |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 153 | DCHECK(target); |
mukesh agrawal | 292dc0f | 2012-01-26 18:02:46 -0800 | [diff] [blame] | 154 | DCHECK(getter); // otherwise, use CustomWriteOnlyAccessor |
| 155 | if (setter_) { |
| 156 | Error e; |
| 157 | default_value_ = Get(&e); |
| 158 | } |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 159 | } |
Paul Stewart | 985d7d5 | 2013-10-16 13:46:09 -0700 | [diff] [blame] | 160 | CustomAccessor(C *target, |
| 161 | T(C::*getter)(Error *error), |
| 162 | bool(C::*setter)(const T &value, Error *error)) |
| 163 | : CustomAccessor(target, getter, setter, NULL) {} |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 164 | virtual ~CustomAccessor() {} |
| 165 | |
Paul Stewart | 985d7d5 | 2013-10-16 13:46:09 -0700 | [diff] [blame] | 166 | void Clear(Error *error) { |
| 167 | if (clearer_) { |
| 168 | (target_->*clearer_)(error); |
| 169 | } else { |
| 170 | Set(default_value_, error); |
| 171 | } |
| 172 | } |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 173 | T Get(Error *error) { |
mukesh agrawal | 292dc0f | 2012-01-26 18:02:46 -0800 | [diff] [blame] | 174 | return (target_->*getter_)(error); |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 175 | } |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 176 | bool Set(const T &value, Error *error) { |
mukesh agrawal | ffa3d04 | 2011-10-06 15:26:10 -0700 | [diff] [blame] | 177 | if (setter_) { |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 178 | return (target_->*setter_)(value, error); |
mukesh agrawal | ffa3d04 | 2011-10-06 15:26:10 -0700 | [diff] [blame] | 179 | } else { |
| 180 | error->Populate(Error::kInvalidArguments, "Property is read-only"); |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 181 | return false; |
mukesh agrawal | ffa3d04 | 2011-10-06 15:26:10 -0700 | [diff] [blame] | 182 | } |
| 183 | } |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 184 | |
| 185 | private: |
mukesh agrawal | 292dc0f | 2012-01-26 18:02:46 -0800 | [diff] [blame] | 186 | C *const target_; |
| 187 | // |default_value_| is non-const because it can't be initialized in |
| 188 | // the initializer list. |
| 189 | T default_value_; |
| 190 | T(C::*const getter_)(Error *error); |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 191 | bool(C::*const setter_)(const T &value, Error *error); |
Paul Stewart | 985d7d5 | 2013-10-16 13:46:09 -0700 | [diff] [blame] | 192 | void(C::*const clearer_)(Error *error); |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 193 | DISALLOW_COPY_AND_ASSIGN(CustomAccessor); |
| 194 | }; |
| 195 | |
mukesh agrawal | 292dc0f | 2012-01-26 18:02:46 -0800 | [diff] [blame] | 196 | // CustomWriteOnlyAccessor<> allows a custom writer method to be provided. |
| 197 | // Get returns an error automatically. Clear resets the value to a |
| 198 | // default value. |
| 199 | template<class C, class T> |
| 200 | class CustomWriteOnlyAccessor : public AccessorInterface<T> { |
| 201 | public: |
| 202 | // |target| is the object on which to call |setter| and |clearer|. |
| 203 | // |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 204 | // |target| and |setter| must be non-NULL. |setter| should return true |
| 205 | // if the value was changed, and false otherwise. |
mukesh agrawal | 292dc0f | 2012-01-26 18:02:46 -0800 | [diff] [blame] | 206 | // |
| 207 | // Either |clearer| or |default_value|, but not both, must be non-NULL. |
| 208 | // Whichever is non-NULL is used to clear the property. |
| 209 | CustomWriteOnlyAccessor(C *target, |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 210 | bool(C::*setter)(const T &value, Error *error), |
mukesh agrawal | 292dc0f | 2012-01-26 18:02:46 -0800 | [diff] [blame] | 211 | void(C::*clearer)(Error *error), |
| 212 | const T *default_value) |
| 213 | : target_(target), |
| 214 | setter_(setter), |
| 215 | clearer_(clearer), |
| 216 | default_value_() { |
| 217 | DCHECK(target); |
| 218 | DCHECK(setter); |
| 219 | DCHECK(clearer || default_value); |
| 220 | DCHECK(!clearer || !default_value); |
| 221 | if (default_value) { |
| 222 | default_value_ = *default_value; |
| 223 | } |
| 224 | } |
| 225 | virtual ~CustomWriteOnlyAccessor() {} |
| 226 | |
| 227 | void Clear(Error *error) { |
| 228 | if (clearer_) { |
| 229 | (target_->*clearer_)(error); |
| 230 | } else { |
| 231 | Set(default_value_, error); |
| 232 | } |
| 233 | } |
| 234 | T Get(Error *error) { |
| 235 | error->Populate(Error::kPermissionDenied, "Property is write-only"); |
| 236 | return T(); |
| 237 | } |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 238 | bool Set(const T &value, Error *error) { |
| 239 | return (target_->*setter_)(value, error); |
mukesh agrawal | 292dc0f | 2012-01-26 18:02:46 -0800 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | private: |
| 243 | C *const target_; |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 244 | bool(C::*const setter_)(const T &value, Error *error); |
mukesh agrawal | 292dc0f | 2012-01-26 18:02:46 -0800 | [diff] [blame] | 245 | void(C::*const clearer_)(Error *error); |
| 246 | // |default_value_| is non-const because it can't be initialized in |
| 247 | // the initializer list. |
| 248 | T default_value_; |
| 249 | DISALLOW_COPY_AND_ASSIGN(CustomWriteOnlyAccessor); |
| 250 | }; |
| 251 | |
Paul Stewart | a61593e | 2012-03-23 13:06:21 -0700 | [diff] [blame] | 252 | // CustomMappedAccessor<> passes an argument to the getter and setter |
| 253 | // so that a generic method can be used, for example one that accesses the |
| 254 | // property in a map. |
| 255 | template<class C, class T, class A> |
| 256 | class CustomMappedAccessor : public AccessorInterface<T> { |
| 257 | public: |
| 258 | // |target| is the object on which to call the methods |getter| and |setter|. |
| 259 | // |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] | 260 | // attempts to set via the accessor. |setter| should return true if the |
| 261 | // value was changed, and false otherwise. |
Paul Stewart | a61593e | 2012-03-23 13:06:21 -0700 | [diff] [blame] | 262 | // |argument| is passed to the getter and setter methods to disambiguate |
| 263 | // between different properties in |target|. |
| 264 | // It is an error to pass NULL for any of |target|, |clearer| or |getter|. |
| 265 | CustomMappedAccessor(C *target, |
| 266 | void(C::*clearer)(const A &argument, Error *error), |
| 267 | T(C::*getter)(const A &argument, Error *error), |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 268 | bool(C::*setter)(const A &argument, const T &value, |
Paul Stewart | a61593e | 2012-03-23 13:06:21 -0700 | [diff] [blame] | 269 | Error *error), |
| 270 | const A &argument) |
| 271 | : target_(target), |
| 272 | clearer_(clearer), |
| 273 | getter_(getter), |
| 274 | setter_(setter), |
| 275 | argument_(argument) { |
| 276 | DCHECK(clearer); |
| 277 | DCHECK(target); |
| 278 | DCHECK(getter); |
| 279 | } |
| 280 | virtual ~CustomMappedAccessor() {} |
| 281 | |
| 282 | void Clear(Error *error) { |
| 283 | (target_->*clearer_)(argument_, error); |
| 284 | } |
| 285 | T Get(Error *error) { |
| 286 | return (target_->*getter_)(argument_, error); |
| 287 | } |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 288 | bool Set(const T &value, Error *error) { |
Paul Stewart | a61593e | 2012-03-23 13:06:21 -0700 | [diff] [blame] | 289 | if (setter_) { |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 290 | return (target_->*setter_)(argument_, value, error); |
Paul Stewart | a61593e | 2012-03-23 13:06:21 -0700 | [diff] [blame] | 291 | } else { |
| 292 | error->Populate(Error::kInvalidArguments, "Property is read-only"); |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 293 | return false; |
Paul Stewart | a61593e | 2012-03-23 13:06:21 -0700 | [diff] [blame] | 294 | } |
| 295 | } |
| 296 | |
| 297 | private: |
| 298 | C *const target_; |
| 299 | void(C::*const clearer_)(const A &argument, Error *error); |
| 300 | T(C::*const getter_)(const A &argument, Error *error); |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 301 | bool(C::*const setter_)(const A &argument, const T &value, Error *error); |
Paul Stewart | a61593e | 2012-03-23 13:06:21 -0700 | [diff] [blame] | 302 | A argument_; |
| 303 | DISALLOW_COPY_AND_ASSIGN(CustomMappedAccessor); |
| 304 | }; |
| 305 | |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 306 | } // namespace shill |
| 307 | |
| 308 | #endif // SHILL_PROPERTY_ACCESSOR_ |