summaryrefslogtreecommitdiff
path: root/Libraries/LibCore/CIODeviceStreamReader.h
blob: 60d26c3fcf53e175d170196390afad5fcb62ac92 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#pragma once

#include <AK/StdLibExtras.h>
#include <LibCore/CIODevice.h>

class CIODeviceStreamReader {
public:
    CIODeviceStreamReader(CIODevice& device)
        : m_device(device)
    {
    }

    bool handle_read_failure()
    {
        return exchange(m_had_failure, false);
    }

    template<typename T>
    CIODeviceStreamReader& operator>>(T& value)
    {
        int nread = m_device.read((u8*)&value, sizeof(T));
        ASSERT(nread == sizeof(T));
        if (nread != sizeof(T))
            m_had_failure = true;
        return *this;
    }

private:
    CIODevice& m_device;
    bool m_had_failure { false };
};