diff options
author | crupest <crupest@outlook.com> | 2021-06-07 23:24:43 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2021-06-07 23:24:43 +0800 |
commit | 2dd9b0e926ed97b64f0e6c2b6c071feaab9c47a0 (patch) | |
tree | 6a87c790fc03e3e95f14de9a59257aec66ba65b7 /computer-network-experiment/IO.h | |
parent | 1dd5c424e198a59b3572352fd973fe34dad225d1 (diff) | |
download | life-2dd9b0e926ed97b64f0e6c2b6c071feaab9c47a0.tar.gz life-2dd9b0e926ed97b64f0e6c2b6c071feaab9c47a0.tar.bz2 life-2dd9b0e926ed97b64f0e6c2b6c071feaab9c47a0.zip |
...
Diffstat (limited to 'computer-network-experiment/IO.h')
-rw-r--r-- | computer-network-experiment/IO.h | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/computer-network-experiment/IO.h b/computer-network-experiment/IO.h new file mode 100644 index 0000000..b0cf489 --- /dev/null +++ b/computer-network-experiment/IO.h @@ -0,0 +1,66 @@ +#pragma once
+#include "Common.h"
+#include "StringUtil.hpp"
+
+#include <fmt/format.h>
+#include <folly/CancellationToken.h>
+#include <folly/MPMCPipeline.h>
+#include <folly/MPMCQueue.h>
+
+#include <iostream>
+#include <thread>
+
+enum class OutputType { Normal, Error };
+enum class OutputColor { Normal, Green, Red, Yellow };
+
+struct Output {
+ Output() = default;
+ Output(String message, OutputType type = OutputType::Normal)
+ : message(std::move(message)), type(type),
+ color(type == OutputType::Error ? OutputColor::Red
+ : OutputColor::Normal) {}
+
+ Output(String message, OutputColor color)
+ : message(std::move(message)), type(OutputType::Normal), color(color) {}
+
+ Output(String message, OutputType type, OutputColor color)
+ : message(std::move(message)), type(type), color(color) {}
+
+ CRU_DEFAULT_COPY(Output)
+ CRU_DEFAULT_MOVE(Output)
+ ~Output() = default;
+
+ String message;
+ OutputType type;
+ OutputColor color;
+};
+
+extern folly::MPMCQueue<Output> output_queue;
+
+inline void SendOutput(Output output) {
+ output_queue.blockingWrite(std::move(output));
+}
+
+inline void SendOutput(String output) { SendOutput(Output{std::move(output)}); }
+
+template <typename... Args> void SendOutput(StringView format, Args &&...args) {
+ output_queue.blockingWrite(fmt::format(format, std::forward<Args>(args)...));
+}
+
+template <typename... Args>
+void SendOutput(OutputType type, StringView format, Args &&...args) {
+ output_queue.blockingWrite(
+ Output{fmt::format(format, std::forward<Args>(args)...), type});
+}
+
+template <typename... Args>
+void SendOutput(OutputColor color, StringView format, Args &&...args) {
+ output_queue.blockingWrite(
+ Output{fmt::format(format, std::forward<Args>(args)...), color});
+}
+
+void SignalAndWaitForOutputThreadStop();
+
+void OnInputLine(StringView line);
+
+void StartIOThread();
|