blob: 62a3ebedde1153783caee8ded4400e1ebb2df1d6 [file] [log] [blame]
Patrick Williamsbf0283a2023-08-19 07:18:04 -05001#include <net/poettering/Calculator/aserver.hpp>
2#include <sdbusplus/async.hpp>
3
4class Calculator :
5 public sdbusplus::aserver::net::poettering::Calculator<Calculator>
6{
7 public:
Patrick Williams3a363a62023-08-22 17:08:15 -05008 explicit Calculator(sdbusplus::async::context& ctx, auto path) :
9 sdbusplus::aserver::net::poettering::Calculator<Calculator>(ctx, path)
10 {}
Patrick Williams72f71162023-08-20 21:36:14 -050011
Patrick Williamse15ff882023-08-22 16:59:16 -050012 auto method_call(multiply_t, auto x, auto y)
13 {
14 auto r = x * y;
15 last_result(r);
16 return r;
17 }
18
19 auto method_call(divide_t, auto x, auto y)
20 -> sdbusplus::async::task<divide_t::return_type>
21 {
Patrick Williams3a363a62023-08-22 17:08:15 -050022 using sdbusplus::net::poettering::Calculator::Error::DivisionByZero;
23 if (y == 0)
24 {
25 status(State::Error);
26 throw DivisionByZero();
27 }
28
Patrick Williamse15ff882023-08-22 16:59:16 -050029 auto r = x / y;
30 last_result(r);
31 co_return r;
32 }
33
34 auto method_call(clear_t) -> sdbusplus::async::task<>
35 {
Patrick Williams3a363a62023-08-22 17:08:15 -050036 auto v = last_result();
Patrick Williamse15ff882023-08-22 16:59:16 -050037 last_result(0);
Patrick Williams3a363a62023-08-22 17:08:15 -050038 cleared(v);
Patrick Williamse15ff882023-08-22 16:59:16 -050039 co_return;
40 }
Patrick Williamsbf0283a2023-08-19 07:18:04 -050041};
42
43int main()
44{
Patrick Williams3a363a62023-08-22 17:08:15 -050045 constexpr auto path = "/net/poettering/calculator";
46
Patrick Williamsbf0283a2023-08-19 07:18:04 -050047 sdbusplus::async::context ctx;
Patrick Williams3a363a62023-08-22 17:08:15 -050048 sdbusplus::server::manager_t manager{ctx, path};
49
50 Calculator c{ctx, path};
51
52 ctx.spawn([](sdbusplus::async::context& ctx) -> sdbusplus::async::task<> {
53 ctx.get_bus().request_name("net.poettering.Calculator");
54 co_return;
55 }(ctx));
Patrick Williamsbf0283a2023-08-19 07:18:04 -050056
57 ctx.run();
58
59 return 0;
60}