parselink-old/source/include/parselink/msgpack/token/reader.h
2023-09-13 19:31:43 -07:00

63 lines
2.1 KiB
C++

//-----------------------------------------------------------------------------
// ___ __ _ _
// / _ \__ _ _ __ ___ ___ / /(_)_ __ | | __
// / /_)/ _` | '__/ __|/ _ \/ / | | '_ \| |/ /
// / ___/ (_| | | \__ \ __/ /__| | | | | <
// \/ \__,_|_| |___/\___\____/_|_| |_|_|\_\ .
//
//-----------------------------------------------------------------------------
// 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 <tl/expected.hpp>
namespace msgpack {
class token_reader {
public:
enum class error {
end_of_message,
incomplete_message
};
constexpr token_reader(std::span<std::byte const> 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<token, error> 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<std::span<token>, error> read_some(
std::span<token> token_buffer) noexcept;
private:
std::span<std::byte const> data_;
std::size_t curr_;
std::size_t end_;
};
} // namespace msgpack
#endif // msgpack_token_reader_8daff350a0b1a519