blob: dd9ac7e45afd623c38e9ee8cf2a509a6f709902a (
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
|
/*
* Copyright (c) 2020-2023 Vasile Vilvoiu <vasi@vilvoiu.ro>
*
* specgram is free software; you can redistribute it and/or modify
* it under the terms of the MIT license. See LICENSE for details.
*/
#ifndef _INPUT_READER_HPP_
#define _INPUT_READER_HPP_
#include <istream>
#include <mutex>
#include <optional>
#include <thread>
#include <vector>
/**
* Input reader base class
*/
class InputReader {
protected:
std::istream * const stream_; /* stream we are straddling */
const std::size_t block_size_bytes_; /* the size of a block in bytes */
/**
* Retrieves an internal buffer that may or may not be block sized.
* @return Buffer.
*/
virtual std::vector<char> GetBuffer() = 0;
public:
InputReader() = delete;
InputReader(const InputReader&) = delete;
InputReader(InputReader&&) = delete;
InputReader & operator=(const InputReader&) = delete;
/**
* @param stream Input stream to use.
* @param block_size_bytes Block size in bytes.
*/
InputReader(std::istream * stream, std::size_t block_size_bytes);
virtual ~InputReader() = default;
/**
* @return True if end of stream was received.
*/
virtual bool ReachedEOF() const = 0;
/**
* @return A block of bytes, if such a block exists.
*/
virtual std::optional<std::vector<char>> GetBlock() = 0;
};
/**
* Synchronous input reader specialization
*/
class SyncInputReader : public InputReader {
protected:
std::vector<char> GetBuffer() override;
public:
SyncInputReader(std::istream * stream, std::size_t block_size_bytes);
bool ReachedEOF() const override;
std::optional<std::vector<char>> GetBlock() override;
};
/**
* Asynchronous input reader specialization.
*/
class AsyncInputReader : public InputReader {
private:
/* buffer where we read up until one block */
volatile char *buffer_;
volatile std::size_t bytes_in_buffer_;
/* mutex for accessing buffer */
std::mutex mutex_;
/* thread for reading from input stream */
std::thread reader_thread_;
volatile bool running_;
void Read();
protected:
std::vector<char> GetBuffer() override;
public:
AsyncInputReader(std::istream * stream, std::size_t block_size_bytes);
~AsyncInputReader() override;
bool ReachedEOF() const override; /* no EOF support is assumed in async input */
std::optional<std::vector<char>> GetBlock() override;
};
#endif
|