Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate std::bind calls to lambda expressions #4041

Merged
merged 20 commits into from
Jan 12, 2024

Conversation

FelipeGdM
Copy link
Contributor

As discussed in this issue, most of the examples in the docs were written using the C++11 function std::bind, which is similar to the ROS 1 way of doing things with boost::bind. In later revisions of C++, lambda expressions became more powerful and today are consider to be more "idiomatic" than std::bind

This PR migrates the C++ examples that use std::bind to equivalent lambda expression code

The affected examples are:

  • Writing a simple C++ publisher and subscriber
  • Using parameters in a class (C++)
  • Using callback groups
  • FastDDS configuration
  • Reading from a bagfile (C++)
  • Recording a bag from your own node (C++)
  • Custom ROS 2 interfaces
  • Adding a tf2 frame (C++)
  • Writing a tf2 broadcaster (C++)
  • Writing a tf2 listener (C++)
  • Writing an action server client (C++)

In the original issue, it's said that it would be interessant to keep some exemples with std::bind as a way to show that it's a possible way to do things in C++. I personally don't know which criteria would be better to choose which example to keep using std::bind, so a listed them all above so other people may say what they think 😁

Closes #3618

Signed-off-by: Felipe Gomes de Melo <felipegmelo.42@gmail.com>
Signed-off-by: Felipe Gomes de Melo <felipegmelo.42@gmail.com>
Signed-off-by: Felipe Gomes de Melo <felipegmelo.42@gmail.com>
Signed-off-by: Felipe Gomes de Melo <felipegmelo.42@gmail.com>
Signed-off-by: Felipe Gomes de Melo <felipegmelo.42@gmail.com>
Signed-off-by: Felipe Gomes de Melo <felipegmelo.42@gmail.com>
Signed-off-by: Felipe Gomes de Melo <felipegmelo.42@gmail.com>
Signed-off-by: Felipe Gomes de Melo <felipegmelo.42@gmail.com>
Signed-off-by: Felipe Gomes de Melo <felipegmelo.42@gmail.com>
Signed-off-by: Felipe Gomes de Melo <felipegmelo.42@gmail.com>
Signed-off-by: Felipe Gomes de Melo <felipegmelo.42@gmail.com>
Signed-off-by: Felipe Gomes de Melo <felipegmelo.42@gmail.com>
@sloretz
Copy link
Contributor

sloretz commented Dec 8, 2023

@tylerjw mind reviewing this one, and checking if the demos and examples need updating to match? 🧇

https://github.com/ros2/demos
https://github.com/ros2/examples

Copy link
Contributor

@tylerjw tylerjw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I carefully read through these changes and did not find any errors. There is one small question about creating named lambdas vs using temporaries and what I think might be a small typo.

I found the PR to the examples repo but not one to the demos repo. Would you mind updating the demos repo too?

FelipeGdM and others added 3 commits December 9, 2023 17:37
Co-authored-by: Tyler Weaver <maybe@tylerjw.dev>
Signed-off-by: Felipe Gomes de Melo <felipegmelo.42@gmail.com>
@FelipeGdM
Copy link
Contributor Author

@tylerjw thank you for your review!

I found the PR to the examples repo but not one to the demos repo. Would you mind updating the demos repo too?

I can also create a PR to update the demos repo, sounds reasonable to me

Copy link
Collaborator

@fujitatomoya fujitatomoya left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@FelipeGdM
Copy link
Contributor Author

@fujitatomoya they were not excluded intentionally, it was an oversight on my part 🤦

I will include them in the migration

Signed-off-by: Felipe Gomes de Melo <felipegmelo.42@gmail.com>
Signed-off-by: Felipe Gomes de Melo <felipegmelo.42@gmail.com>
Copy link
Contributor

@tylerjw tylerjw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed the latest changes and they look good. Thank you for the PR to the demos repo.

@FelipeGdM
Copy link
Contributor Author

FelipeGdM commented Dec 29, 2023

@clalancette I'm sorry, I didn't understand the issue. As far as I understood, the PR ros2/examples#317 adds examples for services using lambdas, but the file you mentioned is about topics.

I can see that the PR performs some cosmetic changes in the topics lambda example, such as adding comments here and there, but the code is essentially the same.

I may be missing something in the relationship between the docs and the examples, if that's the case it would be fine to remove this change and create a draft PR with it and merge once the other one is closed.

Copy link
Collaborator

@fujitatomoya fujitatomoya left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@clalancette
Copy link
Contributor

@clalancette I'm sorry, I didn't understand the issue. As far as I understood, the PR ros2/examples#317 adds examples for services using lambdas, but the file you mentioned is about topics.

No, you are right. When I looked at it, I got confused. This part of it is OK, I'll resolve that conversation.

Copy link
Contributor

@clalancette clalancette left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few more updates (mostly to the geometry_tutorials), then I think this will be good to go.

FelipeGdM and others added 2 commits January 5, 2024 13:56
…Cpp.rst

Co-authored-by: Chris Lalancette <clalancette@gmail.com>
Signed-off-by: Felipe Gomes de Melo <felipegmelo.42@gmail.com>
Co-authored-by: Chris Lalancette <clalancette@gmail.com>
Signed-off-by: Felipe Gomes de Melo <felipegmelo.42@gmail.com>
@FelipeGdM
Copy link
Contributor Author

@clalancette I just created the PR in the geometry_tutorials repo and resolved the conversations

@clalancette clalancette self-assigned this Jan 11, 2024
Copy link
Contributor

@clalancette clalancette left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for all of the work to push this over the line. I suspect that there will be small followups in the next couple of weeks, but this looks good to me so I'll go ahead and merge it.

@clalancette clalancette merged commit 913f2b6 into ros2:rolling Jan 12, 2024
3 checks passed
@Ryanf55
Copy link
Contributor

Ryanf55 commented Jan 12, 2024

Great to see this in!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Use of std::bind in C++ code examples
6 participants