CFx SDK Documentation  2020SP3
connection.h
Go to the documentation of this file.
1 //
2 // connection.h
3 // slimsig
4 //
5 // Created by Christopher Tarquini on 4/21/14.
6 //
7 //
8 
9 #ifndef slimsig_connection_h
10 #define slimsig_connection_h
11 #include <memory>
12 #include <functional>
13 #include <algorithm>
14 #include <limits>
15 #include "detail/slot.h"
16 
17 namespace slimsig {
18 template <class Signal>
19 class connection;
20 
21 template <class ThreadPolicy, class Allocator, class F>
23  // detail
24 
25  template <class Signal>
26  class connection {
27  using slot = typename Signal::slot;
28  using slot_type = slot;
29  using slot_list_iterator = typename Signal::slot_list::iterator;
30  using slot_storage = typename Signal::slot_list;
31  using slot_id = typename slot::slot_id;
32  using signal_holder = typename Signal::signal_holder;
33  connection(std::weak_ptr<signal_holder> slots, const slot_type& slot) : connection(std::move(slots), slot.m_slot_id) {};
34  connection(std::weak_ptr<signal_holder> slots, unsigned long long slot_id) : m_slots(std::move(slots)), m_slot_id(slot_id) {};
35  public:
36  connection() {}; // empty connection
37  connection(const connection& other) : m_slots(other.m_slots), m_slot_id(other.m_slot_id) {};
38  connection(connection&& other) : m_slots(std::move(other.m_slots)), m_slot_id(other.m_slot_id) {};
39 
41  this->swap(rhs);
42  return *this;
43  }
45  m_slot_id = rhs.m_slot_id;
46  m_slots = rhs.m_slots;
47  return *this;
48  }
49 
50  void swap(connection& other) {
51  using std::swap;
52  swap(m_slots, other.m_slots);
53  swap(m_slot_id, other.m_slot_id);
54  }
56  explicit operator bool() const { return connected(); };
57  bool connected() const {
58  const auto slots = m_slots.lock();
59  if (slots && slots->signal != nullptr) {
60  return slots->signal->connected(m_slot_id);
61  //auto slot = (*slots)->find(m_slot_id);
62  //return slot != (*slots)->cend() ? slot->connected() : false;
63  }
64  return false;
65  }
66 
67  void disconnect() {
68  std::shared_ptr<signal_holder> slots = m_slots.lock();
69  if (slots != nullptr && slots->signal != nullptr) {
70  slots->signal->disconnect(m_slot_id);
71  //auto slot = *slots->find(m_slot_id);
72  //if (slot != slots->end()) slot->disconnect();
73  }
74  }
75  template <class ThreadPolicy, class Allocator, class F>
76  friend class signal_base;
77  template < class T, class IDGenerator, class FlagType, class Allocator>
78  friend class slot_list;
79 
80 
81  private:
82  std::weak_ptr<signal_holder> m_slots;
83  slot_id m_slot_id;
84  };
85 
86  template <class connection>
88  public:
89  scoped_connection() : m_connection() {};
90  scoped_connection(const connection& target) : m_connection(target) {};
92  scoped_connection(scoped_connection&& other) : m_connection(other.m_connection) {};
93 
95  m_connection = rhs;
96  };
98  this->swap(rhs);
99  return *this;
100  };
101  void swap(scoped_connection& other) {
102  m_connection.swap(other.m_connection);
103  };
105  connection ret{};
106  m_connection.swap(ret);
107  return ret;
108  }
110  m_connection.disconnect();
111  }
112  private:
113  connection m_connection;
114  };
115 
116  template <class connection>
118  return scoped_connection<connection>(std::forward<connection>(target));
119  };
120 
121 }
122 
123 #endif
slimsig::scoped_connection::scoped_connection
scoped_connection(const connection &target)
Definition: connection.h:90
slimsig::scoped_connection::operator=
scoped_connection & operator=(scoped_connection &&rhs)
Definition: connection.h:97
slimsig::scoped_connection::scoped_connection
scoped_connection(const scoped_connection &)=delete
slimsig::slot_list
Definition: slot.h:156
slimsig::connection::connected
bool connected() const
Definition: connection.h:57
slimsig::connection::operator=
connection & operator=(connection &&rhs)
Definition: connection.h:40
slimsig::scoped_connection::~scoped_connection
~scoped_connection()
Definition: connection.h:109
slimsig::make_scoped_connection
scoped_connection< connection > make_scoped_connection(connection &&target)
Definition: connection.h:117
slimsig::connection::disconnect
void disconnect()
Definition: connection.h:67
slimsig
Definition: connection.h:17
slimsig::swap
void swap(signal< Handler, SignalTraits, Allocator > &lhs, signal< Handler, SignalTraits, Allocator > &rhs)
slimsig::connection::operator=
connection & operator=(const connection &rhs)
Definition: connection.h:44
slimsig::scoped_connection::scoped_connection
scoped_connection(scoped_connection &&other)
Definition: connection.h:92
slimsig::signal_base
Definition: connection.h:22
slimsig::scoped_connection::release
connection release()
Definition: connection.h:104
slimsig::connection::swap
void swap(connection &other)
Definition: connection.h:50
slimsig::scoped_connection::operator=
scoped_connection & operator=(const connection &rhs)
Definition: connection.h:94
_FORCE_INLINE
#define _FORCE_INLINE
Definition: slot.h:23
slimsig::connection
Definition: connection.h:26
slot.h
slimsig::connection::connection
connection()
Definition: connection.h:36
slimsig::connection::connection
connection(const connection &other)
Definition: connection.h:37
slimsig::scoped_connection
Definition: connection.h:87
slimsig::scoped_connection::scoped_connection
scoped_connection()
Definition: connection.h:89
slimsig::scoped_connection::swap
void swap(scoped_connection &other)
Definition: connection.h:101
slimsig::connection::connection
connection(connection &&other)
Definition: connection.h:38