How to call shared_ptr<boost::signal> from a vector in a loop?

Posted by BTR on Stack Overflow See other posts from Stack Overflow or by BTR
Published on 2011-01-18T02:23:13Z Indexed on 2011/01/18 3:53 UTC
Read the original article Hit count: 180

I've got a working callback system that uses boost::signal. I'm extending it into a more flexible and efficient callback manager which uses a vector of shared_ptr's to my signals. I've been able to successfully create and add callbacks to the list, but I'm unclear as to how to actually execute the signals.

...

// Signal aliases
typedef boost::signal<void (float *, int32_t)> Callback;
typedef std::shared_ptr<Callback> CallbackRef;

// The callback list
std::vector<CallbackRef> mCallbacks;

// Adds a callback to the list
template<typename T>
void addCallback(void (T::* callbackFunction)(float * data, int32_t size), T * callbackObject) 
{
    CallbackRef mCallback = CallbackRef(new Callback());
    mCallback->connect(boost::function<void (float *, int32_t)>(boost::bind(callbackFunction, callbackObject, _1, _2)));
    mCallbacks.push_back(mCallback);
}

// Pass the float array and its size to the callbacks
void execute(float * data, int32_t size)
{

    // Iterate through the callback list
    for (vector<CallbackRef>::iterator i = mCallbacks.begin(); i != mCallbacks.end(); ++i)
    {

        // What do I do here?
        // (* i)(data, size); // <-- Dereferencing doesn't work

    }   

}

...

All of this code works. I'm just not sure how to run the call from within a shared_ptr from with a vector. Any help would be neat-o. Thanks, in advance.

© Stack Overflow or respective owner

Related posts about c++

Related posts about pointers