diff --git a/README.md b/README.md index 7e3ff56..106d597 100644 --- a/README.md +++ b/README.md @@ -112,7 +112,8 @@ Four use cases — serialize a message that fits one buffer, serialize one too l for it, deserialize a whole message, deserialize one arriving in chunks — plus the generated-code path that wraps them. The examples use the **C core**; the header-only **C++ wrapper** mirrors each through `sofab::OStreamInline` / -`sofab::IStreamObject`. +`sofab::IStreamObject` — or `sofab::IStreamInto`, which decodes into a message +you already hold instead of owning one. ### Serialize diff --git a/src/include/sofab/sofab.hpp b/src/include/sofab/sofab.hpp index d936a94..63a6418 100644 --- a/src/include/sofab/sofab.hpp +++ b/src/include/sofab/sofab.hpp @@ -3569,6 +3569,8 @@ namespace sofab friend class IStreamImpl; template friend class IStreamObject; + template + friend class IStreamInto; struct Context { @@ -3682,6 +3684,55 @@ namespace sofab } }; + /*! + * @brief Decoder that decodes into a message instance the CALLER owns. + * + * @ref IStreamObject with the instance taken out: it binds the message handed + * to the constructor instead of holding one, so fed bytes are dispatched to + * that object's @c deserialize and nothing is staged and copied. The whole + * difference is where the message lives -- same C-ABI field callback, same + * dispatch, no @c std::function -- so it costs a pointer where + * @ref IStreamObject costs a whole message, which on a heap-free target is + * the difference between one and two copies of it on the stack. + * + * The bound object is written as the bytes arrive, so a rejected input + * leaves the fields decoded before the error in it. Put it back where it + * belongs first if that matters -- generated code calls its @c reset() -- + * and note that a field the bytes do not carry keeps whatever the object + * already held (MESSAGE_SPEC §2 omits an all-default + * field), which is the same reason. + * + * The message must outlive the stream, and must not be bound to a second + * stream while this one is fed: binding is what the object's own callback + * context points at. + * + * ```cpp + * Telemetry msg; // kept across messages, reused + * msg.reset(); + * sofab::IStreamInto in{msg}; + * auto r = in.feed(bytes, len); + * ``` + * + * @tparam MessageType An @ref InputMessage type. + */ + template + class IStreamInto : public IStreamImpl + { + MessageType *data_; //!< The caller's message instance being decoded into. + + public: + /*! + * @brief Bind @p message to this stream. + * @param message Destination; must outlive the stream. + */ + explicit IStreamInto(MessageType &message) noexcept + : data_{&message} + { + data_->context_ = IStreamMessage::Context{this, data_}; + sofab_istream_init(&ctx_, MessageType::field_callback_, &data_->context_); + } + }; + /* ---------------------------------------------------------------------- */ /* Wrapper-sequence collectors and encode helpers */ /* */ diff --git a/test/cpp-freestanding/freestanding.cpp b/test/cpp-freestanding/freestanding.cpp index dc90e47..a10009d 100644 --- a/test/cpp-freestanding/freestanding.cpp +++ b/test/cpp-freestanding/freestanding.cpp @@ -203,5 +203,21 @@ extern "C" int sofab_freestanding_roundtrip(void) } #endif + // The same bytes again, decoded into an object the caller owns + // (generator#744). IStreamInto has to work here of all places: this build + // has no std::function, so a decoder that needed one would not compile. + Parent into; + sofab::IStreamInto is2{into}; + if (is2.feed(os.data(), used).code() != sofab::Error::None) + { + return -4; + } + + if (into.data_.header != 7u || into.data_.child.data_.id != 42u + || into.data_.footer != 99u || !into.data_.flag || into.data_.tag != "tag42") + { + return -5; + } + return 0; } diff --git a/test/cpp/test_istream.cpp b/test/cpp/test_istream.cpp index 4d524ae..0c23e50 100644 --- a/test/cpp/test_istream.cpp +++ b/test/cpp/test_istream.cpp @@ -389,6 +389,58 @@ TEST_CASE("IStream: object feed buffer") REQUIRE(istream->value == 3.1415f); } +TEST_CASE("IStream: IStreamInto decodes into the caller's object") +{ + SimpleObject2 object; + sofab::IStreamInto istream{object}; + + const uint8_t buffer[] = {0x08, 0x2a, 0x12, 0x20, 0x56, 0x0e, 0x49, 0x40}; + + auto result = istream.feed(buffer, sizeof(buffer)); + REQUIRE(result.code() == sofab::Error::None); + + // The caller's object, not a staged one copied over it afterwards. + REQUIRE(object->id == 42); + REQUIRE(object->value == 3.1415f); +} + +TEST_CASE("IStream: a bound object keeps what the next message does not carry") +{ + // The other half of decoding in place, and the reason generated code calls + // reset() first: MESSAGE_SPEC §2 leaves an all-default field out of the + // bytes, so a field these bytes do not carry keeps what the object held. + SimpleObject2 object; + + const uint8_t both[] = {0x08, 0x2a, 0x12, 0x20, 0x56, 0x0e, 0x49, 0x40}; + { + sofab::IStreamInto istream{object}; + REQUIRE(istream.feed(both, sizeof(both)).code() == sofab::Error::None); + } + + const uint8_t id_only[] = {0x08, 0x07}; + { + sofab::IStreamInto istream{object}; + REQUIRE(istream.feed(id_only, sizeof(id_only)).code() == sofab::Error::None); + } + + REQUIRE(object->id == 7); + REQUIRE(object->value == 3.1415f); +} + +TEST_CASE("IStream: a rejected message leaves what was decoded before the error") +{ + // The price of decoding in place, stated so it is a decision and not a + // surprise: a valid field, then an id varint that overflows. + SimpleObject2 object; + sofab::IStreamInto istream{object}; + + const uint8_t buffer[] = {0x08, 0x2a, 0xF8, 0xFF, 0xFF, 0xFF, 0x7F, 0x00}; + + auto result = istream.feed(buffer, sizeof(buffer)); + REQUIRE(result.code() == sofab::Error::InvalidMessage); + REQUIRE(object->id == 42); +} + // // Round-trip tests: serialize with the OStream API, feed the produced bytes // back through the IStream API and verify the decoded values. These exercise