blob: d40571db11c71e35a0d6a96a1b1a3e419df85f80 [file] [log] [blame]
Peter Qiuc0beca52015-09-03 11:25:46 -07001//
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 Stewart0153cf02014-06-02 11:53:36 -070016
Ben Chanc45688b2014-07-02 23:50:45 -070017#ifndef SHILL_PROPERTY_OBSERVER_H_
18#define SHILL_PROPERTY_OBSERVER_H_
Paul Stewart0153cf02014-06-02 11:53:36 -070019
Alex Vakulenko8a532292014-06-16 17:18:44 -070020#include <memory>
Paul Stewart0153cf02014-06-02 11:53:36 -070021
Ben Chancc67c522014-09-03 07:19:18 -070022#include <base/callback.h>
23#include <base/macros.h>
24
Paul Stewart0153cf02014-06-02 11:53:36 -070025#include "shill/accessor_interface.h"
26#include "shill/error.h"
27#include "shill/property_observer_interface.h"
28
29namespace 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.
36template <class T>
37class PropertyObserver : public PropertyObserverInterface {
38 public:
Paul Stewart1a212a62015-06-16 13:13:10 -070039 typedef base::Callback<void(const T& new_value)> Callback;
Paul Stewart0153cf02014-06-02 11:53:36 -070040
Alex Vakulenko8a532292014-06-16 17:18:44 -070041 PropertyObserver(std::shared_ptr<AccessorInterface<T>> accessor,
Paul Stewart0153cf02014-06-02 11:53:36 -070042 Callback callback)
43 : accessor_(accessor), callback_(callback) {
44 Error unused_error;
45 saved_value_ = accessor_->Get(&unused_error);
46 }
Ben Chan5ea763b2014-08-13 11:07:54 -070047 ~PropertyObserver() override {}
Paul Stewart0153cf02014-06-02 11:53:36 -070048
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 Vakulenko016fa0e2014-08-11 15:59:58 -070052 void Update() override {
Paul Stewart0153cf02014-06-02 11:53:36 -070053 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 Vakulenko8a532292014-06-16 17:18:44 -070065 std::shared_ptr<AccessorInterface<T>> accessor_;
Paul Stewart0153cf02014-06-02 11:53:36 -070066 Callback callback_;
67 T saved_value_;
68
69 DISALLOW_COPY_AND_ASSIGN(PropertyObserver);
70};
71
72} // namespace shill
73
Ben Chanc45688b2014-07-02 23:50:45 -070074#endif // SHILL_PROPERTY_OBSERVER_H_