Skip to content

Commit

Permalink
Merge pull request #3243 from SergioRAgostinho/bind-doc
Browse files Browse the repository at this point in the history
[docs] Prefer lambdas over binds.
  • Loading branch information
taketwo authored Jul 17, 2019
2 parents 663eb7b + f430784 commit 31038e2
Show file tree
Hide file tree
Showing 15 changed files with 36 additions and 33 deletions.
8 changes: 4 additions & 4 deletions doc/tutorials/content/dinast_grabber.rst
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ The code from *apps/src/dinast_grabber_example.cpp* will be used for this tutori
{
std::function<void (const CloudConstPtr&)> f =
boost::bind (&DinastProcessor::cloud_cb_, this, _1);
[this] (const CloudConstPtr& cloud) { cloud_cb_ (cloud); };
boost::signals2::connection c = interface.registerCallback (f);
Expand Down Expand Up @@ -120,12 +120,12 @@ At the run function what we first have is actually the callback and its registra
.. code-block:: cpp
std::function<void (const CloudConstPtr&)> f =
boost::bind (&DinastProcessor::cloud_cb_, this, _1);
[this] (const CloudConstPtr& cloud) { cloud_cb_ (cloud); };
boost::signals2::connection c = interface.registerCallback (f);
We create a *boost::bind* object with the address of the callback *cloud_cb_*, we pass a reference to our DinastProcessor and the argument place holder *_1*.
The bind then gets casted to a std::function object which is templated on the callback function type, in this case *void (const CloudConstPtr&)*. The resulting function object is then registered with the DinastGrabber interface.
We create a lambda object with the callback *cloud_cb_*, we pass an implicit copy of the DinastProcessor pointer (through *this*).
The lambda then gets casted to a std::function object which is templated on the callback function type, in this case *void (const CloudConstPtr&)*. The resulting function object is then registered with the DinastGrabber interface.

The *registerCallback* call returns a *boost::signals2::connection* object, which we do not use in the this example. However, if you want to interrupt or cancel one or more of the registered data streams, you can call disconnect the callback without stopping the whole grabber:

Expand Down
6 changes: 3 additions & 3 deletions doc/tutorials/content/hdl_grabber.rst
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,8 @@ So let's look at the code. The following represents a simplified version of *vis
cloud_viewer_->setCameraPosition (0.0, 0.0, 30.0, 0.0, 1.0, 0.0, 0);
cloud_viewer_->setCameraClipDistances (0.0, 50.0);
std::function<void (const CloudConstPtr&)> cloud_cb = boost::bind (
&SimpleHDLViewer::cloud_callback, this, _1);
std::function<void (const CloudConstPtr&)> cloud_cb =
[this] (const CloudConstPtr& cloud) { cloud_callback (cloud); };
boost::signals2::connection cloud_connection = grabber_.registerCallback (
cloud_cb);
Expand Down Expand Up @@ -194,7 +194,7 @@ Additional Details

The *HDL Grabber* offers more than one datatype, which is the reason we made
the *Grabber* interface so generic, leading to the relatively complicated
*boost::bind* line. In fact, we can register the following callback types as of
lambda line. In fact, we can register the following callback types as of
this writing:

* `void (const pcl::PointCloud<pcl::PointXYZRGB>::ConstPtr&)`
Expand Down
2 changes: 1 addition & 1 deletion doc/tutorials/content/mobile_streaming.rst
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ few lines of the *run()* method:
.. code-block:: cpp
pcl::OpenNIGrabber grabber (device_id_);
std::function<void (const CloudConstPtr&)> handler_function = boost::bind (&PCLMobileServer::handleIncomingCloud, this, _1);
std::function<void (const CloudConstPtr&)> handler_function = [this] (const CloudConstPtr& cloud) { handleIncomingCloud (cloud); };
grabber.registerCallback (handler_function);
grabber.start ();
Expand Down
15 changes: 8 additions & 7 deletions doc/tutorials/content/openni_grabber.rst
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ So let's look at the code. From *visualization/tools/openni_viewer_simple.cpp*
{
pcl::Grabber* interface = new pcl::OpenNIGrabber();
std::function<void (const pcl::PointCloud<pcl::PointXYZ>::ConstPtr&)> f =
boost::bind (&SimpleOpenNIViewer::cloud_cb_, this, _1);
std::function<void (const pcl::PointCloud<pcl::PointXYZ>::ConstPtr&)> f =
[this] (const pcl::PointCloud<pcl::PointXYZ>::ConstPtr& cloud) { cloud_cb_ (cloud); };
interface->registerCallback (f);
Expand All @@ -87,11 +87,12 @@ So let's look at the code. From *visualization/tools/openni_viewer_simple.cpp*
As you can see, the *run ()* function of *SimpleOpenNIViewer* first creates a
new *OpenNIGrabber* interface. The next line might seem a bit intimidating at
first, but it's not that bad. We create a *boost::bind* object with the address
of the callback *cloud_cb_*, we pass a reference to our *SimpleOpenNIViewer*
and the argument place holder *_1*.
first, but it's not that bad. We create a lambda object which invokes *cloud_cb_*,
we capture a copy of *this* to get an pointer to our *SimpleOpenNIViewer*, so
that *cloud_cb_* can be invoked.

The *bind* then gets casted to a *std::function* object which is templated on

The lambda then gets casted to a *std::function* object which is templated on
the callback function type, in this case *void (const
pcl::PointCloud<pcl::PointXYZ>::ConstPtr&)*. The resulting function object can
the be registered with the *OpenNIGrabber* and subsequently started. Note that
Expand All @@ -103,7 +104,7 @@ Additional Details

The *OpenNIGrabber* offers more than one datatype, which is the reason we made
the *Grabber* interface so generic, leading to the relatively complicated
*boost::bind* line. In fact, we can register the following callback types as of
lambda line. In fact, we can register the following callback types as of
this writing:

* `void (const pcl::PointCloud<pcl::PointXYZRGB>::ConstPtr&)`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ main (int argc,
return (-1);
PCL_WARN ("davidSDK connected\n");

std::function<void (const pcl::PCLImage::Ptr &)> f = boost::bind (&grabberCallback, _1);
std::function<void (const pcl::PCLImage::Ptr&)> f = grabberCallback;
davidsdk_ptr->registerCallback (f);
davidsdk_ptr->start ();
waitForUser ("Press enter to quit");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ main (void)
//ensenso_ptr->initExtrinsicCalibration (5); // Disable projector if you want good looking images.
// You won't be able to detect a calibration pattern with the projector enabled!

std::function<void (const PointCloudT::Ptr&, const PairOfImagesPtr&)> f = boost::bind (&grabberCallback, _1, _2);
std::function<void (const PointCloudT::Ptr&, const PairOfImagesPtr&)> f = grabberCallback;
ensenso_ptr->registerCallback (f);

cv::namedWindow ("Ensenso images", cv::WINDOW_AUTOSIZE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,8 +247,11 @@ class PeoplePCDApp
typedef openni_wrapper::DepthImage::Ptr DepthImagePtr;
typedef openni_wrapper::Image::Ptr ImagePtr;

std::function<void (const PointCloud<PointXYZRGBA>::ConstPtr&)> func1 = boost::bind (&PeoplePCDApp::source_cb1, this, _1);
std::function<void (const ImagePtr&, const DepthImagePtr&, float constant)> func2 = boost::bind (&PeoplePCDApp::source_cb2, this, _1, _2, _3);
std::function<void (const PointCloud<PointXYZRGBA>::ConstPtr&)> func1 = [this] (const PointCloud<PointXYZRGBA>::ConstPtr& cloud) { source_cb1 (cloud); };
std::function<void (const ImagePtr&, const DepthImagePtr&, float)> func2 = [this] (const ImagePtr& img, const DepthImagePtr& depth, float constant)
{
source_cb2 (img, depth, constant);
};
boost::signals2::connection c = cloud_cb_ ? capture_.registerCallback (func1) : capture_.registerCallback (func2);

{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ int main (int argc, char** argv)
bool new_cloud_available_flag = false;
pcl::Grabber* interface = new pcl::OpenNIGrabber();
std::function<void (const pcl::PointCloud<pcl::PointXYZRGBA>::ConstPtr&)> f =
boost::bind (&cloud_cb_, _1, cloud, &new_cloud_available_flag);
[&] (const pcl::PointCloud<pcl::PointXYZRGBA>::ConstPtr& callback_cloud) { cloud_cb_ (callback_cloud, cloud, &new_cloud_available_flag); };
interface->registerCallback (f);
interface->start ();

Expand Down
4 changes: 2 additions & 2 deletions doc/tutorials/content/sources/iccv2011/src/openni_capture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ OpenNICapture::OpenNICapture (const std::string& device_id)
, trigger_ (false)
{
// Register a callback function to our OpenNI grabber...
std::function<void (const PointCloudConstPtr&)> frame_cb = boost::bind (&OpenNICapture::onNewFrame, this, _1);
std::function<void (const PointCloudConstPtr&)> frame_cb = [this] (const PointCloudConstPtr& cloud) { onNewFrame (cloud); };
// ... and start grabbing frames
grabber_.registerCallback (frame_cb);
grabber_.start ();
Expand Down Expand Up @@ -40,7 +40,7 @@ OpenNICapture::snap ()
preview_ = pcl::visualization::PCLVisualizer::Ptr (new pcl::visualization::PCLVisualizer ());

std::function<void (const pcl::visualization::KeyboardEvent&)> keyboard_cb =
boost::bind (&OpenNICapture::onKeyboardEvent, this, _1);
[this] (const pcl::visualization::KeyboardEvent& event) { onKeyboardEvent (event); };

preview_->registerKeyboardCallback (keyboard_cb);
}
Expand Down
4 changes: 2 additions & 2 deletions doc/tutorials/content/sources/iros2011/src/openni_capture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ OpenNICapture::OpenNICapture (const std::string& device_id)
, preview_ ()
{
// Register a callback function to our OpenNI grabber...
std::function<void (const PointCloudConstPtr&)> frame_cb = boost::bind (&OpenNICapture::onNewFrame, this, _1);
std::function<void (const PointCloudConstPtr&)> frame_cb = [this] (const PointCloudConstPtr& cloud) { onNewFrame (cloud); };
// ... and start grabbing frames
grabber_.registerCallback (frame_cb);
grabber_.start ();
Expand Down Expand Up @@ -43,7 +43,7 @@ OpenNICapture::snap ()
preview_ = new pcl::visualization::PCLVisualizer ();

std::function<void (const pcl::visualization::KeyboardEvent&)> keyboard_cb =
boost::bind (&OpenNICapture::onKeyboardEvent, this, _1);
[this] (const pcl::visualization::KeyboardEvent& event) { onKeyboardEvent (event); };

preview_->registerKeyboardCallback (keyboard_cb);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class SimpleOpenNIProcessor

// make callback function from member function
std::function<void (const pcl::PointCloud<pcl::PointXYZRGBA>::ConstPtr&)> f =
boost::bind (&SimpleOpenNIProcessor::cloud_cb_, this, _1);
[this] (const pcl::PointCloud<pcl::PointXYZRGBA>::ConstPtr& cloud) { cloud_cb_ (cloud); };

// connect callback function for desired signal. In this case its a point cloud with color values
boost::signals2::connection c = interface->registerCallback (f);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ int main (int argc, char** argv)
EventHelper event_helper;

std::function<void (const openni_wrapper::DepthImage::Ptr&) > f_depth_image =
boost::bind (&EventHelper::depth_image_cb, &event_helper, _1);
[&] (const openni_wrapper::DepthImage::Ptr& depth) { event_helper.depth_image_cb (depth); };
boost::signals2::connection c_depth_image = interface->registerCallback (f_depth_image);

cout << "Starting grabber\n";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ int main (int argc, char** argv)
EventHelper event_helper;

std::function<void (const openni_wrapper::DepthImage::Ptr&) > f_depth_image =
boost::bind (&EventHelper::depth_image_cb, &event_helper, _1);
[&] (const openni_wrapper::DepthImage::Ptr& depth) { event_helper.depth_image_cb (depth); };
boost::signals2::connection c_depth_image = interface->registerCallback (f_depth_image);

cout << "Starting grabber\n";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ class SimpleOpenNIViewer
pcl::Grabber* interface = new pcl::OpenNIGrabber ();

// make callback function from member function
std::function<void
(const pcl::PointCloud<pcl::PointXYZRGBA>::ConstPtr&)> f = boost::bind (&SimpleOpenNIViewer::cloud_cb_, this, _1);
std::function<void(const pcl::PointCloud<pcl::PointXYZRGBA>::ConstPtr&)> f =
[this] (const pcl::PointCloud<pcl::PointXYZRGBA>::ConstPtr& cloud) { cloud_cb_ (cloud); };

// connect callback function for desired signal. In this case its a point cloud with color values
boost::signals2::connection c = interface->registerCallback (f);
Expand Down
7 changes: 3 additions & 4 deletions doc/tutorials/content/sources/tracking/tracking_sample.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -275,11 +275,10 @@ main (int argc, char** argv)
//Setup OpenNIGrabber and viewer
pcl::visualization::CloudViewer* viewer_ = new pcl::visualization::CloudViewer("PCL OpenNI Tracking Viewer");
pcl::Grabber* interface = new pcl::OpenNIGrabber (device_id);
std::function<void (const CloudConstPtr&)> f =
boost::bind (&cloud_cb, _1);
std::function<void (const CloudConstPtr&)> f = cloud_cb;
interface->registerCallback (f);
viewer_->runOnVisualizationThread (boost::bind(&viz_cb, _1), "viz_cb");

viewer_->runOnVisualizationThread (viz_cb, "viz_cb");

//Start viewer and object tracking
interface->start();
Expand Down

0 comments on commit 31038e2

Please sign in to comment.