2023-03-06 14:42:04 +01:00
|
|
|
// SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
// author: Max Kellermann <max.kellermann@gmail.com>
|
2014-08-07 18:10:23 +02:00
|
|
|
|
2019-12-16 17:02:35 +01:00
|
|
|
#ifndef READER_HXX
|
|
|
|
#define READER_HXX
|
2014-08-07 18:10:23 +02:00
|
|
|
|
2020-03-13 00:46:28 +01:00
|
|
|
#include <cstddef>
|
2023-10-05 10:33:51 +02:00
|
|
|
#include <type_traits>
|
2014-08-07 18:10:23 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
2015-12-16 11:05:33 +01:00
|
|
|
* on end-of-stream
|
2014-08-07 18:10:23 +02:00
|
|
|
*/
|
2021-10-13 11:28:04 +02:00
|
|
|
[[gnu::nonnull]]
|
2021-12-07 11:49:59 +01:00
|
|
|
virtual std::size_t Read(void *data, std::size_t size) = 0;
|
2023-10-05 10:33:51 +02:00
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
requires std::is_standard_layout_v<T> && std::is_trivially_copyable_v<T>
|
|
|
|
void ReadT(T &dest) {
|
|
|
|
// TODO check return value
|
|
|
|
Read(&dest, sizeof(dest));
|
|
|
|
}
|
2014-08-07 18:10:23 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|