Hello ROS community!
I would like to discuss a usage and a potential improvement of a message builders for C++.
Not sure that this Idea will be useful in any way, so I’ve decided to start a topic here to discuss it.
Problem at hand
Say I have a message my_msgs/msg/Example.msg
, that goes something like this:
# Some non-default value
string str
# String with default value
string def_str "This is a default string value"
In my C++ code I might construct this message in two ways:
- Using a constructor and filling the data afterwards
- Using a builder
Constructor approach
Using constructor with no values might look something like this
...
auto ex_msg = Example();
ex_msg.str = "My non-default string value";
...
Builder approach
It seems to me that it is a dedicated design approach, but it feels kind of cumbersome.
...
auto ex_msg = my_msgs::build<Example> ()
.str("My non-default string value")
.def_str("Need to set value for field with default value");
...
Python
In Pythhon I may utilize a much simpler approach
from my_msgs.msgs import Example
...
ex_msg = Example(
str="My non-default string value"
)
Solution approaches, as I see the
There are a few approaches coming to my mind, for solving this problem.
- Introduce changes to generated C++ code for builder. But I’m not quite sure what this may look like yet.
- Add new interface. Like an another generated better_builder(lol) class for messages.
- Give C++ users an option to use message constructors as Python users do.
Potential pitfalls with these options
- Existing interface is not designed for such changes. Might take some dirty hacks to do something like that
- New interface means more code to support, new bugs etc. I’m not sure that this is really necessary for a such unpopular problem
Other discussions that I’ve found
I’ve found this discussion on rosidl’s github page. It is mentioned there that a syntax like this
auto my_header = std_msgs::msg::Header({.frame_id = "hello"});
may be added with the introduction of C++20, but it seems that C++20 may stay unsupported for quite some time in the future(there was a discussion at ros2 github, but I have failed to find it again. Will add to the comments if I’ll find it later), and I’m not sure if that will solve the default-value message fields issue in general.