Oregami
Repositories/oxedyne/fe2o3

oxedyne/fe2o3/fe2o3_text/tests/detect_corpus/cpp/modern_cpp.txt

1.9 KiB, 1 run

created by r1870400018:11950, which is this file's identity for as long as the history lasts, whatever it is later renamed to

download · who wrote it · its history

1#include <optional>
2#include <variant>
3#include <functional>
4#include <string>
5#include <iostream>
6
7using Callback = std::function<void(const std::string&)>;
8
9struct Config {
10 std::string host = "localhost";
11 int port = 8080;
12 std::optional<std::string> tls_cert;
13 bool verbose = false;
14};
15
16using ParseResult = std::variant<Config, std::string>;
17
18ParseResult parse_args(int argc, const char* argv[]) {
19 Config cfg;
20 for (int i = 1; i < argc; ++i) {
21 std::string arg = argv[i];
22 if (arg == "--verbose") {
23 cfg.verbose = true;
24 } else if (arg == "--host" && i + 1 < argc) {
25 cfg.host = argv[++i];
26 } else if (arg == "--port" && i + 1 < argc) {
27 cfg.port = std::stoi(argv[++i]);
28 } else if (arg == "--tls-cert" && i + 1 < argc) {
29 cfg.tls_cert = argv[++i];
30 } else {
31 return "unknown argument: " + arg;
32 }
33 }
34 return cfg;
35}
36
37class Server {
38public:
39 explicit Server(Config config)
40 : config_(std::move(config)) {}
41
42 void on_request(Callback cb) {
43 handler_ = std::move(cb);
44 }
45
46 void start() {
47 if (config_.verbose) {
48 std::cout << "starting on " << config_.host
49 << ":" << config_.port << "\n";
50 }
51 if (config_.tls_cert.has_value()) {
52 std::cout << "TLS: " << config_.tls_cert.value() << "\n";
53 }
54 }
55
56private:
57 Config config_;
58 Callback handler_;
59};
60
61int main(int argc, const char* argv[]) {
62 auto result = parse_args(argc, argv);
63
64 if (auto* err = std::get_if<std::string>(&result)) {
65 std::cerr << "error: " << *err << "\n";
66 return 1;
67 }
68
69 auto cfg = std::get<Config>(result);
70 Server srv(std::move(cfg));
71 srv.on_request([](const std::string& req) {
72 std::cout << "request: " << req << "\n";
73 });
74 srv.start();
75
76 return 0;
77}