mpd/src/io/Reader.hxx

40 lines
822 B
C++
Raw Normal View History

// 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
#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
* 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