1
0
Fork 0
mirror of https://bitbucket.org/King_DuckZ/keepupnpup.git synced 2024-11-07 21:29:00 +00:00

Put exceptions into separate files.

This commit is contained in:
King_DuckZ 2016-08-26 02:25:05 +02:00
parent 308e364201
commit 1d35e84cb4
5 changed files with 95 additions and 49 deletions

View file

@ -24,6 +24,7 @@ endif()
add_executable(${PROJECT_NAME}
src/main.cpp
src/upnp.cpp
src/upnpexceptions.cpp
)
target_include_directories(${PROJECT_NAME}

View file

@ -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

View file

@ -18,9 +18,9 @@
#pragma once
#include "redirection.hpp"
#include "upnpexceptions.hpp"
#include <memory>
#include <string>
#include <stdexcept>
#include <vector>
#include <cstdint>
@ -57,27 +57,4 @@ namespace kuu {
std::unique_ptr<LocalData> 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

47
src/upnpexceptions.cpp Normal file
View file

@ -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 <http://www.gnu.org/licenses/>.
*/
#include "upnpexceptions.hpp"
#include <sstream>
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

46
src/upnpexceptions.hpp Normal file
View file

@ -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 <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <stdexcept>
#include <string>
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