cpp-mate  0.7
Helpful library for C++.
SharedState.hpp
1 /*
2  * Copyright (C) 2021 Alexander Kornilov (akornilov.82@gmail.com)
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef CPP_MATE_SHARED_STATE_HPP
18 #define CPP_MATE_SHARED_STATE_HPP
19 
20 #include <functional>
21 #include <mutex>
22 #include <condition_variable>
23 
24 #if __cplusplus >= 201703L
25 #include <shared_mutex>
26 #endif // __cplusplus >= 201703L
27 
28 namespace CppMate {
29 
33 template<typename T>
34 class SharedState final
35 {
36 public:
37  class Action;
38 
43  template<typename... A, typename = std::enable_if_t<std::is_constructible_v<T, A...>>>
44  inline SharedState(A&&... args): _state(std::forward<A>(args)...) {}
45 
50  inline void view(std::function<void(const T&)> block) const {
51  LockRead lock(_mutex);
52  block(_state);
53  }
54 
60  template<typename R>
61  inline R view(std::function<R(const T&)> block) const {
62  LockRead lock(_mutex);
63  return block(_state);
64  }
65 
70  inline void modify(std::function<void(T&)> block) {
71  LockWriteGuard lock(_mutex);
72  block(_state);
73  }
74 
79  inline Action modify() {
80  return Action(*this);
81  }
82 
83 private:
84 #if __cplusplus >= 201703L
85  using Mutex = std::shared_mutex;
86  using LockRead = std::shared_lock<Mutex>;
87  using LockWriteGuard = std::unique_lock<Mutex>;
88  using ConditionVariable = std::condition_variable_any;
89 
90 #else
91  using Mutex = std::mutex;
92  using LockRead = std::lock_guard<Mutex>;
93  using LockWriteGuard = std::lock_guard<Mutex>;
94  using ConditionVariable = std::condition_variable;
95 #endif // __cplusplus >= 201703L
96 
97  using LockWrite = std::unique_lock<Mutex>;
98 
99  T _state;
100  mutable Mutex _mutex;
101  ConditionVariable _notify;
102 };
103 
107 template <typename T>
109 {
110 public:
111 
116  void access(std::function<void(T&)> block) {
117  return block(_parent._state);
118  }
119 
125  template<typename R>
126  inline R extract(std::function<R(T&)> block) {
127  return block(_parent._state);
128  }
129 
134  inline void notifyOne(std::function<void(T&)> block) {
135  block(_parent._state);
136  _parent._notify.notify_one();
137  }
138 
144  template<typename R>
145  inline R notifyOne(std::function<R(T&)> block) {
146  R result = block(_parent._state);
147  _parent._notify.notify_one();
148  return result;
149  }
150 
155  inline void notifyAll(std::function<void(T&)> block) {
156  block(_parent._state);
157  _parent._notify.notify_all();
158  }
159 
165  template<typename R>
166  inline R notifyAll(std::function<R(T&)> block) {
167  R result = block(_parent._state);
168  _parent._notify.notify_all();
169  return result;
170  }
171 
177  inline Action& when(std::function<bool(T&)> pred) {
178  _parent._notify.wait(_lock, [&pred, this] { return pred(_parent._state); });
179  return *this;
180  }
181 
182 private:
183  friend class SharedState;
184 
185  inline Action(SharedState& parent):
186  _parent(parent),
187  _lock(_parent._mutex)
188  {
189  }
190 
191  SharedState& _parent;
192  LockWrite _lock;
193 };
194 
195 } // namespace CppMate
196 
197 #endif // CPP_MATE_SHARED_STATE_HPP
Represents possible actions for modification of the state.
Definition: SharedState.hpp:109
R extract(std::function< R(T &)> block)
Provides read and write access to state within the block and returns a value.
Definition: SharedState.hpp:126
friend class SharedState
Definition: SharedState.hpp:183
void notifyAll(std::function< void(T &)> block)
Provides read and write access to state within the block and notifies all threads waiting for state c...
Definition: SharedState.hpp:155
Action & when(std::function< bool(T &)> pred)
Waits till state will satisfy predicate condition.
Definition: SharedState.hpp:177
R notifyAll(std::function< R(T &)> block)
Provides read and write access to state within the block and notifies all threads waiting for state c...
Definition: SharedState.hpp:166
void notifyOne(std::function< void(T &)> block)
Provides read and write access to state within the block and notifies one thread waiting for state ch...
Definition: SharedState.hpp:134
void access(std::function< void(T &)> block)
Provides read and write access to state within the block.
Definition: SharedState.hpp:116
R notifyOne(std::function< R(T &)> block)
Provides read and write access to state within the block and notifies one thread waiting for state ch...
Definition: SharedState.hpp:145
Represents shared state wrapper for thread pool tasks.
Definition: SharedState.hpp:35
R view(std::function< R(const T &)> block) const
Provides read access to state within a block and return value.
Definition: SharedState.hpp:61
void modify(std::function< void(T &)> block)
Provides read and write access to state within a block.
Definition: SharedState.hpp:70
SharedState(A &&... args)
Constructor.
Definition: SharedState.hpp:44
Action modify()
Provides read and write actions with state.
Definition: SharedState.hpp:79
void view(std::function< void(const T &)> block) const
Provides read access to state within a block.
Definition: SharedState.hpp:50
Definition: BinaryData.hpp:28