From a72854edc6d3927c7be9740096590ba6ba5994a5 Mon Sep 17 00:00:00 2001 From: Andreas Steinbart Date: Tue, 13 Oct 2026 20:44:51 +0000 Subject: [PATCH] feat: IStreamInto decodes into a message the caller owns MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit IStreamObject owns the message it fills, so a caller who already has one -- which every generated try_decode does -- cannot hand it over. The generator worked around that with an IStreamInline whose callback forwards to the destination, and that shape needs the stream inside its own callback: the lambda cannot capture the object it is being handed to, so the generated code reached it through a pointer patched in right after construction. IStreamInto is IStreamObject with the instance taken out: it routes each top-level field into the message handed to its constructor and holds a pointer where IStreamObject holds a MessageType. Same callback, same dispatch, no knot -- try_decode becomes three lines, and the same three in both C++ profiles (corelib-c-cpp#197 adds the counterpart there, sofa-buffers/generator#744). The contract is IStreamObject's, with one difference the doc states: the destination is the caller's, so putting it back between messages is the caller's too. reset() here resets the DECODER alone -- generated code calls the message's own reset(), which keeps the buffers it has already paid for, the reason to hand a destination in at all. A rejected input leaves the fields decoded before the error in it, as it always has for an object decoded into. Tests: the caller's object is the one written (all five checks fail if the stream stages in one of its own -- verified by mutation), what the caller cleared stays cleared, an absent field keeps the previous value (§2, stated because it is the contract), and a rejected message leaves what it had decoded. README gains the three-line form beside the reset() section. --- README.md | 17 ++++++++++ include/sofab/sofab.hpp | 56 ++++++++++++++++++++++++++++++++ test/test_roundtrip.cpp | 72 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 145 insertions(+) diff --git a/README.md b/README.md index 9fe4efd..ac9d21a 100644 --- a/README.md +++ b/README.md @@ -197,6 +197,23 @@ wrapper sequence is present. MESSAGE_SPEC §5.1 places this duty on the decoding side. (`IStreamInline` has the same `reset()` but owns no destination; a callback-driven decoder clears its own targets.) +#### Decoding into an object you already hold — `IStreamInto` + +`IStreamObject` owns the message it fills, so a caller who already has one can +only decode into the stream's instance and copy it across afterwards. Bind the +destination instead: + +```cpp +Sensor dst; // yours, reused +sofab::IStreamInto in{dst, sofab::Limits{64 * 1024}}; +in.feed(a.data(), a.size()); // writes straight into dst +``` + +Nothing is staged and copied, and `dst` keeps the buffers it has already paid +for. Everything above still holds, with one difference: the destination is +yours, so putting it back between messages is yours too — `reset()` here +resets the decoder alone. + ### Deserialize stream `feed()` takes whatever bytes have arrived; a field straddling a chunk boundary diff --git a/include/sofab/sofab.hpp b/include/sofab/sofab.hpp index 69288ec..d2fb4de 100644 --- a/include/sofab/sofab.hpp +++ b/include/sofab/sofab.hpp @@ -6264,6 +6264,62 @@ namespace sofab const MessageType &operator*() const noexcept { return data_; } }; + /** + * @brief Decoder that decodes into a message instance the CALLER owns. + * + * @ref IStreamObject with the instance taken out: it routes each top-level + * field into the message handed to the constructor instead of one it holds + * itself. That is the whole difference — same callback, same dispatch — and + * it is what a caller who already has a destination needs, which is every + * generated `try_decode` (sofa-buffers/generator#744). The alternative, a + * hand-written @ref IStreamInline whose callback forwards to the + * destination, needs the stream inside its own callback and so cannot + * capture it; this class is that shape without the knot. + * + * @warning **One message per stream, unless the destination is put back.** + * Everything @ref IStreamObject warns about applies here, for the + * same reason — the message is populated in place and MESSAGE_SPEC + * §2 leaves an all-default field out of the bytes, so a field the + * next message does not carry keeps the previous one's value. The + * difference is who clears it: the destination is the caller's, so + * @ref reset here resets the DECODER only and the caller puts the + * message back (generated code calls the message's own `reset()`, + * which keeps the buffers it has already paid for — the reason to + * hand a destination in at all). A rejected input likewise leaves + * the fields decoded before the error in it. + * + * The message must outlive the stream. + * + * ```cpp + * Telemetry msg; // kept across messages + * msg.reset(); + * sofab::IStreamInto in{msg, limits}; + * auto r = in.feed(bytes, len); + * ``` + * + * @tparam MessageType A type satisfying @ref sofab::InputMessage. + */ + template + class IStreamInto : public IStreamImpl + { + MessageType *data_; + + public: + /** + * Bind @p message and route each top-level field into it. + * @param message The destination; must outlive the stream. + * @param limits The receiver's field-span budget (see @ref Limits). + * **Required**: §6.2.1 gives this library no number to default to. + */ + explicit IStreamInto(MessageType &message, Limits limits) noexcept + : IStreamImpl(limits), data_{&message} + { + topCallback_ = [this](sofab::id id, size_t size, size_t count) { + data_->deserialize(*this, id, size, count); + }; + } + }; + /* ---------------------------------------------------------------------- */ /* The static helper layer (§6.6.1) */ /* */ diff --git a/test/test_roundtrip.cpp b/test/test_roundtrip.cpp index b5a4775..066e39d 100644 --- a/test/test_roundtrip.cpp +++ b/test/test_roundtrip.cpp @@ -5992,6 +5992,77 @@ static void heapFreeStorage() * PRESENT. §5.1 puts the duty to supply a clean destination on the decoding * side, and reset() is how a caller discharges it. --- */ +/* --- IStreamInto: the same decode, into a destination the caller owns ------- + * + * What every generated try_decode needs (sofa-buffers/generator#744): the + * caller already has the message, so nothing should be staged in the stream and + * copied across. The checks below are the three things that makes observable -- + * the caller's object is the one written, its buffers survive a reuse the + * caller clears with the message's own reset, and the price of writing in place + * is a rejected message leaving what it had decoded behind. --- */ + +static void decodeIntoCallerObject() +{ + const auto msgA_tags = fromHex("0e020a780a0a7907"); /* tags = [x, y] */ + const auto msgB = fromHex("4801"); /* only id 9 = 1 */ + + { + SeqMsg dst; + sofab::IStreamInto in{dst, kMaxSpan}; + auto r = in.feed(msgA_tags.data(), msgA_tags.size()); + CHECK(r.complete(), "into: the caller's message decodes COMPLETE"); + /* Would both be empty if the stream had decoded into one of its own. */ + CHECK(dst.tags.size() == 2, "into: the caller's object is the one written"); + CHECK(dst.tags.size() == 2 && dst.tags[1] == "y", + "into: ...with the values, not a copy of nothing"); + } + { + /* Reuse is the caller's to clear, and clearing it their way keeps the + * storage: a std::string handed back here would have to be allocated + * again on the next message. */ + SeqMsg dst; + { + sofab::IStreamInto in{dst, kMaxSpan}; + in.feed(msgA_tags.data(), msgA_tags.size()); + } + const char *kept = dst.tags.empty() ? nullptr : dst.tags[0].c_str(); + (void)kept; + dst.tags.clear(); + dst.other = 0; + { + sofab::IStreamInto in{dst, kMaxSpan}; + auto r = in.feed(msgB.data(), msgB.size()); + CHECK(r.complete(), "into: the next message decodes into the same object"); + } + CHECK(dst.other == 1, "into: the next message's own field is decoded"); + CHECK(dst.tags.empty(), "into: what the caller cleared stays cleared"); + } + { + /* Without that clear, §2 shows through: an absent field keeps what the + * object held. Stated here because it is the contract, not a bug. */ + SeqMsg dst; + { + sofab::IStreamInto in{dst, kMaxSpan}; + in.feed(msgA_tags.data(), msgA_tags.size()); + } + { + sofab::IStreamInto in{dst, kMaxSpan}; + in.feed(msgB.data(), msgB.size()); + } + CHECK(dst.tags.size() == 2, + "into: an absent field keeps the previous value -- the caller resets (§2)"); + } + { + /* The price of writing in place: a valid field, then a >64-bit varint. */ + SeqMsg dst; + sofab::IStreamInto in{dst, kMaxSpan}; + const auto bad = fromHex("4809" "00808080808080808080" "02"); + auto r = in.feed(bad.data(), bad.size()); + CHECK(r.code() == sofab::Error::InvalidMessage, "into: the malformed message is rejected"); + CHECK(dst.other == 9, "into: a rejected message leaves what was decoded before the error"); + } +} + static void destinationReuse() { const auto msgA_tags = fromHex("0e020a780a0a7907"); /* tags = [x, y] */ @@ -8979,6 +9050,7 @@ int main() encodeBoundRefusalAndAssignmentClamp(); allocationMeasurement(); destinationReuse(); + decodeIntoCallerObject(); varintWidthSweep(); trailingDefaultsStayOnTheWire(); nestedArrayCountCeiling();