Paul Stewart | 0153cf0 | 2014-06-02 11:53:36 -0700 | [diff] [blame] | 1 | // Copyright (c) 2014 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 | |
Ben Chan | c45688b | 2014-07-02 23:50:45 -0700 | [diff] [blame] | 5 | #ifndef SHILL_PROPERTY_OBSERVER_H_ |
| 6 | #define SHILL_PROPERTY_OBSERVER_H_ |
Paul Stewart | 0153cf0 | 2014-06-02 11:53:36 -0700 | [diff] [blame] | 7 | |
Alex Vakulenko | 8a53229 | 2014-06-16 17:18:44 -0700 | [diff] [blame] | 8 | #include <memory> |
Paul Stewart | 0153cf0 | 2014-06-02 11:53:36 -0700 | [diff] [blame] | 9 | |
Ben Chan | cc67c52 | 2014-09-03 07:19:18 -0700 | [diff] [blame] | 10 | #include <base/callback.h> |
| 11 | #include <base/macros.h> |
| 12 | |
Paul Stewart | 0153cf0 | 2014-06-02 11:53:36 -0700 | [diff] [blame] | 13 | #include "shill/accessor_interface.h" |
| 14 | #include "shill/error.h" |
| 15 | #include "shill/property_observer_interface.h" |
| 16 | |
| 17 | namespace shill { |
| 18 | |
| 19 | // A templated object that retains a reference to a typed accessor, |
| 20 | // and a saved value retrieved from the accessor. When the update |
| 21 | // method is called, it compares its saved value to the current |
| 22 | // value returned by the accessor. If the value has changed, it |
| 23 | // calls the supplied callback and updates the saved value. |
| 24 | template <class T> |
| 25 | class PropertyObserver : public PropertyObserverInterface { |
| 26 | public: |
| 27 | typedef base::Callback<void(const T &new_value)> Callback; |
| 28 | |
Alex Vakulenko | 8a53229 | 2014-06-16 17:18:44 -0700 | [diff] [blame] | 29 | PropertyObserver(std::shared_ptr<AccessorInterface<T>> accessor, |
Paul Stewart | 0153cf0 | 2014-06-02 11:53:36 -0700 | [diff] [blame] | 30 | Callback callback) |
| 31 | : accessor_(accessor), callback_(callback) { |
| 32 | Error unused_error; |
| 33 | saved_value_ = accessor_->Get(&unused_error); |
| 34 | } |
Ben Chan | 5ea763b | 2014-08-13 11:07:54 -0700 | [diff] [blame] | 35 | ~PropertyObserver() override {} |
Paul Stewart | 0153cf0 | 2014-06-02 11:53:36 -0700 | [diff] [blame] | 36 | |
| 37 | // Implements PropertyObserverInterface. Compares the saved value with |
| 38 | // what the Get() method of |accessor_| returns. If the value has changed |
| 39 | // |callback_| is invoked and |saved_value_| is updated. |
Alex Vakulenko | 016fa0e | 2014-08-11 15:59:58 -0700 | [diff] [blame] | 40 | void Update() override { |
Paul Stewart | 0153cf0 | 2014-06-02 11:53:36 -0700 | [diff] [blame] | 41 | Error error; |
| 42 | T new_value_ = accessor_->Get(&error); |
| 43 | if (!error.IsSuccess() || saved_value_ == new_value_) { |
| 44 | return; |
| 45 | } |
| 46 | callback_.Run(new_value_); |
| 47 | saved_value_ = new_value_; |
| 48 | } |
| 49 | |
| 50 | private: |
| 51 | friend class PropertyObserverTest; |
| 52 | |
Alex Vakulenko | 8a53229 | 2014-06-16 17:18:44 -0700 | [diff] [blame] | 53 | std::shared_ptr<AccessorInterface<T>> accessor_; |
Paul Stewart | 0153cf0 | 2014-06-02 11:53:36 -0700 | [diff] [blame] | 54 | Callback callback_; |
| 55 | T saved_value_; |
| 56 | |
| 57 | DISALLOW_COPY_AND_ASSIGN(PropertyObserver); |
| 58 | }; |
| 59 | |
| 60 | } // namespace shill |
| 61 | |
Ben Chan | c45688b | 2014-07-02 23:50:45 -0700 | [diff] [blame] | 62 | #endif // SHILL_PROPERTY_OBSERVER_H_ |