Reputation: 13
I am struggling to use boost::function and boost::bind correctly to bind the passed object and member function into a boost::function which than is later called when data are in the queue
I am working on a multi producer single consumer queue for inter-thread communication. The basic idea is to provide a template class. When data published into the queue, the subscriber gets the data via a callback function which has to supply on subscription.
Note: ...
denotes places where I omit code for legibility reasons
T is the type which is stored in the queue
template <typename T>
class pipeline_mpsc
{...};
The subscribe function
template <typename FUNC, class OBJ>
bool subscribe(FUNC fn, OBJ obj)
{
bool loc_return = false;
if (Callback_fn_ == NULL)
{
boost::function<void(const T &)> BoundCallback(boost::bind(fn, obj, _1));
Callback_fn_ = &BoundCallback;
boost::thread th(&pipeline_mpsc::Callbackcaller, this, &BoundCallback); //pipeline.hpp[38, 25]
loc_return = true;
}
return loc_return;
};
void Callbackcaller(boost::function<void(const T &)> *Callback_fn) {
...
Callback_fn(loc_tmp); //pipeline.hpp[96, 18]
};
How I call the subscribe function
laserscan_sub_->subscribe(&LidarFovFilter::laserscan_callback, this); //LidarFovFilter.cpp[25, 73]
Prototype of the Callback function
void LidarFovFilter::laserscan_callback(const LaserScan &scan)
When I compile it I get following error from gcc:
‘Callback_fn’ cannot be used as a function
pipeline.hpp[96, 18]:In instantiation of ‘void pipeline_mpsc::Callbackcaller(boost::function) [with T = LaserScan]’:
pipeline.hpp[38, 25]:required from ‘bool pipeline_mpsc::subscribe(FUNC, OBJ) [with FUNC = void (LidarFovFilter::
LidarFovFilter.cpp[25, 73]: required from here
From what I read how to use boost::bind and boost::function I think it my code should work (but obviously it doesn't).I am at loss here why it doesn't. Where is my mistake? Help would be really appricated.
Upvotes: 1
Views: 218
Reputation: 10962
Issue is you pass a pointer to a boost::function - you need to dereference first:
void Callbackcaller(boost::function<void(const T &)> *Callback_fn) {
...
(*Callback_fn)(loc_tmp); //pipeline.hpp[96, 18]
};
Upvotes: 0