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 // 16 17 #ifndef SHILL_PROPERTY_OBSERVER_H_ 18 #define SHILL_PROPERTY_OBSERVER_H_ 19 20 #include <memory> 21 22 #include <base/callback.h> 23 #include <base/macros.h> 24 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: 39 typedef base::Callback<void(const T& new_value)> Callback; 40 PropertyObserver(std::shared_ptr<AccessorInterface<T>> accessor,Callback callback)41 PropertyObserver(std::shared_ptr<AccessorInterface<T>> accessor, 42 Callback callback) 43 : accessor_(accessor), callback_(callback) { 44 Error unused_error; 45 saved_value_ = accessor_->Get(&unused_error); 46 } ~PropertyObserver()47 ~PropertyObserver() override {} 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. Update()52 void Update() override { 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 65 std::shared_ptr<AccessorInterface<T>> accessor_; 66 Callback callback_; 67 T saved_value_; 68 69 DISALLOW_COPY_AND_ASSIGN(PropertyObserver); 70 }; 71 72 } // namespace shill 73 74 #endif // SHILL_PROPERTY_OBSERVER_H_ 75