1 #include <net/poettering/Calculator/client.hpp>
2 #include <net/poettering/Calculator/error.hpp>
3 #include <net/poettering/Calculator/server.hpp>
4 #include <sdbusplus/server.hpp>
5 
6 #include <iostream>
7 #include <string_view>
8 
9 using Calculator_inherit =
10     sdbusplus::server::object_t<sdbusplus::server::net::poettering::Calculator>;
11 
12 /** Example implementation of net.poettering.Calculator */
13 struct Calculator : Calculator_inherit
14 {
15     /** Constructor */
16     Calculator(sdbusplus::bus_t& bus, const char* path) :
17         Calculator_inherit(bus, path)
18     {}
19 
20     /** Multiply (x*y), update lastResult */
21     int64_t multiply(int64_t x, int64_t y) override
22     {
23         return lastResult(x * y);
24     }
25 
26     /** Divide (x/y), update lastResult
27      *
28      *  Throws DivisionByZero on error.
29      */
30     int64_t divide(int64_t x, int64_t y) override
31     {
32         using sdbusplus::error::net::poettering::calculator::DivisionByZero;
33         if (y == 0)
34         {
35             status(State::Error);
36             throw DivisionByZero();
37         }
38 
39         return lastResult(x / y);
40     }
41 
42     /** Clear lastResult, broadcast 'Cleared' signal */
43     void clear() override
44     {
45         auto v = lastResult();
46         lastResult(0);
47         cleared(v);
48         return;
49     }
50 };
51 
52 int main()
53 {
54     // Create a new bus and affix an object manager for the subtree path we
55     // intend to place objects at..
56     auto b = sdbusplus::bus::new_default();
57     sdbusplus::server::manager_t m{b, Calculator::instance_path};
58 
59     // Reserve the dbus service name : net.poettering.Calculator
60     b.request_name(Calculator::default_service);
61 
62     // Create a calculator object at /net/poettering/calculator
63     Calculator c1{b, Calculator::instance_path};
64 
65     // Handle dbus processing forever.
66     b.process_loop();
67 }
68