// SPDX-License-Identifier: BSD-2-Clause // author: Max Kellermann #pragma once #include #include #include /** * An interface that can read bytes from a stream until the stream * ends. * * This interface is simpler and less cumbersome to use than * #InputStream. */ class Reader { public: Reader() = default; Reader(const Reader &) = delete; /** * Read data from the stream. * * @return the number of bytes read into the given buffer or 0 * on end-of-stream */ [[nodiscard]] virtual std::size_t Read(std::span dest) = 0; /** * Like Read(), but throws an exception when there is not * enough data to fill the destination buffer. */ void ReadFull(std::span dest); template requires std::is_standard_layout_v && std::is_trivially_copyable_v void ReadT(T &dest) { ReadFull(std::as_writable_bytes(std::span{&dest, 1})); } };