diff options
| author | William A. Kennington III <wak@google.com> | 2018-06-22 19:00:05 -0700 |
|---|---|---|
| committer | Brad Bishop <bradleyb@fuzziesquirrel.com> | 2018-07-09 18:20:42 +0000 |
| commit | 376575018d05edc3d060f84b5909d0b6866e74bf (patch) | |
| tree | 73fc91a94f2300cb7ec37a03caca85a4f2fe0421 | |
| parent | 6e43390193d59723159e78da98443a6a02d1d329 (diff) | |
| download | sdbusplus-376575018d05edc3d060f84b5909d0b6866e74bf.tar.gz sdbusplus-376575018d05edc3d060f84b5909d0b6866e74bf.zip | |
SdBusError: Add Dependency Injection for the SdBusInterface
We want to be able to mock some of the sd_bus_error* calls for testing
the SdBusError class. This work makes that possible in the future while
retaining the same behavior.
Tested:
Errors still work fine and are backward compatible. New test cases
are now able to build against this class.
Change-Id: Ib125dc48eed60ac2bbdfadc4f1a81c52ab047cd4
Signed-off-by: William A. Kennington III <wak@google.com>
| -rw-r--r-- | sdbusplus/exception.cpp | 24 | ||||
| -rw-r--r-- | sdbusplus/exception.hpp | 10 |
2 files changed, 24 insertions, 10 deletions
diff --git a/sdbusplus/exception.cpp b/sdbusplus/exception.cpp index 5e99abb..f160c22 100644 --- a/sdbusplus/exception.cpp +++ b/sdbusplus/exception.cpp @@ -1,15 +1,19 @@ #include <sdbusplus/exception.hpp> +#include <sdbusplus/sdbus.hpp> + +extern sdbusplus::SdBusImpl sdbus_impl; namespace sdbusplus { namespace exception { -SdBusError::SdBusError(int error, const char* prefix) : - std::system_error(error, std::generic_category()), error(SD_BUS_ERROR_NULL) +SdBusError::SdBusError(int error, const char* prefix, SdBusInterface* intf) : + std::system_error(error, std::generic_category()), error(SD_BUS_ERROR_NULL), + intf(intf) { if (error == ENOMEM || - sd_bus_error_set_errno(&this->error, error) == -ENOMEM) + intf->sd_bus_error_set_errno(&this->error, error) == -ENOMEM) { throw std::bad_alloc(); } @@ -17,9 +21,11 @@ SdBusError::SdBusError(int error, const char* prefix) : populateMessage(prefix); } -SdBusError::SdBusError(sd_bus_error error, const char* prefix) : - std::system_error(sd_bus_error_get_errno(&error), std::generic_category()), - error(error) +SdBusError::SdBusError(sd_bus_error error, const char* prefix, + SdBusInterface* intf) : + std::system_error(intf->sd_bus_error_get_errno(&error), + std::generic_category()), + error(error), intf(intf) { populateMessage(prefix); } @@ -40,7 +46,7 @@ SdBusError& SdBusError::operator=(SdBusError&& other) SdBusError::~SdBusError() { - sd_bus_error_free(&error); + intf->sd_bus_error_free(&error); } const char* SdBusError::name() const noexcept @@ -75,7 +81,9 @@ void SdBusError::populateMessage(const char* prefix) void SdBusError::move(SdBusError&& other) { - sd_bus_error_free(&error); + intf = std::move(other.intf); + + intf->sd_bus_error_free(&error); error = other.error; other.error = SD_BUS_ERROR_NULL; diff --git a/sdbusplus/exception.hpp b/sdbusplus/exception.hpp index 06f488e..73b26a0 100644 --- a/sdbusplus/exception.hpp +++ b/sdbusplus/exception.hpp @@ -1,10 +1,13 @@ #pragma once #include <exception> +#include <sdbusplus/sdbus.hpp> #include <string> #include <system_error> #include <systemd/sd-bus.h> +extern sdbusplus::SdBusImpl sdbus_impl; + namespace sdbusplus { @@ -29,9 +32,11 @@ class SdBusError final : public internal_exception, public std::system_error { public: /** Errno must be positive */ - SdBusError(int error, const char* prefix); + SdBusError(int error, const char* prefix, + SdBusInterface* intf = &sdbus_impl); /** Becomes the owner of the error */ - SdBusError(sd_bus_error error, const char* prefix); + SdBusError(sd_bus_error error, const char* prefix, + SdBusInterface* intf = &sdbus_impl); SdBusError(const SdBusError&) = delete; SdBusError& operator=(const SdBusError&) = delete; @@ -46,6 +51,7 @@ class SdBusError final : public internal_exception, public std::system_error private: sd_bus_error error; std::string full_message; + SdBusInterface* intf; /** Populates the full_message from the stored * error and the passed in prefix. */ |

