27#ifndef EASY3D_CORE_SIGNAL_H
28#define EASY3D_CORE_SIGNAL_H
32#include <unordered_map>
55 template<
typename... Args>
67 slots_(std::move(other.slots_)),
68 current_id_(other.current_id_) {
74 slots_ = std::move(other.slots_);
75 current_id_ = other.current_id_;
97 int connect(std::function<
void(Args...)>
const &slot) {
98 slots_.insert(std::make_pair(++current_id_, slot));
114 template<
typename Class>
115 int connect(Class *inst,
void (Class::*func)(Args...)) {
116 return connect([=](Args... args) {
117 (inst->*func)(args...);
133 template<
typename Class>
134 int connect(Class *inst,
void (Class::*func)(Args...)
const) {
135 return connect([=](Args... args) {
136 (inst->*func)(args...);
161 for (
auto const &it : slots_) {
168 for (
auto const &it : slots_) {
169 if (it.first != excludedConnectionID) {
177 auto const &it = slots_.find(connectionID);
178 if (it != slots_.end()) {
184 std::unordered_map<int, std::function<void(Args...)>> slots_;
201 template<
typename SIGNAL,
typename FUNCTION>
202 inline int connect(SIGNAL *signal, FUNCTION
const &slot) {
203 return signal->connect(slot);
218 template<
typename SIGNAL,
typename CLASS,
typename FUNCTION>
219 inline int connect(SIGNAL *signal, CLASS *inst, FUNCTION
const &slot) {
220 return signal->connect(inst, slot);
228 template<
typename... Args>
230 return sender->
connect(receiver);
234 template<
typename SIGNAL>
236 signal->disconnect(
id);
240 template<
typename SIGNAL>
242 signal->disconnect_all();
263 template <
typename... Args>
264 inline constexpr auto overload(
void (*func)(Args...)) -> void (*)(Args...) {
286 template <
typename C,
typename... Args>
287 inline constexpr auto overload(
void (C::*func)(Args...)) -> void (C::*)(Args...) {
A light-weight implementation of the simple signal-slot mechanism.
Definition: signal.h:56
void disconnect_all()
Disconnects all previously connected functions.
Definition: signal.h:155
void send_for_all_but_one(int excludedConnectionID, Args... p)
Calls all connected functions except for one.
Definition: signal.h:167
int connect(Class *inst, void(Class::*func)(Args...) const)
Connects a const member function of an object to this signal.
Definition: signal.h:134
void emit_for(int connectionID, Args... p)
Calls only one connected function.
Definition: signal.h:176
int connect(std::function< void(Args...)> const &slot)
Connects a function to this signal.
Definition: signal.h:97
void send(Args... p)
Calls all connected functions.
Definition: signal.h:160
Signal(Signal const &)
Copy constructor and assignment create a new signal.
Definition: signal.h:63
int connect(Signal< Args... > *receiver)
Connects this signal to another signal receiver.
Definition: signal.h:145
Signal(Signal &&other) noexcept
Move constructor.
Definition: signal.h:66
Signal & operator=(Signal &&other) noexcept
The assignment operator.
Definition: signal.h:72
int connect(Class *inst, void(Class::*func)(Args...))
Connects a member function of an object to this signal.
Definition: signal.h:115
void disconnect(int id)
Disconnects a previously connected function.
Definition: signal.h:150
Signal & operator=(Signal const &other)
The assignment operator.
Definition: signal.h:81
Definition: collider.cpp:182
void disconnect(SIGNAL *signal, int id)
Disconnects a previously connected function.
Definition: signal.h:235
int connect(SIGNAL *signal, FUNCTION const &slot)
Connects a function to the signal.
Definition: signal.h:202
void disconnect_all(SIGNAL *signal)
Disconnects all previously connected functions.
Definition: signal.h:241
constexpr auto overload(void(*func)(Args...)) -> void(*)(Args...)
Helper function for resolving overloaded non-member functions.
Definition: signal.h:264