aboutsummaryrefslogtreecommitdiff
path: root/works/life/computer-network-experiment/IO.h
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2021-06-07 23:24:43 +0800
committercrupest <crupest@outlook.com>2021-06-07 23:24:43 +0800
commit9c42586bc406015be0145576fd6cb3586686b4ca (patch)
tree92d0a84756dcc587f7e514ab98c5584e81852cc1 /works/life/computer-network-experiment/IO.h
parent08325faff52d0704c6f17d065ca8d72ea07ca6a0 (diff)
downloadcrupest-9c42586bc406015be0145576fd6cb3586686b4ca.tar.gz
crupest-9c42586bc406015be0145576fd6cb3586686b4ca.tar.bz2
crupest-9c42586bc406015be0145576fd6cb3586686b4ca.zip
import(life): ...
Diffstat (limited to 'works/life/computer-network-experiment/IO.h')
-rw-r--r--works/life/computer-network-experiment/IO.h66
1 files changed, 66 insertions, 0 deletions
diff --git a/works/life/computer-network-experiment/IO.h b/works/life/computer-network-experiment/IO.h
new file mode 100644
index 0000000..b0cf489
--- /dev/null
+++ b/works/life/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();