Sunshine latest
Self-hosted game stream host for Moonlight.
sync.h
Go to the documentation of this file.
1
5#pragma once
6
7// standard includes
8#include <array>
9#include <mutex>
10#include <utility>
11
12namespace sync_util {
13
14 template<class T, class M = std::mutex>
15 class sync_t {
16 public:
17 using value_t = T;
18 using mutex_t = M;
19
20 std::lock_guard<mutex_t> lock() {
21 return std::lock_guard {_lock};
22 }
23
24 template<class... Args>
25 sync_t(Args &&...args):
26 raw {std::forward<Args>(args)...} {
27 }
28
29 sync_t &operator=(sync_t &&other) noexcept {
30 std::lock(_lock, other._lock);
31
32 raw = std::move(other.raw);
33
34 _lock.unlock();
35 other._lock.unlock();
36
37 return *this;
38 }
39
40 sync_t &operator=(sync_t &other) noexcept {
41 std::lock(_lock, other._lock);
42
43 raw = other.raw;
44
45 _lock.unlock();
46 other._lock.unlock();
47
48 return *this;
49 }
50
51 template<class V>
52 sync_t &operator=(V &&val) {
53 auto lg = lock();
54
55 raw = val;
56
57 return *this;
58 }
59
60 sync_t &operator=(const value_t &val) noexcept {
61 auto lg = lock();
62
63 raw = val;
64
65 return *this;
66 }
67
68 sync_t &operator=(value_t &&val) noexcept {
69 auto lg = lock();
70
71 raw = std::move(val);
72
73 return *this;
74 }
75
76 value_t *operator->() {
77 return &raw;
78 }
79
80 value_t &operator*() {
81 return raw;
82 }
83
84 const value_t &operator*() const {
85 return raw;
86 }
87
88 value_t raw;
89
90 private:
91 mutex_t _lock;
92 };
93
94} // namespace sync_util
Definition sync.h:15
Functions for handling command line arguments.
Definition entry_handler.cpp:39