nasa / fprime

F´ - A flight software and embedded systems framework
https://fprime.jpl.nasa.gov
Apache License 2.0
9.95k stars 1.28k forks source link

Consider having `Fw::Buffer::getSerializeRepr` return an object instead of a reference #2714

Open bocchino opened 2 months ago

bocchino commented 2 months ago

Fw::Buffer::getSerializeRepr currently returns a reference into a serialize buffer stored as a member of Fw::Buffer. There are some issues with this:

  1. When you call getSerializeRepr, you're not sure what state the serialize buffer is in. It depends on what has previously been done with the buffer.
  2. The serialize representation state is reset when the buffer object is serialized and deserialized but otherwise persists. This could be an unexpected behavior.
  3. Performing deserialize operations on the buffer updates the serialize buffer and therefore updates the Fw::Buffer. It would be better for theFw::Buffer to be read-only in this case.

Instead we could have getSerializeRepr return an object, say Fw::ExternalSerializeBufferWithMemberCopy. That way the user would own the serialize representation, and it could be freshly initialized each time it is requested. An argument to getSerializeRepr could specify whether the representation is for serialization (in which case the internal serial pointer should be set to zero) or deserialization (in which case the internal serial pointer should be set to the end of the data). Access to the buffer for deserialization would be read-only.

LeStarch commented 2 months ago

I like it! Thanks for the write-up @bocchino!