From 057ef1d83ba263fb2adf1aa86f8e281ab0065c43 Mon Sep 17 00:00:00 2001 From: Bent Bisballe Nyeng Date: Fri, 8 Apr 2016 00:15:32 +0200 Subject: Refactoring to finally get rid of MessageHandler/Receiver in favor of the new Settings mechanism. --- src/atomic.h | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) (limited to 'src/atomic.h') 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 lock{mutex}; + return other == data; + } + + bool operator!=(const T& other) const + { + std::lock_guard lock{mutex}; + return !(other == data); + } + + bool operator==(const Atomic& other) const + { + std::lock_guard lock{mutex}; + return other.load() == data; + } + + bool operator!=(const Atomic& other) const + { + std::lock_guard lock{mutex}; + return !(other.load() == data); + } + private: T data; mutable std::mutex mutex; }; + +//! Getter utility class. +template class SettingRef +{ +public: + SettingRef(Atomic& value) + : value(value) + { + // string isn't lock free either + assert((std::is_same::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& value; + Atomic cache; +}; -- cgit v1.2.3