summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPatrick Williams <patrick@stwcx.xyz>2016-09-02 15:14:16 -0500
committerPatrick Williams <patrick@stwcx.xyz>2016-09-12 21:54:52 -0500
commit5acb5492ea449c147b49d5c3113473175f1454c2 (patch)
tree2dca040761be26581f6a728f7f26b1a1a419a4d9
parent5b4857945e0107eb7b0a7fa1561f1d38f5a80c2a (diff)
downloadsdbusplus-5acb5492ea449c147b49d5c3113473175f1454c2.tar.gz
sdbusplus-5acb5492ea449c147b49d5c3113473175f1454c2.zip
Add message bindings
Add C++ bindings for the sd_bus_message_* class functions. This binding creates a unique_ptr wrapper around sd_bus_message*, using the appropriate sd_bus_message deref calls when the unique_ptr leaves scope. Change-Id: Ieb8b981a43406332ed3b65bc5326a0f18671d496 Signed-off-by: Patrick Williams <patrick@stwcx.xyz>
-rw-r--r--sdbusplus/message.hpp85
1 files changed, 85 insertions, 0 deletions
diff --git a/sdbusplus/message.hpp b/sdbusplus/message.hpp
new file mode 100644
index 0000000..acaa04d
--- /dev/null
+++ b/sdbusplus/message.hpp
@@ -0,0 +1,85 @@
+#pragma once
+
+#include <memory>
+#include <systemd/sd-bus.h>
+#include <sdbusplus/message/append.hpp>
+
+namespace sdbusplus
+{
+
+ // Forward declare sdbusplus::bus::bus for 'friend'ship.
+namespace bus { struct bus; };
+
+namespace message
+{
+
+using msgp_t = sd_bus_message*;
+class message;
+
+namespace details
+{
+
+/** @brief unique_ptr functor to release a msg reference. */
+struct MsgDeleter
+{
+ void operator()(msgp_t ptr) const
+ {
+ sd_bus_message_unref(ptr);
+ }
+};
+
+/* @brief Alias 'msg' to a unique_ptr type for auto-release. */
+using msg = std::unique_ptr<sd_bus_message, MsgDeleter>;
+
+} // namespace details
+
+/** @class message
+ * @brief Provides C++ bindings to the sd_bus_message_* class functions.
+ */
+struct message
+{
+ /* Define all of the basic class operations:
+ * Not allowed:
+ * - Default constructor to avoid nullptrs.
+ * - Copy operations due to internal unique_ptr.
+ * Allowed:
+ * - Move operations.
+ * - Destructor.
+ */
+ message() = delete;
+ message(const message&) = delete;
+ message& operator=(const message&) = delete;
+ message(message&&) = default;
+ message& operator=(message&&) = default;
+ ~message() = default;
+
+ /** @brief Conversion constructor for 'msgp_t'.
+ *
+ * Takes ownership of the msg-pointer and releases it when done.
+ */
+ explicit message(msgp_t m) : _msg(m) {}
+
+ /** @brief Release ownership of the stored msg-pointer. */
+ msgp_t release() { return _msg.release(); }
+
+ /** @brief Perform sd_bus_message_append, with automatic type deduction.
+ *
+ * @tparam ...Args - Type of items to append to message.
+ * @param[in] args - Items to append to message.
+ */
+ template <typename ...Args> void append(Args&&... args)
+ {
+ sdbusplus::message::append(_msg.get(), std::forward<Args>(args)...);
+ }
+
+ friend struct sdbusplus::bus::bus;
+
+ private:
+ /** @brief Get a pointer to the owned 'msgp_t'. */
+ msgp_t get() { return _msg.get(); }
+ details::msg _msg;
+};
+
+} // namespace message
+
+} // namespace sdbusplus
OpenPOWER on IntegriCloud