2020-01-18 09:38:21 +01:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 09:38:21 +01:00
|
|
|
*/
|
|
|
|
|
|
2019-07-27 16:37:48 +02:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include <AK/StdLibExtras.h>
|
2020-02-06 15:04:03 +01:00
|
|
|
#include <LibCore/IODevice.h>
|
2019-07-27 16:37:48 +02:00
|
|
|
|
2020-02-02 12:34:39 +01:00
|
|
|
namespace Core {
|
|
|
|
|
|
|
|
|
|
class IODeviceStreamReader {
|
2019-07-27 16:37:48 +02:00
|
|
|
public:
|
2020-02-02 12:34:39 +01:00
|
|
|
IODeviceStreamReader(IODevice& device)
|
2019-07-30 15:16:14 +02:00
|
|
|
: m_device(device)
|
2019-07-27 16:37:48 +02:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool handle_read_failure()
|
|
|
|
|
{
|
|
|
|
|
return exchange(m_had_failure, false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<typename T>
|
2020-02-02 12:34:39 +01:00
|
|
|
IODeviceStreamReader& operator>>(T& value)
|
2019-07-27 16:37:48 +02:00
|
|
|
{
|
2019-07-30 15:16:14 +02:00
|
|
|
int nread = m_device.read((u8*)&value, sizeof(T));
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(nread == sizeof(T));
|
2019-07-27 16:37:48 +02:00
|
|
|
if (nread != sizeof(T))
|
|
|
|
|
m_had_failure = true;
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
2020-02-02 12:34:39 +01:00
|
|
|
IODevice& m_device;
|
2019-07-27 16:37:48 +02:00
|
|
|
bool m_had_failure { false };
|
|
|
|
|
};
|
2020-02-02 12:34:39 +01:00
|
|
|
|
|
|
|
|
}
|