2019-07-27 17:37:48 +03:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <AK/StdLibExtras.h>
|
2019-07-30 16:16:14 +03:00
|
|
|
#include <LibCore/CIODevice.h>
|
2019-07-27 17:37:48 +03:00
|
|
|
|
2019-07-30 16:16:14 +03:00
|
|
|
class CIODeviceStreamReader {
|
2019-07-27 17:37:48 +03:00
|
|
|
public:
|
2019-07-30 16:16:14 +03:00
|
|
|
CIODeviceStreamReader(CIODevice& device)
|
|
|
|
: m_device(device)
|
2019-07-27 17:37:48 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool handle_read_failure()
|
|
|
|
{
|
|
|
|
return exchange(m_had_failure, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2019-07-30 16:16:14 +03:00
|
|
|
CIODeviceStreamReader& operator>>(T& value)
|
2019-07-27 17:37:48 +03:00
|
|
|
{
|
2019-07-30 16:16:14 +03:00
|
|
|
int nread = m_device.read((u8*)&value, sizeof(T));
|
2019-07-27 17:37:48 +03:00
|
|
|
ASSERT(nread == sizeof(T));
|
|
|
|
if (nread != sizeof(T))
|
|
|
|
m_had_failure = true;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2019-07-30 16:16:14 +03:00
|
|
|
CIODevice& m_device;
|
2019-07-27 17:37:48 +03:00
|
|
|
bool m_had_failure { false };
|
|
|
|
};
|