-
Notifications
You must be signed in to change notification settings - Fork 97
Add C++ support #739
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
Add C++ support #739
Conversation
c767d02 to
9c79006
Compare
kmdade
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Amazing.
So do we draw lots for who goes back and replaces all the existing SBP one-offs? ;)
|
|
||
| public: | ||
|
|
||
| explicit MessageHandler(State *state) : details::CallbackInterface<MsgTypes...>(), state_(*state), callback_nodes_() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a reason the ctor argument isn't State&?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The State needs to be mutable, and our C++ guidelines say any non-const parameter should be passed as a pointer.
RReichert
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Brilliant work, I see no major fault stopping me from approving.
| virtual s32 write(const u8 *buffer, u32 buffer_length) = 0; | ||
| }; | ||
|
|
||
| class State { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm wondering it the class name could be better. The class acts as a SBP processor, where you have a state internally.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The name came from the sbp_state_t which it wraps into a C++ object. I'm open to alternate suggestions though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would actually call it SBP since this class is how I think of SBP but it might be inappropriate. I'll leave it up to you, my comment was merely a suggestion
| @@ -0,0 +1,1026 @@ | |||
| /** | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is there a CI step that validates that the committed message file is identical to that generated from the generator tool?
I'm assuming that this file was generated by the python/template file that was added
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think there's a double check in the CI. I think it's assumed as part of the manual review that only the relevant changes are made to the auto generated files.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd probably say it would be worth adding that in so there is no misalignment between one source and its C++ representation
| * the `context` parameter. | ||
| * | ||
| * @tparam MsgT The message type to decode the payload as | ||
| * @tparam ClassT The class type to call the function on |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a way to call the same function on multiple class types? I find this is a common pattern for me (i.e do the same thing for any ephemeris message received)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not with how this PR currently stands. The problem with that is that all of the message types are distinct and there's little to no information relating the content of similar messages. The closest thing we could get is defining a base class that handles each of the distinct message types and converts them into a common type and then calls another member function that works on the common type.
Something like this:
class CommonEphemerisHandler : public sbp::MessageHandler<EphemerisMessage1, EphemerisMessage2> {
public:
struct CommonEphemerisType { ... };
virtual void handle_common_ephemeris(const CommonEphemerisType& eph) = 0;
void handle_sbp_msg(uint16_t sender_id, uint8_t message_length, const EphemerisMessage1& msg) {
CommonEphemerisType eph = convertEph1(msg);
handle_common_ephemeris(eph);
}
void handle_sbp_msg(uint16_t sender_id, uint8_t message_length, const EphemerisMessage2& msg) {
CommonEphemerisType eph = convertEph2(msg);
handle_common_ephemeris(eph);
}
};
class CustomEphemerisHandler : public CommonEphemerisHandler {
public:
void handle_common_ephemeris(const CommonEphemerisType& eph) { ... }
};
class OtherEphemerisHandler : public CommonEphemerisHandler {
public:
void handle_common_ephemeris(const CommonEphemerisType& eph) { ... }
};
3473007 to
c175d1c
Compare
Added steps to autogenerate SBP message traits for the C++ headers Regenerated the C files
d2bc31c to
73e7159
Compare
This PR adds a few lightweight C++ classes that wrap around the existing libsbp C interface. The main item of interest here is the
MessageHandler. This class automates the infrastructure for registering a callback to have a member function called upon receipt of a sbp message.Here is an example of how it would be used
The instantiation and registration of the
sbp_msg_callbacks_node_tobjects are all taken care of automatically by thesbp::MessageHandlerconstructor, and are unregistered in the destructor. The derived class is able to publicly or privately inherit fromsbp::MessageHandler, and thehandle_sbp_msgmember functions can be public or private. Eachhandle_sbp_msgis a virtual member function so additional levels of inheritance can be employed, though we do incur the overhead of the vtable lookup at run time. The mapping of message struct to message ID is achieved via a set of type traits that are automatically generated from the specification YAML. Currently only the message ID is included in the message type traits, but additional information could be added in the future if deemed useful.The other classes are thin wrappers around the existing C structs, and probably don't need much explanation.