Création de vos propres signaux

Maintenant que nous avons vu les signaux et les gestionnaires de signal dans gtkmm, vous aimeriez peut-être utiliser la même technique pour interagir entre vos propres classes. C'est vraiment très simple en utilisant directement la bibliothèque libsigc++.

This isn't purely a gtkmm or GUI issue. gtkmm uses libsigc++ to implement its proxy wrappers for the GTK signal system, but for new, non-GTK signals, you can create pure C++ signals, using the sigc::signal<> template.

For instance, to create a signal that sends 2 parameters, a bool and an int, just declare a sigc::signal, like so:

sigc::signal<void(bool, int)> signal_something;

You could just declare that signal as a public member variable, but some people find that distasteful and prefer to make it available via an accessor method, like so:

class Server
{
public:
  //signal accessor:
  using type_signal_something = sigc::signal<void(bool, int)>;
  type_signal_something signal_something();

protected:
  type_signal_something m_signal_something;
};

Server::type_signal_something Server::signal_something()
{
  return m_signal_something;
}

You can then connect to the signal using the same syntax used when connecting to gtkmm signals. For instance,

server.signal_something().connect(
  sigc::mem_fun(client, &Client::on_server_something) );