Allow copy constructor/assignment operator for message::message
This change enables copies and assignments of messages, specifically,
copy constructors and copy assignment operators and the default empty
constructor. A boost::asio::yield_context needs to be able to store the
result of the async operation (a message::message) in a helper struct,
which results in a default construction (with a null msg_t) and later an
assignment to replace it when the result is ready. But adding the
appropriate sd_bus_message_ref/unref calls makes this possible.
While the default-constructed object does not sit around for long, this
change also adds in some asserts to catch any invalid accesses of the
object in that state.
Change-Id: I2059cb677fc5a30a3c7e3c2b81ed4199e5460dde
Signed-off-by: Vernon Mauery <vernon.mauery@linux.intel.com>
diff --git a/sdbusplus/message.hpp b/sdbusplus/message.hpp
index ac468dc..a989a48 100644
--- a/sdbusplus/message.hpp
+++ b/sdbusplus/message.hpp
@@ -49,17 +49,13 @@
class message
{
/* Define all of the basic class operations:
- * Not allowed:
- * - Default constructor to avoid nullptrs.
- * - Copy operations due to internal unique_ptr.
* Allowed:
+ * - Default constructor
+ * - Copy operations.
* - Move operations.
* - Destructor.
*/
public:
- message() = delete;
- message(const message&) = delete;
- message& operator=(const message&) = delete;
message(message&&) = default;
message& operator=(message&&) = default;
~message() = default;
@@ -74,7 +70,7 @@
* Takes increment ref-count of the msg-pointer and release when
* destructed.
*/
- explicit message(msgp_t m) : message(m, &sdbus_impl)
+ explicit message(msgp_t m = nullptr) : message(m, &sdbus_impl)
{
}
@@ -91,6 +87,26 @@
{
}
+ /** @brief Copy constructor for 'message'.
+ *
+ * Copies the message class and increments the ref on _msg
+ */
+ message(const message& other) :
+ _intf(other._intf), _msg(sd_bus_message_ref(other._msg.get()))
+ {
+ }
+
+ /** @brief Assignment operator for 'message'.
+ *
+ * Copies the message class and increments the ref on _msg
+ */
+ message& operator=(const message& other)
+ {
+ _msg.reset(sd_bus_message_ref(other._msg.get()));
+ _intf = other._intf;
+ return *this;
+ }
+
/** @brief Release ownership of the stored msg-pointer. */
msgp_t release()
{