Span-Based Packet View

Medium C++ Data Flow Asked at Marvell
Solve this problem

Parse a network packet without copying by providing a non-owning view type with bounds-checked field accessors.

Constraints

  • The view stores a pointer and a length only.
  • Reading beyond the length must return a failure, never read out of bounds.
  • Provide u8, u16be and u32be readers at a byte offset.
  • No allocation, no exceptions.
  • The view must not extend the lifetime of the buffer.
  • Must be constexpr-constructible.
Starter codeC++
1#include <cstdint>2#include <cstddef>3 4class PacketView {5public:6    constexpr PacketView(const uint8_t* data, size_t len);7    bool u16be(size_t offset, uint16_t& out) const;8};