54 inline bool IsEmpty()
const {
return target ==
nullptr;}
58 template <
typename...>
85 virtual void RemoveSubscriptionByID(
int,
Subscription*) = 0;
144 template <
typename... Types>
147 std::list< std::function<void(Types...)> > subscribers_forever;
148 std::map< int, std::function<void(Types...)> > subscribers_with_id;
149 std::list< std::function<void(Types...)> > subscribers_once;
150 void RemoveSubscriptionByID(
int id,
Subscription* ptr)
override{
151 auto it = subscribers_with_id.find(
id);
152 if(it == subscribers_with_id.end()){
153 std::cout <<
"WARNING: removing an unexisting subscription!" << std::endl;
156 subscribers_with_id.erase(it);
172 void SubscribeForever( std::function<
void(Types...)> f ) { subscribers_forever.push_back(f); }
176 void SubscribeOnce( std::function<
void(Types...)> f ) { subscribers_once.push_back(f); }
180 void SubscribeForever( C* class_ptr,
void (C::*member_ptr)(Types...)) { subscribers_forever.push_back( std::bind(member_ptr,class_ptr,std::placeholders::_1) ); }
182 void SubscribeOnce( C* class_ptr,
void (C::*member_ptr)(Types...)) { subscribers_once.push_back( std::bind(member_ptr,class_ptr,std::placeholders::_1) ); }
191 std::list< std::function<void(Types...)> > list_to_call;
192 for(
auto& f : subscribers_forever) list_to_call.push_back(f);
193 for(
auto& f : subscribers_once) list_to_call.push_back(f);
194 for(
auto& it : subscribers_with_id) list_to_call.push_back(it.second);
197 subscribers_once.clear();
199 for(
auto f : list_to_call) f(t...);
200 return !list_to_call.empty();
204 unsigned int Count(){
return subscribers_forever.size() + subscribers_once.size() + subscribers_with_id.size();}
207 template <
typename... Types>
209 int sub_id = ++subscription_id_counter;
210 subscribers_with_id[sub_id] = f;
213 subscriptions.push_back(&p);
216 template <
typename... Types>
template <
class C>
218 int sub_id = ++subscription_id_counter;
219 subscribers_with_id[sub_id] = std::bind(member_ptr,class_ptr,std::placeholders::_1);
222 subscriptions.push_back(&p);
233 list.push_back(std::move(s));
friend class Subscription
Definition: Signal.hpp:87
~Signal()
Definition: Signal.hpp:162
friend class SignalBase
Definition: Signal.hpp:62
~Subscription()
Definition: Signal.hpp:52
SubscriptionList subscriptions
Definition: Signal.hpp:249
Definition: Signal.hpp:77
Signal & operator=(const Signal &other)=delete
Definition: Signal.hpp:247
friend class Signal
Definition: Signal.hpp:59
void SubscribeOnce(std::function< void(Types...)> f)
Definition: Signal.hpp:176
SubscriptionList()
Definition: Signal.hpp:236
void ReleaseAll()
Definition: Signal.hpp:231
std::list< Subscription > list
Definition: Signal.hpp:230
bool Happen(Types...t)
Definition: Signal.hpp:187
Subscription Subscribe(std::function< void(Types...)> f) __attribute__((warn_unused_result))
SubscriptionList & operator+=(Subscription &&s)
Definition: Signal.hpp:232
Subscription()
Definition: Signal.hpp:47
Definition: Alertable.hpp:26
void SubscribeForever(std::function< void(Types...)> f)
Definition: Signal.hpp:172
Definition: Signal.hpp:44
bool IsEmpty() const
Definition: Signal.hpp:54
Definition: Signal.hpp:227
void SubscribeOnce(C *class_ptr, void(C::*member_ptr)(Types...))
Definition: Signal.hpp:182
static int subscription_id_counter
Definition: Signal.hpp:86
Definition: Signal.hpp:145
Subscription __attribute__((warn_unused_result)) Signal< Types...>
Definition: Signal.hpp:208
unsigned int Count()
Definition: Signal.hpp:204
std::list< Subscription * > subscriptions
Definition: Signal.hpp:83
Subscription & operator=(Subscription &&other)
Signal()
Definition: Signal.hpp:161