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