1 #include <net/poettering/Calculator/aserver.hpp> 2 #include <sdbusplus/async.hpp> 3 4 class Calculator : 5 public sdbusplus::aserver::net::poettering::Calculator<Calculator> 6 { 7 public: 8 explicit Calculator(sdbusplus::async::context& ctx, auto path) : 9 sdbusplus::aserver::net::poettering::Calculator<Calculator>(ctx, path) 10 {} 11 12 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 { 22 using sdbusplus::error::net::poettering::calculator::DivisionByZero; 23 if (y == 0) 24 { 25 status(State::Error); 26 throw DivisionByZero(); 27 } 28 29 auto r = x / y; 30 last_result(r); 31 co_return r; 32 } 33 34 auto method_call(clear_t) -> sdbusplus::async::task<> 35 { 36 auto v = last_result(); 37 last_result(0); 38 cleared(v); 39 co_return; 40 } 41 }; 42 43 int main() 44 { 45 constexpr auto path = Calculator::instance_path; 46 47 sdbusplus::async::context ctx; 48 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.request_name(Calculator::default_service); 54 co_return; 55 }(ctx)); 56 57 ctx.run(); 58 59 return 0; 60 } 61