Jump to content
  • Advertisement
Sign in to follow this  
wild_pointer

boost::signals usage

This topic is 4089 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!