diff --git a/backend/genesys_error.cc b/backend/genesys_error.cc index c98a49003..afe80c150 100644 --- a/backend/genesys_error.cc +++ b/backend/genesys_error.cc @@ -69,6 +69,48 @@ static unsigned num_uncaught_exceptions() #endif } +SaneException::SaneException(SANE_Status status) : status_(status) +{ + set_msg(nullptr); +} + +SaneException::SaneException(SANE_Status status, const char* msg) : status_(status) +{ + set_msg(msg); +} + +SaneException::SaneException(const char* msg) : SaneException(SANE_STATUS_INVAL, msg) +{ +} + +SANE_Status SaneException::status() const +{ + return status_; +} + +const char* SaneException::what() const noexcept +{ + return msg_.c_str(); +} + +void SaneException::set_msg(const char* msg) +{ + const char* status_msg = sane_strstatus(status_); + std::size_t status_msg_len = std::strlen(status_msg); + + if (msg) { + std::size_t msg_len = std::strlen(msg); + msg_.reserve(msg_len + status_msg_len + 3); + msg_ = msg; + msg_ += " : "; + msg_ += status_msg; + return; + } + + msg_.reserve(status_msg_len); + msg_ = status_msg; +} + DebugMessageHelper::DebugMessageHelper(const char* func) { func_ = func; diff --git a/backend/genesys_error.h b/backend/genesys_error.h index 1ab5957e3..fd438c692 100644 --- a/backend/genesys_error.h +++ b/backend/genesys_error.h @@ -64,40 +64,18 @@ class SaneException : std::exception { public: - SaneException(SANE_Status status) : status_(status) - { - set_msg(nullptr); - } + SaneException(SANE_Status status); + SaneException(SANE_Status status, const char* msg); - SaneException(SANE_Status status, const char* msg) : status_(status) - { - set_msg(msg); - } + SaneException(const char* msg); - SaneException(const char* msg) : SaneException(SANE_STATUS_INVAL, msg) {} - SANE_Status status() const { return status_; } - virtual const char* what() const noexcept override { return msg_.c_str(); } + SANE_Status status() const; + const char* what() const noexcept override; private: - void set_msg(const char* msg) - { - const char* status_msg = sane_strstatus(status_); - std::size_t status_msg_len = std::strlen(status_msg); - - if (msg) { - std::size_t msg_len = std::strlen(msg); - msg_.reserve(msg_len + status_msg_len + 3); - msg_ = msg; - msg_ += " : "; - msg_ += status_msg; - return; - } - - msg_.reserve(status_msg_len); - msg_ = status_msg; - } + void set_msg(const char* msg); std::string msg_; SANE_Status status_;