//----------------------------------------------------------------------------- // ___ __ _ _ // / _ \__ _ _ __ ___ ___ / /(_)_ __ | | __ // / /_)/ _` | '__/ __|/ _ \/ / | | '_ \| |/ / // / ___/ (_| | | \__ \ __/ /__| | | | | < // \/ \__,_|_| |___/\___\____/_|_| |_|_|\_\ . // //----------------------------------------------------------------------------- // Author: Kurt Sassenrath // Module: msgpack // // Token-based reader for unpacking MessagePack data. Ensure lifetime of the // MessagePack buffer exceeds the lifetime of parsed tokens. // // TBD: How best to handle arrays and maps. // // Copyright (c) 2023 Kurt Sassenrath. // // License TBD. //----------------------------------------------------------------------------- #ifndef msgpack_token_reader_8daff350a0b1a519 #define msgpack_token_reader_8daff350a0b1a519 #include "type.h" #include namespace msgpack { class token_reader { public: enum class error { end_of_message, incomplete_message }; constexpr token_reader(std::span src) noexcept : data_(src), end_(src.size()) {} // Read the next token. If the reader currently points to the end of the // byte buffer, then end_of_message is returned, and if there is still // some data present in the buffer, then incomplete_message is returned, // potentially hinting that further buffering is required. constexpr tl::expected read_one() noexcept; // Read multiple tokens from the byte buffer. The number of tokens parsed // can be surmised from the returned span of tokens. If the reader // currently points to the end of the byte buffer, then // error::end_of_message is returned, and if there is not enough data to // fully parse a token, then incomplete_message is returned. constexpr tl::expected, error> read_some( std::span token_buffer) noexcept; private: std::span data_; std::size_t curr_; std::size_t end_; }; } // namespace msgpack #endif // msgpack_token_reader_8daff350a0b1a519