开发者

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

开发者 https://www.devze.com 2023-02-05 09:10 出处:网络
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

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开发者_Go百科_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.


That's because dereferencing results in a shared_ptr, not the callback object:

        // (* i)(data, size); // <-- Dereferencing doesn't work

Try dereferencing twice:

        (**i)(data, size);

optionally you can call:

        (*i)->operator()(data, size);
0

精彩评论

暂无评论...
验证码 换一张
取 消