diff options
author | Christian Glöckner <cgloeckner@freenet.de> | 2016-03-31 10:12:20 +0200 |
---|---|---|
committer | Bent Bisballe Nyeng <deva@aasimon.org> | 2016-03-31 21:15:43 +0200 |
commit | afa2b578d6debc42bd4429b6efce8868b8f8fa81 (patch) | |
tree | 15d45816881889746bc5777fef2e390ada696c53 /src/syncedsettings.h | |
parent | 3ecca5323cba595fa05a599777f0b4c455bdd058 (diff) |
Cleanup on Synced Settings Header
Diffstat (limited to 'src/syncedsettings.h')
-rw-r--r-- | src/syncedsettings.h | 128 |
1 files changed, 56 insertions, 72 deletions
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 <mutex> -// type trait helper +template <typename T> class Group; -template <typename T, typename... Types> -struct pack_contains; +template <typename T> class Accessor +{ +private: + std::lock_guard<std::mutex> lock; -template <typename T, typename... Types> -struct pack_contains<T, T, Types...> - : std::true_type { -}; +public: + Accessor(Group<T>& parent) + : lock{parent.mutex} + , data{parent.data} + { + } -template <typename T, typename Head, typename... Types> -struct pack_contains<T, Head, Types...> - : pack_contains<T, Types...> { + T& data; }; -template <typename T> -struct pack_contains<T> - : std::false_type { -}; +template <typename T> class Group +{ +private: + friend class Accessor<T>; -// -------------------------------------------------------------------- + mutable std::mutex mutex; + T data; -template <typename T> -class Group; +public: + Group() + : mutex{} + , data{} + { + } -template <typename T> -class Accessor { - private: - std::lock_guard<std::mutex> lock; - - public: - Accessor(Group<T>& 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<T> const& other) + : mutex{} + , data{} + { + std::lock_guard<std::mutex> lock{other.mutex}; + data = other.data; + } + + Group(Group<T>&& other) + : mutex{} + , data{} + { + std::lock_guard<std::mutex> lock{other.mutex}; + std::swap(data, other.data); + } -template <typename T> -class Group { - private: - friend class Accessor<T>; - - 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<T> const & other) - : mutex{} - , data{} { - std::lock_guard<std::mutex> lock{other.mutex}; - data = other.data; - } - - Group(Group<T>&& other) - : mutex{} - , data{} { - std::lock_guard<std::mutex> lock{other.mutex}; - std::swap(data, other.data); - } - - operator T() const { - std::lock_guard<std::mutex> lock{mutex}; - return data; - } + operator T() const + { + std::lock_guard<std::mutex> lock{mutex}; + return data; + } }; |