TR1 multicast function

How to implement multicast for TR1 functions? I have callback slots implemented as

void setCallback(std::tr1::function<void (std::string)> cb) { this->callback = cb; } 

but you must pass more than one callback in one of them. I don’t want to go into more complex solutions like observer, as this is the only time I need multicast. I also cannot use Boost.Signals (as suggested here ) because I cannot use Boost. I do not need to explicitly handle disabling callback when the subscriber no longer exists.

0
c ++ functor tr1
source share
3 answers

You most likely want:

 void registerCallback(std::tr1::function<void (std::string)> cb) { this->callbacks.push_back(cb); } 

with callbacks container (depending on what you like) of std::tr1::function objects instead of one. When sending, repeat callbacks.

Also, if you want to remove callbacks later, you can do something in this direction:

 // I use list because I don't want the iterators to be invalid // after I add / remove elements std::list<std::function<void(std::string)>> callbacks; ... typedef std::list<std::function<void(std::string)>>::iterator callback_id; callback_id register_callback(std::function<void(std::string)> f) { return callbacks.insert(callbacks.end(), f); } void unregister_callback(callback_id id) { callbacks.erase(id); } 
+3
source share

Have a sequence of them, not one function (i.e. a vector<...> ), but when you call back, repeat the sequence and call.

eg

 std::vector<std::tr1::function<void (std::string)> > callbacks; auto it = callbacks.begin(), end = callbacks.end(); for(; it != end; ++it) (*it)("somestring"); 
0
source share

Put them on a list / vector. If you must delete them separately, you must wrap them somehow (because they are not comparable).

0
source share

All Articles