summaryrefslogtreecommitdiffstats
path: root/vbench/src/vbench/core/byte_input.h
blob: 7294c9682bbb5b7db300e4fad090f6462e7a5527 (plain) (blame)
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#pragma once

#include "input.h"

namespace vbench {

/**
 * Concrete utility class used to read input data one byte at a time
 * by wrapping a generic Input implementation.
 **/
class ByteInput
{
private:
    Input  &_input;
    Memory  _data;
    size_t  _pos;

public:
    /**
     * Wrap an Input to read one byte at a time.
     *
     * @param input the underlying Input
     **/
    ByteInput(Input &input)
        : _input(input), _data(), _pos(0) {}
    ~ByteInput() { _input.evict(_pos); }

    /**
     * Read the next byte of input.
     *
     * @return next byte of input, or -1 if no more input is available
     **/
    int get() {
        if (_pos < _data.size) {
            return (_data.data[_pos++] & 0xff);
        } else {
            _data = _input.evict(_pos).obtain();
            if ((_pos = 0) < _data.size) {
                return (_data.data[_pos++] & 0xff);
            }
        }
        return -1;
    }
};

} // namespace vbench