From afa2b578d6debc42bd4429b6efce8868b8f8fa81 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Gl=C3=B6ckner?= Date: Thu, 31 Mar 2016 10:12:20 +0200 Subject: Cleanup on Synced Settings Header --- src/syncedsettings.h | 128 ++++++++++++++++++++++----------------------------- 1 file changed, 56 insertions(+), 72 deletions(-) (limited to 'src') diff --git a/src/syncedsettings.h b/src/syncedsettings.h index f83847c..eb8e66e 100644 --- a/src/syncedsettings.h +++ b/src/syncedsettings.h @@ -28,85 +28,69 @@ #include -// type trait helper +template class Group; -template -struct pack_contains; +template class Accessor +{ +private: + std::lock_guard lock; -template -struct pack_contains - : std::true_type { -}; +public: + Accessor(Group& parent) + : lock{parent.mutex} + , data{parent.data} + { + } -template -struct pack_contains - : pack_contains { + T& data; }; -template -struct pack_contains - : std::false_type { -}; +template class Group +{ +private: + friend class Accessor; -// -------------------------------------------------------------------- + mutable std::mutex mutex; + T data; -template -class Group; +public: + Group() + : mutex{} + , data{} + { + } -template -class Accessor { - private: - std::lock_guard lock; - - public: - Accessor(Group& parent) - : lock{parent.mutex} - , data{parent.data} { - } - - T& data; -}; + Group(T const& data) + : mutex{} + , data{data} + { + } + + Group(T&& data) + : mutex{} + , data{std::move(data)} + { + } + + Group(Group const& other) + : mutex{} + , data{} + { + std::lock_guard lock{other.mutex}; + data = other.data; + } + + Group(Group&& other) + : mutex{} + , data{} + { + std::lock_guard lock{other.mutex}; + std::swap(data, other.data); + } -template -class Group { - private: - friend class Accessor; - - mutable std::mutex mutex; - T data; - - public: - Group() - : mutex{} - , data{} { - } - - Group(T const & data) - : mutex{} - , data{data} { - } - - Group(T&& data) - : mutex{} - , data{std::move(data)} { - } - - Group(Group const & other) - : mutex{} - , data{} { - std::lock_guard lock{other.mutex}; - data = other.data; - } - - Group(Group&& other) - : mutex{} - , data{} { - std::lock_guard lock{other.mutex}; - std::swap(data, other.data); - } - - operator T() const { - std::lock_guard lock{mutex}; - return data; - } + operator T() const + { + std::lock_guard lock{mutex}; + return data; + } }; -- cgit v1.2.3