exception: add string r-value constructor
If we are building a string as part of throwing this exception type,
it is more efficient to move the string rather than getting the
`c_str()` and doing another allocation inside the class.
Signed-off-by: Patrick Williams <patrick@stwcx.xyz>
Change-Id: Ibec1c550f82208d5dde6dfef1626f907b7d2ad50
diff --git a/include/sdbusplus/exception.hpp b/include/sdbusplus/exception.hpp
index b7d7d50..e7e31ea 100644
--- a/include/sdbusplus/exception.hpp
+++ b/include/sdbusplus/exception.hpp
@@ -52,6 +52,8 @@
/** Errno must be positive */
SdBusError(int error, const char* prefix,
SdBusInterface* intf = &sdbus_impl);
+ SdBusError(int error, std::string&& prefix,
+ SdBusInterface* intf = &sdbus_impl);
/** Becomes the owner of the error */
SdBusError(sd_bus_error* error, const char* prefix,
SdBusInterface* intf = &sdbus_impl);
@@ -75,7 +77,7 @@
/** Populates the full_message from the stored
* error and the passed in prefix. */
- void populateMessage(const char* prefix);
+ void populateMessage(std::string&& prefix);
/** Helper to reduce duplicate move logic */
void move(SdBusError&& other);
diff --git a/src/exception.cpp b/src/exception.cpp
index a7a3596..1bfd890 100644
--- a/src/exception.cpp
+++ b/src/exception.cpp
@@ -23,6 +23,11 @@
SdBusError::SdBusError(int error_in, const char* prefix,
SdBusInterface* intf_in) :
+ SdBusError(error_in, std::string(prefix), intf_in)
+{}
+
+SdBusError::SdBusError(int error_in, std::string&& prefix,
+ SdBusInterface* intf_in) :
error(SD_BUS_ERROR_NULL),
intf(intf_in)
{
@@ -36,7 +41,7 @@
throw std::runtime_error("Failed to create SdBusError");
}
- populateMessage(prefix);
+ populateMessage(std::move(prefix));
}
SdBusError::SdBusError(sd_bus_error* error_in, const char* prefix,
@@ -47,7 +52,7 @@
// We own the error so remove the caller's reference
*error_in = SD_BUS_ERROR_NULL;
- populateMessage(prefix);
+ populateMessage(std::string(prefix));
}
SdBusError::SdBusError(SdBusError&& other) : error(SD_BUS_ERROR_NULL)
@@ -94,9 +99,9 @@
return &error;
}
-void SdBusError::populateMessage(const char* prefix)
+void SdBusError::populateMessage(std::string&& prefix)
{
- full_message = prefix;
+ full_message = std::move(prefix);
if (error.name)
{
full_message += ": ";