semf
slot.h
Go to the documentation of this file.
1
11#ifndef SEMF_UTILS_CORE_SIGNALS_SLOT_H_
12#define SEMF_UTILS_CORE_SIGNALS_SLOT_H_
13
15#include <utility>
16
21#define SEMF_SLOT_FUNC(function) [](auto& obj, auto&&... args) { obj.function(std::forward<decltype(args)>(args)...); }
30#define SEMF_SLOT(name, className, object, function, ...) semf::Slot<className, ##__VA_ARGS__> name = {object, SEMF_SLOT_FUNC(function)}
31
32namespace semf
33{
41template <typename T, typename... Arguments>
42class Slot : public SlotBase<Arguments...>
43{
44public:
46 typedef void (*Function)(T&, Arguments&&...);
53 : m_object(&object),
54 m_function(function)
55 {
56 }
57 virtual ~Slot() = default;
62 void setObject(T& object);
67 T& object() const;
77 Function function() const;
78
79protected:
80 void call(Arguments&&... arguments);
81
82private:
84 T* m_object;
86 Function m_function;
87};
88
89template <typename T, typename... Arguments>
91{
92 m_object = &object;
93}
94
95template <typename T, typename... Arguments>
97{
98 return *m_object;
99}
100
101template <typename T, typename... Arguments>
103{
104 m_function = function;
105}
106
107template <typename T, typename... Arguments>
108typename Slot<T, Arguments...>::Function Slot<T, Arguments...>::function() const
109{
110 return m_function;
111}
112
113template <typename T, typename... Arguments>
114void Slot<T, Arguments...>::call(Arguments&&... arguments)
115{
116 (m_function)(*m_object, std::forward<Arguments>(arguments)...);
117}
118
119} // namespace semf
120#endif // SEMF_UTILS_CORE_SIGNALS_SLOT_H_
Base Class for all Slot implementations.
Definition: slotbase.h:25
Slot for lightweight signal/slot implementation. This ConstSlot is for connecting a signal to a membe...
Definition: slot.h:43
void setFunction(Function function)
setFunction Set the method which should called.
Definition: slot.h:102
Slot(T &object, Function function)
Constructor.
Definition: slot.h:52
Function function() const
Get the method which should called.
Definition: slot.h:108
T & object() const
Get the reference to the context.
Definition: slot.h:96
void(* Function)(T &, Arguments &&...)
Definition: slot.h:46
virtual ~Slot()=default
void call(Arguments &&... arguments)
The invocation of the callback.
Definition: slot.h:114
void setObject(T &object)
Set the reference to the context.
Definition: slot.h:90