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

Alternative version of ros::param::param with return value #591

Closed
xperroni opened this Issue Mar 30, 2015 · 4 comments

Comments

Projects
None yet
3 participants
@xperroni
Contributor

xperroni commented Mar 30, 2015

The template function ros::param::param sets a variable either to the value of a parameter, or if not available, a predefined default value. In cases where the variable is passed straight to a constructor or function call and then disregarded, more streamlined code could be written if the result value could be retrieved as a function return value, rather than through an output variable.

For example, the code below:

std::string path;
std::string format;
double fps;

param("~path", path, "video.mpg");
param("~format", format, "MPEG");
param("~fps", fps, 30.0);

VideoRecorder recorder(path, format, fps);

Could be more succinctly written as:

VideoRecorder recorder(
    param<std::string>("~path", "video.mpg"),
    param<std::string>("~format", "MPEG"),
    param<double>("~fps", 30.0)
);

If an alternative definition of ros::param::param such as this was available:

template<typename T>
T param(const std::string& param_name, const T& default_val)
{
  T param_val;
  param(param_name, param_val, default_val);
  return value;
}
@dirk-thomas

This comment has been minimized.

Member

dirk-thomas commented Mar 30, 2015

Looks reasonable. Could you please provide a pull request adding this feature? It would be great if you would also include a unit test for it to ensure its working / continues to work in the future.

The existing function is implemented in two locations:

  • clients/roscpp/include/ros/param.h
  • clients/roscpp/include/ros/node_handle.h
@xperroni

This comment has been minimized.

Contributor

xperroni commented Mar 31, 2015

Done. I wasn't aware of the param method in NodeHandle or the unit test suite, thank you for the heads-up.

@xperroni

This comment has been minimized.

Contributor

xperroni commented Apr 7, 2015

Pull request sent and merged.

@xperroni xperroni closed this Apr 7, 2015

@tfoote

This comment has been minimized.

Member

tfoote commented Apr 7, 2015

For reference it was #592

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment