shill: PropertyChangeNotifier: Initial commit

Add a PropertyObserver, whose task is to track changes
to the returned value of an accessor.  Further, create
a ServicePropertyChangeNotifier, whose task is to monitor
a collection of PropertyObservers for a Service and call
the appropriate Emit*Property method for those properties
which have changed.

BUG=chromium:379948
TEST=Unit tests
Change-Id: I450b74b388f8ecb44010f277cc77149412b24950
Reviewed-on: https://chromium-review.googlesource.com/202441
Reviewed-by: mukesh agrawal <quiche@chromium.org>
Commit-Queue: Paul Stewart <pstew@chromium.org>
Tested-by: Paul Stewart <pstew@chromium.org>
diff --git a/property_observer.h b/property_observer.h
new file mode 100644
index 0000000..e024cdc
--- /dev/null
+++ b/property_observer.h
@@ -0,0 +1,61 @@
+// Copyright (c) 2014 The Chromium OS Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef SHILL_PROPERTY_OBSERVER_
+#define SHILL_PROPERTY_OBSERVER_
+
+#include <base/basictypes.h>
+#include <base/callback.h>
+#include <tr1/memory>
+
+#include "shill/accessor_interface.h"
+#include "shill/error.h"
+#include "shill/property_observer_interface.h"
+
+namespace shill {
+
+// A templated object that retains a reference to a typed accessor,
+// and a saved value retrieved from the accessor.  When the update
+// method is called, it compares its saved value to the current
+// value returned by the accessor.  If the value has changed, it
+// calls the supplied callback and updates the saved value.
+template <class T>
+class PropertyObserver : public PropertyObserverInterface {
+ public:
+  typedef base::Callback<void(const T &new_value)> Callback;
+
+  PropertyObserver(std::tr1::shared_ptr<AccessorInterface<T>> accessor,
+                   Callback callback)
+      : accessor_(accessor), callback_(callback) {
+    Error unused_error;
+    saved_value_ = accessor_->Get(&unused_error);
+  }
+  virtual ~PropertyObserver() {}
+
+  // Implements PropertyObserverInterface.  Compares the saved value with
+  // what the Get() method of |accessor_| returns.  If the value has changed
+  // |callback_| is invoked and |saved_value_| is updated.
+  virtual void Update() override {
+    Error error;
+    T new_value_ = accessor_->Get(&error);
+    if (!error.IsSuccess() || saved_value_ == new_value_) {
+      return;
+    }
+    callback_.Run(new_value_);
+    saved_value_ = new_value_;
+  }
+
+ private:
+  friend class PropertyObserverTest;
+
+  std::tr1::shared_ptr<AccessorInterface<T>> accessor_;
+  Callback callback_;
+  T saved_value_;
+
+  DISALLOW_COPY_AND_ASSIGN(PropertyObserver);
+};
+
+}  // namespace shill
+
+#endif  // SHILL_PROPERTY_OBSERVER_