Signals Library
connection_impl_base.hpp
1 #ifndef SIGNALS_DETAIL_CONNECTION_IMPL_BASE_HPP
2 #define SIGNALS_DETAIL_CONNECTION_IMPL_BASE_HPP
3 #include <cstddef>
4 #include <mutex>
5 #include <utility>
6 
7 namespace sig {
8 
9 // Provides an interface for the Connection class to hold a pointer to a
10 // non-templated implementation. A Connection can remain non-templated, while
11 // having an internal implementation vary on the Slot type.
13  public:
14  virtual ~Connection_impl_base() = default;
15 
16  Connection_impl_base() = default;
17 
19  {
20  auto const lock = std::lock_guard{other.mtx_};
21  blocking_object_count_ = other.blocking_object_count_;
22  }
23 
25  {
26  auto const lock = std::lock_guard{other.mtx_};
27  blocking_object_count_ = std::move(other.blocking_object_count_);
28  }
29 
30  auto operator=(Connection_impl_base const& rhs) -> Connection_impl_base&
31  {
32  if (this != &rhs) {
33  auto lhs_lock = std::unique_lock{this->mtx_, std::defer_lock};
34  auto rhs_lock = std::unique_lock{rhs.mtx_, std::defer_lock};
35  std::lock(lhs_lock, rhs_lock);
36  blocking_object_count_ = rhs.blocking_object_count_;
37  }
38  return *this;
39  }
40 
41  auto operator=(Connection_impl_base&& rhs) -> Connection_impl_base&
42  {
43  if (this != &rhs) {
44  auto lhs_lock = std::unique_lock{this->mtx_, std::defer_lock};
45  auto rhs_lock = std::unique_lock{rhs.mtx_, std::defer_lock};
46  std::lock(lhs_lock, rhs_lock);
47  blocking_object_count_ = std::move(rhs.blocking_object_count_);
48  }
49  return *this;
50  }
51 
52  public:
53  virtual void disconnect() = 0;
54 
55  virtual auto connected() const -> bool = 0;
56 
57  auto blocked() const -> bool
58  {
59  auto const lock = std::lock_guard{mtx_};
60  return blocking_object_count_ < 1 ? false : true;
61  }
62 
63  void add_block()
64  {
65  auto const lock = std::lock_guard{mtx_};
66  ++blocking_object_count_;
67  }
68 
69  void remove_block()
70  {
71  auto const lock = std::lock_guard{mtx_};
72  --blocking_object_count_;
73  }
74 
75  protected:
76  std::size_t blocking_object_count_ = 0;
77 
78  mutable std::mutex mtx_;
79 };
80 
81 } // namespace sig
82 #endif // SIGNALS_DETAIL_CONNECTION_IMPL_BASE_HPP
sig
Definition: connection.hpp:8
sig::Connection_impl_base
Definition: connection_impl_base.hpp:12