boost::signals usage

Started by
1 comment, last by wild_pointer 16 years, 6 months ago
I've been using boost::signals all over and in each case it looks something like:

struct Window {
  boost::signal<void (int, int)> onResized;

  void Resize()
  {
    onResized(100, 100);
  }
};
Then to make connections I have:

window->onResized.connect(bind(&Renderer::OnResized, r, _1, _2));
I have the signal in the public interface allows anyone to set up connections (which I want) but also allows anyone to fire off the signal (which I don't want). The solution could be something like:

class Window {
  boost::signal<void (int, int)> onResized;

public:
  void ResizedConnect(boost::function<void (int, int)> f)
  {
    onResized.connect(f);
  }
};
But it's more boilerplate stuff than I'd care for. Is there some simpler way this could be done? I've experimented trying to do this with templates but it gets messy with lots of signals.
[size=2]
Advertisement
Quote:Original post by wild_pointer
I have the signal in the public interface allows anyone to set up connections (which I want) but also allows anyone to fire off the signal (which I don't want).

Yeah, I ran into this problem when I was designing my own event system. One potential solution is to expose it as a public view of a private object:

template<something>class SignalView{public:    SignalView(signal<something> & signal) : m_signal(signal) { }    template<something> void connect(something const& baz) { m_signal.connect(baz); }private:    signal<something> & m_signal;};class MyClass{public:    MyClass() : MySignal(m_mySignalImpl)private:    signal<void> m_mySignalImpl;public:    SignalView<something> MySignal;};

I've been vague about the template parameters, because that's the headachiest part of all this. Basically, you have a proxy object which only exposes the functions you want to expose to the general public.

A better solution, IMHO, is not to have the cute-but-dangerous operator() for signal invocation in the signal/slot library. That's not under your control in this case, though. Regardless, I'd just advocate the "trust the users" strategy here.
Quote:Original post by Sneftel
Regardless, I'd just advocate the "trust the users" strategy here.


I trust myself completely!

Thanks.
[size=2]

This topic is closed to new replies.

Advertisement