Kagome
Polkadot Runtime Engine in C++17
safe_object.hpp
Go to the documentation of this file.
1 
6 #ifndef KAGOME_SAFE_OBJECT_HPP
7 #define KAGOME_SAFE_OBJECT_HPP
8 
9 #include <mutex>
10 #include <shared_mutex>
11 
12 // clang-format off
36 // clang-format on
37 template <typename T, typename M = std::shared_mutex>
38 struct SafeObject {
39  template <typename... Args>
40  SafeObject(Args &&...args) : t_(std::forward<Args>(args)...) {}
41 
42  template <typename F>
43  inline auto exclusiveAccess(F &&f) {
44  std::unique_lock lock(cs_);
45  return std::forward<F>(f)(t_);
46  }
47 
48  template <typename F>
49  inline auto sharedAccess(F &&f) const {
50  std::shared_lock lock(cs_);
51  return std::forward<F>(f)(t_);
52  }
53 
54  private:
55  T t_;
56  mutable M cs_;
57 };
58 
59 #endif // KAGOME_SAFE_OBJECT_HPP
SafeObject(Args &&...args)
Definition: safe_object.hpp:40
STL namespace.
auto sharedAccess(F &&f) const
Definition: safe_object.hpp:49
auto exclusiveAccess(F &&f)
Definition: safe_object.hpp:43