Is it possible to block the removal of certain slots from a signal in the boost.signals library?
If so how should a code that does such a thing will look like? Do I need to create a derived class just for that specific signal to do so?
views:
32answers:
1
+1
A:
Supply your own slot connection function that fails to return the connection. Without the connection the client can't break it.
Edit: code example:
struct my_class
{
boost::signals::connection listen_event1(boost::function<void (my_class const&)> const& f)
{
return signal1.connect(f);
}
void listen_event2(boost::function<void my_class const&)> const& f)
{
signal2.connect(f);
}
private:
typedef boost::signals::signal<void(my_class const&)> sig_t;
sig_t signal1;
sig_t signal2;
};
signal2 connections cannot be disconnected.
Noah Roberts
2010-06-01 16:05:27
You mean that my signal will be a special signal that derives the original one? Will it have two connect functions? One that accepts a removable connection and one that accepts a non-removable connection? Or do you mean that I should supply a function that connects but doesn't return a connection? How can I overload the return type?
the_drow
2010-06-01 16:13:15
No. Hopefully you're not exposing your signal object publicly, right? You have functions in the class that generates signals that allows you to connect with them. Just don't return the boost::signals::connection object that is returned by the signal object you're using to implement that signal.
Noah Roberts
2010-06-01 16:17:55
I am exposing those signals through getters. I've seen it done in http://www.webtoolkit.eu (Wt)
the_drow
2010-06-01 16:19:45
Well, guess now you know why that's not necessarily a good idea. I'd refactor so that those signal objects are encapsulated. Then you can block disconnection by not returning the connection. I don't know of any other way to get what you want.
Noah Roberts
2010-06-01 16:24:05