summaryrefslogtreecommitdiff
path: root/src/atomic.h
diff options
context:
space:
mode:
authorBent Bisballe Nyeng <deva@aasimon.org>2016-04-08 00:15:32 +0200
committerBent Bisballe Nyeng <deva@aasimon.org>2016-04-08 00:15:32 +0200
commit057ef1d83ba263fb2adf1aa86f8e281ab0065c43 (patch)
tree31c1f237e3a9df9fc241d87527f7dd4245665d43 /src/atomic.h
parentb530ac02af61e320e137a392decef1b3cc5af2c4 (diff)
Refactoring to finally get rid of MessageHandler/Receiver in favor of the new Settings mechanism.
Diffstat (limited to 'src/atomic.h')
-rw-r--r--src/atomic.h60
1 files changed, 60 insertions, 0 deletions
diff --git a/src/atomic.h b/src/atomic.h
index e0b80b1..95a5e9d 100644
--- a/src/atomic.h
+++ b/src/atomic.h
@@ -116,7 +116,67 @@ public:
return data;
}
+ bool operator==(const T& other) const
+ {
+ std::lock_guard<std::mutex> lock{mutex};
+ return other == data;
+ }
+
+ bool operator!=(const T& other) const
+ {
+ std::lock_guard<std::mutex> lock{mutex};
+ return !(other == data);
+ }
+
+ bool operator==(const Atomic<T>& other) const
+ {
+ std::lock_guard<std::mutex> lock{mutex};
+ return other.load() == data;
+ }
+
+ bool operator!=(const Atomic<T>& other) const
+ {
+ std::lock_guard<std::mutex> lock{mutex};
+ return !(other.load() == data);
+ }
+
private:
T data;
mutable std::mutex mutex;
};
+
+//! Getter utility class.
+template <typename T> class SettingRef
+{
+public:
+ SettingRef(Atomic<T>& value)
+ : value(value)
+ {
+ // string isn't lock free either
+ assert((std::is_same<T, std::string>::value || value.is_lock_free()));
+ }
+
+ bool hasChanged()
+ {
+ T tmp = cache;
+ cache.exchange(value);
+
+ if(firstAccess)
+ {
+ firstAccess = false;
+ return true;
+ }
+
+ return cache != tmp;
+ }
+
+ T getValue() const
+ {
+ return cache;
+ }
+
+private:
+ bool firstAccess{true};
+ Atomic<T>& value;
+ Atomic<T> cache;
+};