1 #if !defined(_BTKSIGNAL_HPP_) 2 #define _BTKSIGNAL_HPP_ 11 [[noreturn]]
void throwEmptySignal();
15 typedef void(*DeleteFn)(
void *
self,
bool from_hasslots);
16 SlotBase(DeleteFn fn):delete_ptr(fn){};
19 void cleanup(
bool from_hasslots =
false){
20 delete_ptr(
this,from_hasslots);
25 template<
class RetT,
class ...Args>
28 typedef RetT(*InvokeFn)(
const void *
self,Args ...args);
30 Slot(DeleteFn df,InvokeFn fn):
34 RetT invoke(Args ...args)
const{
35 return invoke_ptr(
this,std::forward<Args>(args)...);
40 template<
class T,
class RetT,
class ...Args>
43 Slot<RetT,Args...>(Delete,Invoke),
44 fn(std::forward<T>(f)){};
48 static void Delete(
void *
self,
bool){
52 static RetT Invoke(
const void *__self,Args ...args){
54 return slot->fn(std::forward<Args>(args)...);
61 SignalBase(
const SignalBase &) =
delete;
64 void disconnect_all(){
65 for(
auto iter = slots.begin();iter != slots.end();){
67 iter = slots.erase(iter);
70 bool empty()
const noexcept{
74 bool operator ==(std::nullptr_t)
const noexcept{
77 bool operator !=(std::nullptr_t)
const noexcept{
80 operator bool()
const noexcept{
83 size_t connected_slots()
const noexcept{
86 std::list<Impl::SlotBase*> slots;
91 std::list<Impl::SlotBase*>::iterator iter;
93 void disconnect(
bool from_hasslots =
false){
94 (*iter)->cleanup(from_hasslots);
95 current->slots.erase(iter);
99 bool try_disconnect(
bool from_hasslots =
false){
100 std::list<Impl::SlotBase*>::iterator i;
101 for(i = current->slots.begin();i != current->slots.end(); ++i){
104 disconnect(from_hasslots);
115 template<
class RetT,
class ...Args>
116 struct Signal<RetT(Args...)>;
121 HasSlots(
const HasSlots &) =
delete;
125 void disconnect_all();
127 std::list<Connection> _connections;
132 template<
class Class,
class Method,
class RetT,
class ...Args>
135 :
Slot<RetT,Args...>(Delete,Invoke),
142 std::list<Connection>::iterator iter;
144 static RetT Invoke(
const void *__self,Args ...args){
146 return ((slot->class_ptr)->*(slot->method))(
147 std::forward<Args>(args)...
150 static void Delete(
void *
self,
bool from_hasslots){
152 if(not from_hasslots){
155 slot->class_ptr->HasSlots::_connections.erase(
169 template<
class RetT,
class ...Args>
174 RetT emit(Args ...args)
const{
175 if constexpr(std::is_same<RetT,void>::value){
178 static_cast<Impl::Slot<RetT,Args...
>*>(s)->invoke(
179 std::forward<Args>(args)...
187 ret =
static_cast<Impl::Slot<RetT,Args...
>*>(s)->invoke(
188 std::forward<Args>(args)...
195 template<
class Callable>
199 std::forward<Callable>(callable)
208 template<
class Callable,
class Object>
209 void connect(Callable &&callable,Object *
object){
210 static_assert(std::is_base_of<HasSlots,Object>::value,
"Object must inherit HasSlots");
215 slots.push_back(solt);
217 object->HasSlots::_connections.push_back({
223 solt->iter = --(
object->HasSlots::_connections.end());
225 RetT operator ()(Args ...args)
const{
226 return Signal::emit(std::forward<Args>(args)...);
229 typedef RetT result_type;
234 #endif // _BTKSIGNAL_HPP_ Definition: signal.hpp:13
Definition: signal.hpp:113
Definition: signal.hpp:89
This header include many useful containers.
Definition: async.hpp:7
Definition: signal.hpp:133
Definition: signal.hpp:26
Definition: signal.hpp:119
Definition: signal.hpp:59
Definition: signal.hpp:41