From 1d35e84cb42a977ff544a06739bddb52068aa621 Mon Sep 17 00:00:00 2001 From: King_DuckZ Date: Fri, 26 Aug 2016 02:25:05 +0200 Subject: [PATCH] Put exceptions into separate files. --- CMakeLists.txt | 1 + src/upnp.cpp | 25 ---------------------- src/upnp.hpp | 25 +--------------------- src/upnpexceptions.cpp | 47 ++++++++++++++++++++++++++++++++++++++++++ src/upnpexceptions.hpp | 46 +++++++++++++++++++++++++++++++++++++++++ 5 files changed, 95 insertions(+), 49 deletions(-) create mode 100644 src/upnpexceptions.cpp create mode 100644 src/upnpexceptions.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index d5639f6..e93a91c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -24,6 +24,7 @@ endif() add_executable(${PROJECT_NAME} src/main.cpp src/upnp.cpp + src/upnpexceptions.cpp ) target_include_directories(${PROJECT_NAME} diff --git a/src/upnp.cpp b/src/upnp.cpp index 8d6b9eb..435bb14 100644 --- a/src/upnp.cpp +++ b/src/upnp.cpp @@ -66,14 +66,6 @@ namespace kuu { return retval; } - std::string build_exception_msg (int parError, const char* parMessageIntro, const char* parMessage) { - std::ostringstream oss; - oss << parMessageIntro << " (" << parError << ")"; - if (parMessage) - oss << ": \"" << parMessage << '"'; - return oss.str(); - } - bool is_enabled (const std::string& parEnabled) { if (parEnabled.size() == 4) { if (strncasecmp("true", parEnabled.c_str(), 4) == 0) @@ -310,21 +302,4 @@ namespace kuu { throw UPNPException(r, oss.str().c_str(), strupnperror(r)); } } - - - UPNPException::UPNPException (int parError, const char* parMessageIntro, const char* parMessage) : - Exception(build_exception_msg(parError, parMessageIntro, parMessage)), - m_error(parError) - { - } - - ScanException::ScanException (const std::string& parMessage) : - Exception(parMessage) - { - } - - ScanException::ScanException (const char* parMessage) : - Exception(parMessage) - { - } } //namespace kuu diff --git a/src/upnp.hpp b/src/upnp.hpp index 447253e..0f1a5f3 100644 --- a/src/upnp.hpp +++ b/src/upnp.hpp @@ -18,9 +18,9 @@ #pragma once #include "redirection.hpp" +#include "upnpexceptions.hpp" #include #include -#include #include #include @@ -57,27 +57,4 @@ namespace kuu { std::unique_ptr m_local_data; }; - - class Exception : public std::runtime_error { - protected: - explicit Exception (const char* parMessage) : std::runtime_error(parMessage) {} - explicit Exception (const std::string& parMessage) : std::runtime_error(parMessage) {} - }; - - class UPNPException : public Exception { - public: - UPNPException (int parError, const char* parMessageIntro, const char* parMessage); - virtual ~UPNPException() noexcept = default; - - int error_code() const { return m_error; } - - private: - int m_error; - }; - - class ScanException : public Exception { - public: - explicit ScanException (const char* parMessage); - explicit ScanException (const std::string& parMessage); - }; } //namespace kuu diff --git a/src/upnpexceptions.cpp b/src/upnpexceptions.cpp new file mode 100644 index 0000000..1927f7f --- /dev/null +++ b/src/upnpexceptions.cpp @@ -0,0 +1,47 @@ +/* Copyright 2016, Michele Santullo + * This file is part of "keepupnpup". + * + * "keepupnpup" is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * "keepupnpup" is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with "keepupnpup". If not, see . + */ + +#include "upnpexceptions.hpp" +#include + +namespace kuu { + namespace { + std::string build_exception_msg (int parError, const char* parMessageIntro, const char* parMessage) { + std::ostringstream oss; + oss << parMessageIntro << " (" << parError << ")"; + if (parMessage) + oss << ": \"" << parMessage << '"'; + return oss.str(); + } + } //unnamed namespace + + UPNPException::UPNPException (int parError, const char* parMessageIntro, const char* parMessage) : + Exception(build_exception_msg(parError, parMessageIntro, parMessage)), + m_error(parError) + { + } + + ScanException::ScanException (const std::string& parMessage) : + Exception(parMessage) + { + } + + ScanException::ScanException (const char* parMessage) : + Exception(parMessage) + { + } +} //namespace kuu diff --git a/src/upnpexceptions.hpp b/src/upnpexceptions.hpp new file mode 100644 index 0000000..94908ef --- /dev/null +++ b/src/upnpexceptions.hpp @@ -0,0 +1,46 @@ +/* Copyright 2016, Michele Santullo + * This file is part of "keepupnpup". + * + * "keepupnpup" is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * "keepupnpup" is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with "keepupnpup". If not, see . + */ + +#pragma once + +#include +#include + +namespace kuu { + class Exception : public std::runtime_error { + protected: + explicit Exception (const char* parMessage) : std::runtime_error(parMessage) {} + explicit Exception (const std::string& parMessage) : std::runtime_error(parMessage) {} + }; + + class UPNPException : public Exception { + public: + UPNPException (int parError, const char* parMessageIntro, const char* parMessage); + virtual ~UPNPException() noexcept = default; + + int error_code() const { return m_error; } + + private: + int m_error; + }; + + class ScanException : public Exception { + public: + explicit ScanException (const char* parMessage); + explicit ScanException (const std::string& parMessage); + }; +} //namespace kuu