Signals Library
shared_connection_block.hpp
1 #ifndef SIGNALS_SHARED_CONNECTION_BLOCK_HPP
2 #define SIGNALS_SHARED_CONNECTION_BLOCK_HPP
3 #include <memory>
4 
5 #include "connection.hpp"
6 
7 namespace sig {
8 class Connection_impl_base;
9 
11 
14  public:
16 
21  explicit Shared_connection_block(Connection const& conn = Connection{},
22  bool initially_block = true)
23  : connection_{conn.pimpl_}, blocking_{initially_block}
24  {
25  if (this->active())
26  connection_.lock()->add_block();
27  }
28 
30 
32  : connection_{x.connection_}, blocking_{x.blocking_}
33  {
34  if (this->active())
35  connection_.lock()->add_block();
36  }
37 
39 
43  {
44  if (this == &x)
45  return *this;
46  this->reset(x);
47  return *this;
48  }
49 
51 
53 
55  void unblock()
56  {
57  if (this->active()) {
58  connection_.lock()->remove_block();
59  blocking_ = false;
60  }
61  }
62 
64  void block()
65  {
66  if (!connection_.expired() && !blocking_) {
67  connection_.lock()->add_block();
68  blocking_ = true;
69  }
70  }
71 
73  auto blocking() const -> bool
74  {
75  return !connection_.expired() && blocking_;
76  }
77 
79  auto connection() const -> Connection { return Connection(connection_); }
80 
81  private:
82  std::weak_ptr<Connection_impl_base> connection_;
83  bool blocking_;
84 
85  private:
86  // Remove the block to the associated Connection and reset with the contents
87  // of \p x, applying a block to the new Connection if x is blocking.
88  void reset(Shared_connection_block const& x)
89  {
90  this->unblock();
91  connection_ = x.connection_;
92  blocking_ = x.blocking_;
93  if (this->active())
94  connection_.lock()->add_block();
95  }
96 
97  // Return true if the connection pointed to is still alive and *this is
98  // currently blocking.
99  auto active() -> bool { return !connection_.expired() && blocking_; }
100 };
101 
102 } // namespace sig
103 #endif // SIGNALS_SHARED_CONNECTION_BLOCK_HPP
sig::Shared_connection_block::Shared_connection_block
Shared_connection_block(Connection const &conn=Connection{}, bool initially_block=true)
Create a Shared_connection_block from a Connection and a boolean.
Definition: shared_connection_block.hpp:21
sig::Shared_connection_block
Blocks a Signal/Slot Connection.
Definition: shared_connection_block.hpp:13
sig
Definition: connection.hpp:8
sig::Shared_connection_block::block
void block()
Reasserts a block on a Connection. No-op if currently blocking.
Definition: shared_connection_block.hpp:64
sig::Shared_connection_block::blocking
auto blocking() const -> bool
Definition: shared_connection_block.hpp:73
sig::Shared_connection_block::Shared_connection_block
Shared_connection_block(Shared_connection_block const &x)
Creates a copy of x, increasing the block count on the.
Definition: shared_connection_block.hpp:31
sig::Shared_connection_block::connection
auto connection() const -> Connection
Definition: shared_connection_block.hpp:79
sig::Shared_connection_block::unblock
void unblock()
Releases the Connection block. No-op if not currently blocking.
Definition: shared_connection_block.hpp:55
sig::Shared_connection_block::~Shared_connection_block
~Shared_connection_block()
Releases this block from the Connection.
Definition: shared_connection_block.hpp:52
sig::Shared_connection_block::operator=
auto operator=(Shared_connection_block const &x) -> Shared_connection_block &
Reset *this' Connection to point to x's Connection.
Definition: shared_connection_block.hpp:42
sig::Connection
Represents the connection made when a Slot is connected to a Signal.
Definition: connection.hpp:13