summaryrefslogtreecommitdiff
path: root/src/input-reader.hpp
diff options
context:
space:
mode:
authorVasile Vilvoiu <vasi.vilvoiu@gmail.com>2020-12-29 19:33:03 +0200
committerVasile Vilvoiu <vasi.vilvoiu@gmail.com>2020-12-29 19:33:03 +0200
commit26293db40f8ac62f3971e0e9dbbc0bf3439e61c0 (patch)
tree218c93aba851c3c3123e9e72d25c974aa65cfd52 /src/input-reader.hpp
Initial commit
Diffstat (limited to 'src/input-reader.hpp')
-rw-r--r--src/input-reader.hpp77
1 files changed, 77 insertions, 0 deletions
diff --git a/src/input-reader.hpp b/src/input-reader.hpp
new file mode 100644
index 0000000..71025c8
--- /dev/null
+++ b/src/input-reader.hpp
@@ -0,0 +1,77 @@
+/*
+ * Copyright (c) 2020-2021 Vasile Vilvoiu <vasi.vilvoiu@gmail.com>
+ *
+ * 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_;
+ const std::size_t block_size_bytes_;
+
+public:
+ InputReader() = delete;
+ InputReader(const InputReader&) = delete;
+ InputReader(InputReader&&) = delete;
+ InputReader & operator=(const InputReader&) = delete;
+
+ InputReader(std::istream * stream, std::size_t block_size_bytes);
+ virtual ~InputReader() = default;
+
+ virtual bool ReachedEOF() const = 0;
+ virtual std::optional<std::vector<char>> GetBlock() = 0;
+ virtual std::vector<char> GetBuffer() = 0;
+};
+
+/*
+ * Synchronous input reader
+ */
+class SyncInputReader : public InputReader {
+public:
+ SyncInputReader(std::istream * stream, std::size_t block_size_bytes);
+
+ bool ReachedEOF() const override;
+ std::optional<std::vector<char>> GetBlock() override;
+ std::vector<char> GetBuffer() override;
+};
+
+/*
+ * Asynchronous input reader
+ */
+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();
+
+public:
+ AsyncInputReader(std::istream * stream, std::size_t block_size_bytes);
+ ~AsyncInputReader() override;
+
+ bool ReachedEOF() const override;
+ std::optional<std::vector<char>> GetBlock() override;
+ std::vector<char> GetBuffer() override;
+};
+
+#endif