Boost logo

Boost Users :

From: Stefan Funck (stefanfunck_at_[hidden])
Date: 2007-10-08 10:43:14


Hi everybody,

I'd like to have a generic wrapper that can be put "in between" a signal and a
slot to provide some additional functionality to the slot.
So where I normally have something like

void myslot(int i)
{
    std::cout << "slot(" << i << ")\n";
}

int main(int argc, char *argv[])
{
    boost::signal<void (int)> sig;
    sig.connect(boost::bind(myslot, _1));
    sig(123);

    return 0;
}

I want something like

int main(int argc, char *argv[])
{
    boost::signal<void (int)> sig;
    SlotWrapper<void (int)> slotWrap(boost::bind(myslot, _1));

    sig.connect(boost::bind(SlotWrapper<void (int)>::slot, &slotWrap, _1));
    sig(123);

    return 0;
}

The signature of SlotWrapper<Signature>'s member function slot() is defined by
Signature. slot()'s implementation should have some common functionality (that's
what I want the wrapper for) and then call the actual slot that was given at
construction (myslot() in the example above).

Is that doable? So far I have two questions:
1) How can I define a member function according to the signature that is given
as a template parameter to the class?
2) If I can do 1), how can I then bind the arguments given and call another
function, i.e. the original slot?

A skeleton of the wrapper might look like:

template <typename Signature>
class SlotWrapper
{
public:
    SlotWrapper(const typename boost::signal<Signature>::slot_function_type& slot)
    : m_slot(slot)
    {}

    // define a function according to Signature
    typename boost::signal<Signature>::result_type slot(...arguments...)
    {
        // do something common

        // call somehow the actual slot (m_slot) with the given ...arguments...
    }

private:
        const typename boost::function<Signature> m_slot;
};

How can I write the slot() function?

Thanks for any help,
Stefan


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net