Bitcoin Core 29.99.0
P2P Digital Currency
calculator.cpp
Go to the documentation of this file.
1// Copyright (c) The Bitcoin Core developers
2// Distributed under the MIT software license, see the accompanying
3// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5#include <calculator.h>
6#include <init.capnp.h>
7#include <init.capnp.proxy.h> // NOLINT(misc-include-cleaner) // IWYU pragma: keep
8
9#include <charconv>
10#include <cstring>
11#include <fstream>
12#include <iostream>
13#include <kj/async.h>
14#include <kj/common.h>
15#include <kj/memory.h>
16#include <memory>
17#include <mp/proxy-io.h>
18#include <stdexcept>
19#include <string>
20#include <system_error>
21#include <utility>
22
24{
25public:
26 CalculatorImpl(std::unique_ptr<Printer> printer) : m_printer(std::move(printer)) {}
27 void solveEquation(const std::string& eqn) override { m_printer->print("Wow " + eqn + ", that's a tough one.\n"); }
28 std::unique_ptr<Printer> m_printer;
29};
30
31class InitImpl : public Init
32{
33public:
34 std::unique_ptr<Calculator> makeCalculator(std::unique_ptr<Printer> printer) override
35 {
36 return std::make_unique<CalculatorImpl>(std::move(printer));
37 }
38};
39
40static void LogPrint(bool raise, const std::string& message)
41{
42 if (raise) throw std::runtime_error(message);
43 std::ofstream("debug.log", std::ios_base::app) << message << std::endl;
44}
45
46int main(int argc, char** argv)
47{
48 if (argc != 2) {
49 std::cout << "Usage: mpcalculator <fd>\n";
50 return 1;
51 }
52 int fd;
53 if (std::from_chars(argv[1], argv[1] + strlen(argv[1]), fd).ec != std::errc{}) {
54 std::cerr << argv[1] << " is not a number or is larger than an int\n";
55 return 1;
56 }
57 mp::EventLoop loop("mpcalculator", LogPrint);
58 std::unique_ptr<Init> init = std::make_unique<InitImpl>();
59 mp::ServeStream<InitInterface>(loop, fd, *init);
60 loop.loop();
61 return 0;
62}
int main(int argc, char **argv)
Definition: calculator.cpp:46
static void LogPrint(bool raise, const std::string &message)
Definition: calculator.cpp:40
CalculatorImpl(std::unique_ptr< Printer > printer)
Definition: calculator.cpp:26
void solveEquation(const std::string &eqn) override
Definition: calculator.cpp:27
std::unique_ptr< Printer > m_printer
Definition: calculator.cpp:28
Definition: init.h:13
std::unique_ptr< Calculator > makeCalculator(std::unique_ptr< Printer > printer) override
Definition: calculator.cpp:34
Event loop implementation.
Definition: proxy-io.h:161
void loop()
Run event loop.
Definition: proxy.cpp:214