Debian Codegraph @

qt/position/instance.h

raw
1 /*
2 * Copyright © 2013 Canonical Ltd.
3 * Copyright 2022 UBports Foundation.
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License version 3 as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 *
17 * Authored by: Thomas Voß <[email protected]>
18 * Marius Gripsgard <[email protected]>
19 */
20
21 #ifndef INSTANCE_H_
22 #define INSTANCE_H_
23
24 #include <com/lomiri/location/service/stub.h>
25
26 #include <core/dbus/resolver.h>
27 #include <core/dbus/asio/executor.h>
28
29 #include <thread>
30
31 class Instance
32 {
33 public:
34 Instance()
35 : bus(std::make_shared<core::dbus::Bus>(core::dbus::WellKnownBus::system)),
36 executor(core::dbus::asio::make_executor(bus)),
37 service(core::dbus::resolve_service_on_bus<
38 com::lomiri::location::service::Interface,
39 com::lomiri::location::service::Stub
40 >(bus))
41 {
42 bus->install_executor(executor);
43 worker = std::move(std::thread([&]() { bus->run(); }));
44 }
45
46 ~Instance() noexcept
47 {
48 try
49 {
50 bus->stop();
51
52 if (worker.joinable())
53 worker.join();
54 } catch(...)
55 {
56 // We silently ignore errors to fulfill our noexcept guarantee.
57 }
58 }
59
60 const com::lomiri::location::service::Interface::Ptr& getService() const
61 {
62 return service;
63 }
64
65 private:
66
67 core::dbus::Bus::Ptr bus;
68 core::dbus::Executor::Ptr executor;
69 std::thread worker;
70
71 com::lomiri::location::service::Interface::Ptr service;
72 };
73
74 #endif // INSTANCE_H_