Peter Qiu | c0beca5 | 2015-09-03 11:25:46 -0700 | [diff] [blame] | 1 | // |
| 2 | // Copyright (C) 2014 The Android Open Source Project |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | // |
Paul Stewart | 0153cf0 | 2014-06-02 11:53:36 -0700 | [diff] [blame] | 16 | |
Ben Chan | c45688b | 2014-07-02 23:50:45 -0700 | [diff] [blame] | 17 | #ifndef SHILL_PROPERTY_OBSERVER_H_ |
| 18 | #define SHILL_PROPERTY_OBSERVER_H_ |
Paul Stewart | 0153cf0 | 2014-06-02 11:53:36 -0700 | [diff] [blame] | 19 | |
Alex Vakulenko | 8a53229 | 2014-06-16 17:18:44 -0700 | [diff] [blame] | 20 | #include <memory> |
Paul Stewart | 0153cf0 | 2014-06-02 11:53:36 -0700 | [diff] [blame] | 21 | |
Ben Chan | cc67c52 | 2014-09-03 07:19:18 -0700 | [diff] [blame] | 22 | #include <base/callback.h> |
| 23 | #include <base/macros.h> |
| 24 | |
Paul Stewart | 0153cf0 | 2014-06-02 11:53:36 -0700 | [diff] [blame] | 25 | #include "shill/accessor_interface.h" |
| 26 | #include "shill/error.h" |
| 27 | #include "shill/property_observer_interface.h" |
| 28 | |
| 29 | namespace shill { |
| 30 | |
| 31 | // A templated object that retains a reference to a typed accessor, |
| 32 | // and a saved value retrieved from the accessor. When the update |
| 33 | // method is called, it compares its saved value to the current |
| 34 | // value returned by the accessor. If the value has changed, it |
| 35 | // calls the supplied callback and updates the saved value. |
| 36 | template <class T> |
| 37 | class PropertyObserver : public PropertyObserverInterface { |
| 38 | public: |
Paul Stewart | 1a212a6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 39 | typedef base::Callback<void(const T& new_value)> Callback; |
Paul Stewart | 0153cf0 | 2014-06-02 11:53:36 -0700 | [diff] [blame] | 40 | |
Alex Vakulenko | 8a53229 | 2014-06-16 17:18:44 -0700 | [diff] [blame] | 41 | PropertyObserver(std::shared_ptr<AccessorInterface<T>> accessor, |
Paul Stewart | 0153cf0 | 2014-06-02 11:53:36 -0700 | [diff] [blame] | 42 | Callback callback) |
| 43 | : accessor_(accessor), callback_(callback) { |
| 44 | Error unused_error; |
| 45 | saved_value_ = accessor_->Get(&unused_error); |
| 46 | } |
Ben Chan | 5ea763b | 2014-08-13 11:07:54 -0700 | [diff] [blame] | 47 | ~PropertyObserver() override {} |
Paul Stewart | 0153cf0 | 2014-06-02 11:53:36 -0700 | [diff] [blame] | 48 | |
| 49 | // Implements PropertyObserverInterface. Compares the saved value with |
| 50 | // what the Get() method of |accessor_| returns. If the value has changed |
| 51 | // |callback_| is invoked and |saved_value_| is updated. |
Alex Vakulenko | 016fa0e | 2014-08-11 15:59:58 -0700 | [diff] [blame] | 52 | void Update() override { |
Paul Stewart | 0153cf0 | 2014-06-02 11:53:36 -0700 | [diff] [blame] | 53 | Error error; |
| 54 | T new_value_ = accessor_->Get(&error); |
| 55 | if (!error.IsSuccess() || saved_value_ == new_value_) { |
| 56 | return; |
| 57 | } |
| 58 | callback_.Run(new_value_); |
| 59 | saved_value_ = new_value_; |
| 60 | } |
| 61 | |
| 62 | private: |
| 63 | friend class PropertyObserverTest; |
| 64 | |
Alex Vakulenko | 8a53229 | 2014-06-16 17:18:44 -0700 | [diff] [blame] | 65 | std::shared_ptr<AccessorInterface<T>> accessor_; |
Paul Stewart | 0153cf0 | 2014-06-02 11:53:36 -0700 | [diff] [blame] | 66 | Callback callback_; |
| 67 | T saved_value_; |
| 68 | |
| 69 | DISALLOW_COPY_AND_ASSIGN(PropertyObserver); |
| 70 | }; |
| 71 | |
| 72 | } // namespace shill |
| 73 | |
Ben Chan | c45688b | 2014-07-02 23:50:45 -0700 | [diff] [blame] | 74 | #endif // SHILL_PROPERTY_OBSERVER_H_ |