Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -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 | #ifndef SHILL_PROPERTY_ACCESSOR_ |
| 6 | #define SHILL_PROPERTY_ACCESSOR_ |
| 7 | |
| 8 | #include <base/basictypes.h> |
Chris Masone | 27bf103 | 2011-06-28 17:02:01 -0700 | [diff] [blame] | 9 | #include <base/logging.h> |
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" |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 13 | |
| 14 | namespace shill { |
| 15 | |
| 16 | // Templated implementations of AccessorInterface<>. |
| 17 | // PropertyAccessor<> and ConstPropertyAccessor<> respectively provide |
| 18 | // R/W and R/O access to the value pointed to by |property|. |
| 19 | // this allows a class to easily map strings to member variables, so that |
| 20 | // pieces of state stored in the class can be queried or updated by name. |
| 21 | // |
| 22 | // bool foo = true; |
| 23 | // map<string, BoolAccessor> accessors; |
| 24 | // accessors["foo"] = BoolAccessor(new PropertyAccessor<bool>(&foo)); |
| 25 | // bool new_foo = accessors["foo"]->Get(); // new_foo == true |
| 26 | // accessors["foo"]->Set(false); // returns true, because setting is allowed. |
| 27 | // // foo == false, new_foo == true |
| 28 | // new_foo = accessors["foo"]->Get(); // new_foo == false |
| 29 | template <class T> |
| 30 | class PropertyAccessor : public AccessorInterface<T> { |
| 31 | public: |
| 32 | explicit PropertyAccessor(T *property) : property_(property) { |
| 33 | DCHECK(property); |
| 34 | } |
| 35 | virtual ~PropertyAccessor() {} |
| 36 | |
| 37 | const T &Get() { return *property_; } |
mukesh agrawal | ffa3d04 | 2011-10-06 15:26:10 -0700 | [diff] [blame^] | 38 | void Set(const T &value, Error */*error*/) { |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 39 | *property_ = value; |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | private: |
| 43 | T * const property_; |
| 44 | DISALLOW_COPY_AND_ASSIGN(PropertyAccessor); |
| 45 | }; |
| 46 | |
| 47 | template <class T> |
| 48 | class ConstPropertyAccessor : public AccessorInterface<T> { |
| 49 | public: |
| 50 | explicit ConstPropertyAccessor(const T *property) : property_(property) { |
| 51 | DCHECK(property); |
| 52 | } |
| 53 | virtual ~ConstPropertyAccessor() {} |
| 54 | |
| 55 | const T &Get() { return *property_; } |
mukesh agrawal | ffa3d04 | 2011-10-06 15:26:10 -0700 | [diff] [blame^] | 56 | void Set(const T &/*value*/, Error *error) { |
| 57 | // TODO(quiche): check if this is the right error. |
| 58 | // (maybe Error::kPermissionDenied instead?) |
| 59 | error->Populate(Error::kInvalidArguments, "Property is read-only"); |
| 60 | } |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 61 | |
| 62 | private: |
| 63 | const T * const property_; |
| 64 | DISALLOW_COPY_AND_ASSIGN(ConstPropertyAccessor); |
| 65 | }; |
| 66 | |
| 67 | // CustomAccessor<> allows custom getter and setter methods to be provided. |
| 68 | // Thus, if the state to be returned is to be derived on-demand, we can |
| 69 | // still fit it into the AccessorInterface<> framework. |
| 70 | template<class C, class T> |
| 71 | class CustomAccessor : public AccessorInterface<T> { |
| 72 | public: |
| 73 | // |target| is the object on which to call the methods |getter| and |setter| |
| 74 | // |setter| is allowed to be NULL, in which case we will simply reject |
| 75 | // attempts to set via the accessor. |
| 76 | // 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^] | 77 | CustomAccessor(C *target, |
| 78 | T(C::*getter)(), |
| 79 | void(C::*setter)(const T&, Error *)) |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 80 | : target_(target), |
| 81 | getter_(getter), |
| 82 | setter_(setter) { |
| 83 | DCHECK(target); |
| 84 | DCHECK(getter); |
| 85 | } |
| 86 | virtual ~CustomAccessor() {} |
| 87 | |
| 88 | const T &Get() { return storage_ = (target_->*getter_)(); } |
mukesh agrawal | ffa3d04 | 2011-10-06 15:26:10 -0700 | [diff] [blame^] | 89 | void Set(const T &value, Error *error) { |
| 90 | if (setter_) { |
| 91 | (target_->*setter_)(value, error); |
| 92 | } else { |
| 93 | error->Populate(Error::kInvalidArguments, "Property is read-only"); |
| 94 | } |
| 95 | } |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 96 | |
| 97 | private: |
| 98 | C * const target_; |
| 99 | // Get() returns a const&, so we need to have internal storage to which to |
| 100 | // return a reference. |
| 101 | T storage_; |
| 102 | T(C::*getter_)(void); |
mukesh agrawal | ffa3d04 | 2011-10-06 15:26:10 -0700 | [diff] [blame^] | 103 | void(C::*setter_)(const T&, Error *); |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 104 | DISALLOW_COPY_AND_ASSIGN(CustomAccessor); |
| 105 | }; |
| 106 | |
| 107 | } // namespace shill |
| 108 | |
| 109 | #endif // SHILL_PROPERTY_ACCESSOR_ |