Enable proper full C++ exception handling on MSVC
[openal-soft.git] / core / dbus_wrap.h
blobafc27d8429f84ae8a48e7ceefc12c407c8f2b21a
1 #ifndef CORE_DBUS_WRAP_H
2 #define CORE_DBUS_WRAP_H
4 #include <memory>
6 #include <dbus/dbus.h>
8 #include "dynload.h"
10 #ifdef HAVE_DYNLOAD
12 #include <mutex>
14 #define DBUS_FUNCTIONS(MAGIC) \
15 MAGIC(dbus_error_init) \
16 MAGIC(dbus_error_free) \
17 MAGIC(dbus_bus_get) \
18 MAGIC(dbus_connection_set_exit_on_disconnect) \
19 MAGIC(dbus_connection_unref) \
20 MAGIC(dbus_connection_send_with_reply_and_block) \
21 MAGIC(dbus_message_unref) \
22 MAGIC(dbus_message_new_method_call) \
23 MAGIC(dbus_message_append_args) \
24 MAGIC(dbus_message_iter_init) \
25 MAGIC(dbus_message_iter_next) \
26 MAGIC(dbus_message_iter_recurse) \
27 MAGIC(dbus_message_iter_get_arg_type) \
28 MAGIC(dbus_message_iter_get_basic) \
29 MAGIC(dbus_set_error_from_message)
31 inline void *dbus_handle{};
32 #define DECL_FUNC(x) inline decltype(x) *p##x{};
33 DBUS_FUNCTIONS(DECL_FUNC)
34 #undef DECL_FUNC
36 #ifndef IN_IDE_PARSER
37 #define dbus_error_init (*pdbus_error_init)
38 #define dbus_error_free (*pdbus_error_free)
39 #define dbus_bus_get (*pdbus_bus_get)
40 #define dbus_connection_set_exit_on_disconnect (*pdbus_connection_set_exit_on_disconnect)
41 #define dbus_connection_unref (*pdbus_connection_unref)
42 #define dbus_connection_send_with_reply_and_block (*pdbus_connection_send_with_reply_and_block)
43 #define dbus_message_unref (*pdbus_message_unref)
44 #define dbus_message_new_method_call (*pdbus_message_new_method_call)
45 #define dbus_message_append_args (*pdbus_message_append_args)
46 #define dbus_message_iter_init (*pdbus_message_iter_init)
47 #define dbus_message_iter_next (*pdbus_message_iter_next)
48 #define dbus_message_iter_recurse (*pdbus_message_iter_recurse)
49 #define dbus_message_iter_get_arg_type (*pdbus_message_iter_get_arg_type)
50 #define dbus_message_iter_get_basic (*pdbus_message_iter_get_basic)
51 #define dbus_set_error_from_message (*pdbus_set_error_from_message)
52 #endif
54 void PrepareDBus();
56 inline auto HasDBus()
58 static std::once_flag init_dbus{};
59 std::call_once(init_dbus, []{ PrepareDBus(); });
60 return dbus_handle;
63 #else
65 constexpr bool HasDBus() noexcept { return true; }
66 #endif /* HAVE_DYNLOAD */
69 namespace dbus {
71 struct Error {
72 Error() { dbus_error_init(&mError); }
73 Error(const Error&) = delete;
74 Error(Error&&) = delete;
75 ~Error() { dbus_error_free(&mError); }
77 void operator=(const Error&) = delete;
78 void operator=(Error&&) = delete;
80 DBusError* operator->() { return &mError; }
81 DBusError &get() { return mError; }
83 private:
84 DBusError mError{};
87 struct ConnectionDeleter {
88 void operator()(DBusConnection *c) { dbus_connection_unref(c); }
90 using ConnectionPtr = std::unique_ptr<DBusConnection,ConnectionDeleter>;
92 } // namespace dbus
94 #endif /* CORE_DBUS_WRAP_H */