FlightGear next
dbusdispatcher.h
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: (C) 2019-2022 swift Project Community / Contributors (https://swift-project.org/)
3 * SPDX-FileCopyrightText: (C) 2019-2022 Lars Toenning <dev@ltoenning.de>
4 * SPDX-License-Identifier: GPL-2.0-or-later
5 */
6
7#pragma once
8
9#include "dbuscallbacks.h"
10
11#include <dbus/dbus.h>
12#include <event2/event.h>
13
14#include <memory>
15#include <unordered_map>
16#include <vector>
17
18namespace flightgear::swift {
19
20class WatchHandler;
21class TimeoutHandler;
22class CDBusConnection;
23class CDBusDispatcher;
24
27{
28public:
30 IDispatchable() = default;
31
33 virtual ~IDispatchable() = default;
34
36 virtual void dispatch() = 0;
37
38private:
39 friend CDBusDispatcher;
40};
41
44{
45public:
48
50 virtual ~CDBusDispatcher();
51
53 void add(IDispatchable* dispatchable);
54
56 void remove(IDispatchable* dispatchable);
57
59 void waitAndRun();
60
62 void runOnce();
63
64private:
65 friend class WatchHandler;
66 friend class TimeoutHandler;
67 friend class Timer;
68 friend class CDBusConnection;
69 friend class CDBusServer;
70
71 struct EventBaseDeleter {
72 void operator()(event_base* obj) const { event_base_free(obj); }
73 };
74
75 using WatchCallbacks = DBusAsyncCallbacks<DBusWatch>;
76 using TimeoutCallbacks = DBusAsyncCallbacks<DBusTimeout>;
77
78 void dispatch();
79
80 dbus_bool_t dbusAddWatch(DBusWatch* watch);
81 void dbusRemoveWatch(DBusWatch* watch);
82 void dbusWatchToggled(DBusWatch* watch);
83
84 dbus_bool_t dbusAddTimeout(DBusTimeout* timeout);
85 void dbusRemoveTimeout(DBusTimeout* timeout);
86 void dbusTimeoutToggled(DBusTimeout* timeout);
87
88 WatchCallbacks m_watchCallbacks;
89 TimeoutCallbacks m_timeoutCallbacks;
90 std::unordered_multimap<evutil_socket_t, std::unique_ptr<WatchHandler>> m_watchers;
91 std::vector<std::unique_ptr<TimeoutHandler>> m_timeouts;
92 std::unique_ptr<event_base, EventBaseDeleter> m_eventBase;
93
94 std::vector<IDispatchable*> m_dispatchList;
95};
96} // namespace flightgear::swift
void remove(IDispatchable *dispatchable)
Remove dispatchable object.
void waitAndRun()
Waits for events to be dispatched and handles them.
void add(IDispatchable *dispatchable)
Add dispatchable object.
void runOnce()
Dispatches ready handlers and returns without waiting.
Dispatchable Interface.
virtual ~IDispatchable()=default
Default destructor.
IDispatchable()=default
Default constructor.
virtual void dispatch()=0
Dispatch execution method.