aboutsummaryrefslogtreecommitdiff
path: root/works/life/computer-network-experiment/IO.h
diff options
context:
space:
mode:
authorYuqian Yang <crupest@crupest.life>2025-02-12 15:55:21 +0800
committerYuqian Yang <crupest@crupest.life>2025-02-12 15:55:21 +0800
commit10eb95869601e145b1d8bc909424777c25752d51 (patch)
tree49449a4076ded9bd937a51679318edbe2a532cae /works/life/computer-network-experiment/IO.h
parent29ba3e88b1a7425fe00af0005b8a8228103aa21c (diff)
parentf8c10dd1fc55e60f35286475356e48c4f642eb63 (diff)
downloadcrupest-10eb95869601e145b1d8bc909424777c25752d51.tar.gz
crupest-10eb95869601e145b1d8bc909424777c25752d51.tar.bz2
crupest-10eb95869601e145b1d8bc909424777c25752d51.zip
import(life): IMPORT crupest/life COMPLETE.
Diffstat (limited to 'works/life/computer-network-experiment/IO.h')
-rw-r--r--works/life/computer-network-experiment/IO.h68
1 files changed, 68 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..1658b78
--- /dev/null
+++ b/works/life/computer-network-experiment/IO.h
@@ -0,0 +1,68 @@
+#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();
+
+String ReadInputLine(); \ No newline at end of file