diff options
author | crupest <crupest@outlook.com> | 2021-06-06 21:33:52 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2021-06-06 21:33:52 +0800 |
commit | ff92a987ad05e40a1315306b31bbc4a219d2ee1d (patch) | |
tree | 1dcaea374c9888877d9c583fbfedf2a1b8fd0180 /works/life/computer-network-experiment/Output.h | |
parent | 6d8fecb163a9c813a1b533970997353d33b6bf5e (diff) | |
download | crupest-ff92a987ad05e40a1315306b31bbc4a219d2ee1d.tar.gz crupest-ff92a987ad05e40a1315306b31bbc4a219d2ee1d.tar.bz2 crupest-ff92a987ad05e40a1315306b31bbc4a219d2ee1d.zip |
import(life): ...
Diffstat (limited to 'works/life/computer-network-experiment/Output.h')
-rw-r--r-- | works/life/computer-network-experiment/Output.h | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/works/life/computer-network-experiment/Output.h b/works/life/computer-network-experiment/Output.h new file mode 100644 index 0000000..0e53363 --- /dev/null +++ b/works/life/computer-network-experiment/Output.h @@ -0,0 +1,46 @@ +#pragma once
+#include "StringUtil.hpp"
+
+#include <iostream>
+
+#include <fmt/format.h>
+#include <folly/MPMCPipeline.h>
+#include <folly/MPMCQueue.h>
+
+enum class OutputType { Normal, Error };
+
+struct Output {
+ Output() = default;
+ Output(std::wstring message, OutputType type = OutputType::Normal)
+ : message(std::move(message)), type(type) {}
+
+ CRU_DEFAULT_COPY(Output)
+ CRU_DEFAULT_MOVE(Output)
+ ~Output() = default;
+
+ std::wstring message;
+ OutputType type;
+};
+
+extern folly::MPMCQueue<Output> output_queue;
+
+inline void SendOutput(Output output) {
+ output_queue.blockingWrite(std::move(output));
+}
+
+inline void SendOutput(std::wstring output) {
+ SendOutput(std::move(output));
+}
+
+template <typename... Args>
+void SendOutput(std::wstring_view format, Args &&...args) {
+ output_queue.blockingWrite(fmt::format(format, std::forward<Args>(args)...));
+}
+
+template <typename... Args>
+void SendOutput(OutputType type, std::wstring_view format, Args &&...args) {
+ output_queue.blockingWrite(
+ Output{fmt::format(format, std::forward<Args>(args)...), type});
+}
+
+void OutputThread();
|