Patrick Williams | bf0283a | 2023-08-19 07:18:04 -0500 | [diff] [blame] | 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: |
Patrick Williams | 3a363a6 | 2023-08-22 17:08:15 -0500 | [diff] [blame] | 8 | explicit Calculator(sdbusplus::async::context& ctx, auto path) : |
| 9 | sdbusplus::aserver::net::poettering::Calculator<Calculator>(ctx, path) |
| 10 | {} |
Patrick Williams | 72f7116 | 2023-08-20 21:36:14 -0500 | [diff] [blame] | 11 | |
Patrick Williams | e15ff88 | 2023-08-22 16:59:16 -0500 | [diff] [blame] | 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 | { |
Patrick Williams | 43fcd1c | 2023-08-25 08:51:42 -0500 | [diff] [blame] | 22 | using sdbusplus::error::net::poettering::calculator::DivisionByZero; |
Patrick Williams | 3a363a6 | 2023-08-22 17:08:15 -0500 | [diff] [blame] | 23 | if (y == 0) |
| 24 | { |
| 25 | status(State::Error); |
| 26 | throw DivisionByZero(); |
| 27 | } |
| 28 | |
Patrick Williams | e15ff88 | 2023-08-22 16:59:16 -0500 | [diff] [blame] | 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 | { |
Patrick Williams | 3a363a6 | 2023-08-22 17:08:15 -0500 | [diff] [blame] | 36 | auto v = last_result(); |
Patrick Williams | e15ff88 | 2023-08-22 16:59:16 -0500 | [diff] [blame] | 37 | last_result(0); |
Patrick Williams | 3a363a6 | 2023-08-22 17:08:15 -0500 | [diff] [blame] | 38 | cleared(v); |
Patrick Williams | e15ff88 | 2023-08-22 16:59:16 -0500 | [diff] [blame] | 39 | co_return; |
| 40 | } |
Patrick Williams | bf0283a | 2023-08-19 07:18:04 -0500 | [diff] [blame] | 41 | }; |
| 42 | |
| 43 | int main() |
| 44 | { |
Patrick Williams | b4bae8f | 2023-09-01 10:13:19 -0500 | [diff] [blame] | 45 | constexpr auto path = Calculator::instance_path; |
Patrick Williams | 3a363a6 | 2023-08-22 17:08:15 -0500 | [diff] [blame] | 46 | |
Patrick Williams | bf0283a | 2023-08-19 07:18:04 -0500 | [diff] [blame] | 47 | sdbusplus::async::context ctx; |
Patrick Williams | 3a363a6 | 2023-08-22 17:08:15 -0500 | [diff] [blame] | 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<> { |
Patrick Williams | b4bae8f | 2023-09-01 10:13:19 -0500 | [diff] [blame] | 53 | ctx.get_bus().request_name(Calculator::default_service); |
Patrick Williams | 3a363a6 | 2023-08-22 17:08:15 -0500 | [diff] [blame] | 54 | co_return; |
| 55 | }(ctx)); |
Patrick Williams | bf0283a | 2023-08-19 07:18:04 -0500 | [diff] [blame] | 56 | |
| 57 | ctx.run(); |
| 58 | |
| 59 | return 0; |
| 60 | } |