Overload == and != comparison operators for ObservableValue
#include "observable.h"
#include "valueproducer.h"
-///////////////////
-// ObservableValue is simply a value that notifies its observers if
-// it gets changed.
+// forward declaration for the operator overloading functions
+template <class T>
+class ObservableValue;
+template <class T>
+bool operator==(ObservableValue<T> const& lhs, T const& rhs) {
+ return lhs.output == rhs;
+}
+
+template <class T>
+bool operator!=(ObservableValue<T> const& lhs, T const& rhs) {
+ return lhs.output != rhs;
+}
+
+/*
+ * ObservableValue is a value that notifies its observers if it gets changed.
+ */
template <class T>
class ObservableValue : public ValueProducer<T> {
public:
@@ -15,14 +28,17 @@ class ObservableValue : public ValueProducer<T> {
ObservableValue(const T& value) { ValueProducer<T>::output = value; }
- void set(const T& value) {
- this->ValueProducer<T>::emit(value);
- }
+ void set(const T& value) { this->ValueProducer<T>::emit(value); }
const T& operator=(const T& value) {
set(value);
return value;
}
+
+ template <class U>
+ friend bool operator==(ObservableValue<U> const& lhs, U const& rhs);
+ template <class U>
+ friend bool operator!=(ObservableValue<U> const& lhs, U const& rhs);
};
#endif
