Search code examples
c++boost-signals

How to prevent removal of slots from a certain signal?


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?


Solution

  • 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.