From ff92a987ad05e40a1315306b31bbc4a219d2ee1d Mon Sep 17 00:00:00 2001 From: crupest Date: Sun, 6 Jun 2021 21:33:52 +0800 Subject: import(life): ... --- works/life/computer-network-experiment/Output.h | 46 +++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 works/life/computer-network-experiment/Output.h (limited to 'works/life/computer-network-experiment/Output.h') 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 + +#include +#include +#include + +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_queue; + +inline void SendOutput(Output output) { + output_queue.blockingWrite(std::move(output)); +} + +inline void SendOutput(std::wstring output) { + SendOutput(std::move(output)); +} + +template +void SendOutput(std::wstring_view format, Args &&...args) { + output_queue.blockingWrite(fmt::format(format, std::forward(args)...)); +} + +template +void SendOutput(OutputType type, std::wstring_view format, Args &&...args) { + output_queue.blockingWrite( + Output{fmt::format(format, std::forward(args)...), type}); +} + +void OutputThread(); -- cgit v1.2.3