1 #include <net/poettering/Calculator/aserver.hpp>
2 #include <sdbusplus/async.hpp>
3 
4 #include <iostream>
5 
6 class Calculator :
7     public sdbusplus::aserver::net::poettering::Calculator<Calculator>
8 {
9   public:
Calculator(sdbusplus::async::context & ctx,auto path)10     explicit Calculator(sdbusplus::async::context& ctx, auto path) :
11         sdbusplus::aserver::net::poettering::Calculator<Calculator>(ctx, path)
12     {}
13 
method_call(multiply_t,auto x,auto y)14     auto method_call(multiply_t, auto x, auto y)
15     {
16         auto r = x * y;
17         last_result(r);
18         return r;
19     }
20 
method_call(divide_t,auto x,auto y)21     auto method_call(divide_t, auto x, auto y)
22         -> sdbusplus::async::task<divide_t::return_type>
23     {
24         using sdbusplus::error::net::poettering::Calculator::DivisionByZero;
25         if (y == 0)
26         {
27             status(State::Error);
28             throw DivisionByZero();
29         }
30 
31         auto r = x / y;
32         last_result(r);
33         co_return r;
34     }
35 
method_call(clear_t)36     auto method_call(clear_t) -> sdbusplus::async::task<>
37     {
38         auto v = last_result();
39         last_result(0);
40         cleared(v);
41         co_return;
42     }
43 
get_property(owner_t) const44     auto get_property(owner_t) const
45     {
46         std::cout << " get_property on owner\n";
47         return owner_;
48     }
49 
set_property(owner_t,auto owner)50     bool set_property(owner_t, auto owner)
51     {
52         std::cout << " set_property on owner\n";
53         std::swap(owner_, owner);
54         return owner_ == owner;
55     }
56 };
57 
main()58 int main()
59 {
60     constexpr auto path = Calculator::instance_path;
61 
62     sdbusplus::async::context ctx;
63     sdbusplus::server::manager_t manager{ctx, path};
64 
65     Calculator c{ctx, path};
66 
67     ctx.spawn([](sdbusplus::async::context& ctx) -> sdbusplus::async::task<> {
68         ctx.request_name(Calculator::default_service);
69         co_return;
70     }(ctx));
71 
72     ctx.run();
73 
74     return 0;
75 }
76