An introduction to Boost Signals

By , last updated May 14, 2016


In our game Burnt Islands we use boost::signals very extensively. Actually it’s boost signals2 or boost::signals2. That’s the one which is safe to use with threads, which is very important when you’re making games.

This article will go through a couple of scenarios on how to use Boost Signals2 with various calling techniques and provide several boost signal examples. You can call it an introductory Boost Signals2 tutorial.

All code is at Github.

Signals with free functions with no arguments

The simplest use of signals there is, is to call free methods (functions which are not members of any class). Here is a simple boost signal example:

#include <stdio.h>
#include <boost/signals2.hpp>

// Free function 1
void no_arguments()
{
    std::cout << "No arguments" << std::endl;
}

// Free function 2
void no_arguments_2()
{
    std::cout << "No arguments 2" << std::endl;
}

int main()
{
    // Shortcut typedefs
    typedef boost::signals2::signal<void()>     registration_manager;
    typedef registration_manager::slot_type     registration_request;

    // Define the signal handler
    registration_manager signalHandler;

    // Connect the above methods to this handler
    signalHandler.connect( &no_arguments );
    signalHandler.connect( &no_arguments_2 );

    // Call the handlers
    signalHandler();

    // Output should be:
    // No arguments
    // No arguments 2

    return 0;
}

Now, that isn’t too useful just yet. A free method without any arguments, would either modify the global state or do nothing. Neither is useful and is just ugly.

Signals with free functions with arguments

Next is to pass parameters

#include <stdio.h>
#include <boost/signals2.hpp>

// Free function 1
void argument(double value)
{
    std::cout << "Arguments " << value << std::endl;
}

// Free function 2
void argument_2(double value)
{
    std::cout << "Half argument " << value / 2.0 << std::endl;
}

int main()
{
    // Shortcut typedefs
    typedef boost::signals2::signal<void(double)>   registration_manager;
    typedef registration_manager::slot_type         registration_request;

    // Define the signal handler
    registration_manager signalHandler;

    // Connect the above methods to this handler
    signalHandler.connect( &argument );     // Defined above
    signalHandler.connect( &argument_2 );   // Defined above

    // Call the handlers
    signalHandler( 3.14 );

    // Output should be:
    // Arguments 3.14
    // Half argument 1.57

    return 0;
}

Professional Software Developer, doing mostly C++. Connect with Kent on Twitter.

Comments

  1. Tama May 14, 2016 Leave a Reply

    It’s unreadable because of &gt &amp characters instead of the actual characters.

    • kent May 14, 2016 Leave a Reply

      Hi, thanks for letting us know.

      I’ve fixed it, and lets hope it stays like this. WordPress is stupid when it comes to code in posts. The editor will silently mangle and destroy code, when it’s trying to be “smart”.

      Kent

Leave a Reply


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*