Boost C++ Libraries Home Libraries People FAQ More

PrevUpHomeNext

Quick Start

[Note] Note

You may want to read the Namespace Use and Signal Network and Boost.Fusion sections first.

The Sigal Network library provides many common building block components for a signal network. The most basic is storage, which can be used to store a value received through a signal, send it on, and/or retreive it.

By default, components in the library use operator() to receive a signal. For example, storage objects can receive signals through operator()(). Upon receiving this signal, they will output their stored value through another signal.

The value stored inside a storage object can be retrieved via the at() method.

Using a few storage objects, it is easy to create a network using operator>>=:

Table 1.2. storage class use example

fused

unfused

// instantiate all of the components we need
signals::storage<void (), signals::fused> banger;
signals::storage<void (float), signals::fused> floater(2.5f);
signals::storage<void (float), signals::fused> collector(0.0f);

// create the network (banger to floater.send, floater to collector)
banger >>= floater.send_slot() >>= collector;

// signal from banger causes floater to output 2.5
banger();
BOOST_CHECK_EQUAL(collector.at<0>(), 2.5f);

// change the value in floater
floater(1.5f);
floater.send(); // we can also signal floater directly
BOOST_CHECK_EQUAL(collector.at<0>(), 1.5f);

// instantiate all of the components we need
signals::storage<void (), signals::unfused> banger;
signals::storage<void (float), signals::unfused> floater(2.5f);
signals::storage<void (float), signals::unfused> collector(0.0f);

// create the network (banger to floater.send, floater to collector)
banger >>= floater.send_slot() >>= collector;

// signal from banger causes floater to output 2.5
banger();
BOOST_CHECK_EQUAL(floater.at<0>(), 2.5f);
BOOST_CHECK_EQUAL(collector.at<0>(), 2.5f);

floater(1.5f); // change the value in floater
floater.send(); // we can also signal floater directly
BOOST_CHECK_EQUAL(collector.at<0>(), 1.5f);

Creating your own signal receiver (slot)

The easiest way to create your own signal receiver which can be used with the Signal Network library is to create a class with operator() of the signal signature you wish to receive.

For example, consider the signature void(). This is the signature of a function that returns void and takes no arguments. A class that can receive signals of such a signature would be defined as follows:

class SignalVoidCounter
#ifdef SIGNAL_NETWORK_TRACKABLE
    // Two base signals implementations are supported currently
    // (Boost.Signals by Doug Gregor and the
    // thread_safe_signals version under implementation by Frank Mori Hess).
    // SIGNAL_NETWORK_TRACKABLE is defined if you are using Boost.Signals
    // - in this case, we want to make our component trackable so that
    // it gets disconnected automatically when this object is destroyed.
    : public boost::signals::trackable
#endif
{
    volatile int cnt;
public:
    SignalVoidCounter() : cnt(0) {}
    void operator()()
    {
        cnt++; // whenever a void() signal is received, increase the counter
    }
    int GetCount()
    {
        return cnt;
    }
}; // end class SignalVoidCounter

The above class does something really simple - it receives signals of signature void(), and counts how many it has received. While this functionality is actually covered by the library component counter, it's a good introductory example.

If you read the section about Boost.Fusion, you will find out that the Signal Network library works with both fused and unfused signals. If you wanted the above to work with fused signals, all you would need to do is add or substitute the following operator():

void operator()(const fusion::vector<> &)
{
    cnt++; // whenever a void() signal is received, increase the counter
}

You can now introduce this component into a signal network:

Table 1.3. storage class use example

fused

unfused

SignalVoidCounter counter;
signals::storage<void()> storage;

storage >>= counter;
for (int i=0; i<33; i++)
    storage();

assert(counter.GetCount());

SignalVoidCounter counter;
signals::storage<void(), signals::unfused> storage;

storage >>= counter;
for (int i=0; i<33; i++)
    storage();

assert(counter.GetCount());

To proceed, you may want to look at

Copyright © 2007 Stjepan Rajko

PrevUpHomeNext