Signals Library
connection_impl.hpp
1 #ifndef SIGNALS_DETAIL_CONNECTION_IMPL_HPP
2 #define SIGNALS_DETAIL_CONNECTION_IMPL_HPP
3 #include <memory>
4 #include <mutex>
5 #include <utility>
6 
7 #include "../connection.hpp"
8 #include "../slot.hpp"
9 #include "connection_impl_base.hpp"
10 
11 namespace sig {
12 class Connection;
13 
14 template <typename Signature>
16 
17 // Implementation class for Connection. This class owns the Slot involved in
18 // the connection. Inherits from Connection_impl_base, which implements shared
19 // connection block counts.
20 template <typename R, typename... Args>
21 class Connection_impl<R(Args...)> : public Connection_impl_base {
22  public:
23  using Extended_slot_t = Slot<R(Connection const&, Args...)>;
24 
25  public:
26  Connection_impl() : slot_{}, connected_{false} {}
27 
28  explicit Connection_impl(Slot<R(Args...)> s)
29  : slot_{std::move(s)}, connected_{true}
30  {}
31 
32  public:
33  // Constructs a Connection_impl with an extended slot and connection. This
34  // binds the connection to the first parameter of the Extended_slot_t
35  // function. The slot function type then matches the signal function type.
36  // Then copies tracked items from the extended slot into the new slot.
37  auto emplace_extended(Extended_slot_t const& es, Connection const& c)
38  -> Connection_impl&
39  {
40  auto const lock = std::lock_guard{mtx_};
41  connected_ = true;
42 
43  slot_.slot_function() = [c, es](Args&&... args) {
44  return es.slot_function()(c, std::forward<Args>(args)...);
45  };
46  for (std::weak_ptr<void> const& wp : es.get_tracked_container()) {
47  slot_.track(wp);
48  }
49  return *this;
50  }
51 
52  void disconnect() override
53  {
54  auto const lock = std::lock_guard{mtx_};
55  connected_ = false;
56  }
57 
58  auto connected() const -> bool override
59  {
60  auto const lock = std::lock_guard{mtx_};
61  return connected_;
62  }
63 
64  auto get_slot() -> Slot<R(Args...)>& { return slot_; }
65 
66  auto get_slot() const -> Slot<R(Args...)> const& { return slot_; }
67 
68  private:
69  Slot<R(Args...)> slot_;
70  bool connected_;
71 };
72 
73 } // namespace sig
74 #endif // SIGNALS_DETAIL_CONNECTION_IMPL_HPP
sig
Definition: connection.hpp:8
sig::Connection_impl_base
Definition: connection_impl_base.hpp:12
sig::Slot
Definition: slot_fwd.hpp:11
sig::Connection_impl
Definition: connection_impl.hpp:15
sig::Connection
Represents the connection made when a Slot is connected to a Signal.
Definition: connection.hpp:13