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