Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<Sensor> 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
Expand Down
56 changes: 56 additions & 0 deletions include/sofab/sofab.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<Telemetry> in{msg, limits};
* auto r = in.feed(bytes, len);
* ```
*
* @tparam MessageType A type satisfying @ref sofab::InputMessage.
*/
template <InputMessage MessageType>
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) */
/* */
Expand Down
72 changes: 72 additions & 0 deletions test/test_roundtrip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<SeqMsg> 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<SeqMsg> 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<SeqMsg> 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<SeqMsg> in{dst, kMaxSpan};
in.feed(msgA_tags.data(), msgA_tags.size());
}
{
sofab::IStreamInto<SeqMsg> 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<SeqMsg> 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] */
Expand Down Expand Up @@ -8979,6 +9050,7 @@ int main()
encodeBoundRefusalAndAssignmentClamp();
allocationMeasurement();
destinationReuse();
decodeIntoCallerObject();
varintWidthSweep();
trailingDefaultsStayOnTheWire();
nestedArrayCountCeiling();
Expand Down
Loading