properly handle boost::bad_lexical_cast exceptions

This commit is contained in:
Andrzej Rybczak
2013-09-14 17:26:45 +02:00
parent 7167d036d0
commit 88a3bdb507
8 changed files with 183 additions and 83 deletions

117
src/utility/conversion.h Normal file
View File

@@ -0,0 +1,117 @@
/***************************************************************************
* Copyright (C) 2008-2013 by Andrzej Rybczak *
* electricityispower@gmail.com *
* *
* This program 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 2 of the License, or *
* (at your option) any later version. *
* *
* This program 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 this program; if not, write to the *
* Free Software Foundation, Inc., *
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
***************************************************************************/
#include <boost/format.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/type_traits/is_unsigned.hpp>
#include "gcc.h"
struct ConversionError
{
ConversionError(std::string source) : m_source_value(source) { }
const std::string &value() { return m_source_value; }
private:
std::string m_target_type;
std::string m_source_value;
};
struct OutOfBounds
{
const std::string &errorMessage() { return m_error_message; }
template <typename Type>
GNUC_NORETURN static void raise(const Type &value, const Type &lbound, const Type &ubound)
{
throw OutOfBounds((boost::format(
"Value is out of bounds ([%1%, %2%] expected, %3% given)") % lbound % ubound % value).str());
}
template <typename Type>
GNUC_NORETURN static void raiseLower(const Type &value, const Type &lbound)
{
throw OutOfBounds((boost::format(
"Value is out of bounds ([%1%, ->) expected, %2% given)") % lbound % value).str());
}
template <typename Type>
GNUC_NORETURN static void raiseUpper(const Type &value, const Type &ubound)
{
throw OutOfBounds((boost::format(
"Value is out of bounds ((<-, %1%] expected, %2% given)") % ubound % value).str());
}
private:
OutOfBounds(std::string msg) : m_error_message(msg) { }
std::string m_error_message;
};
template <typename TargetT, bool isUnsigned>
struct unsigned_checker
{
static void apply(const std::string &) { }
};
template <typename TargetT>
struct unsigned_checker<TargetT, true>
{
static void apply(const std::string &s)
{
if (s[0] == '-')
throw ConversionError(s);
}
};
template <typename TargetT>
TargetT fromString(const std::string &source)
{
unsigned_checker<TargetT, boost::is_unsigned<TargetT>::value>::apply(source);
try
{
return boost::lexical_cast<TargetT>(source);
}
catch (boost::bad_lexical_cast &)
{
throw ConversionError(source);
}
}
template <typename Type>
void boundsCheck(const Type &value, const Type &lbound, const Type &ubound)
{
if (value < lbound || value > ubound)
OutOfBounds::raise(value, lbound, ubound);
}
template <typename Type>
void lowerBoundCheck(const Type &value, const Type &lbound)
{
if (value < lbound)
OutOfBounds::raiseLower(value, lbound);
}
template <typename Type>
void upperBoundCheck(const Type &value, const Type &ubound)
{
if (value > ubound)
OutOfBounds::raiseUpper(value, ubound);
}

View File

@@ -23,17 +23,6 @@
#include <algorithm>
#include "utility/string.h"
bool isInteger(const char *s, bool accept_signed)
{
assert(s);
if (*s == '\0')
return false;
for (const char *it = s; *it != '\0'; ++it)
if (!(isdigit(*it) || (accept_signed && it == s && *it == '-')))
return false;
return true;
}
std::string getBasename(const std::string &path)
{
size_t slash = path.rfind("/");

View File

@@ -31,8 +31,6 @@ template <size_t N> size_t const_strlen(const char (&)[N]) {
return N-1;
}
bool isInteger(const char *s, bool accept_signed);
std::string getBasename(const std::string &path);
std::string getParentDirectory(const std::string &path);
std::string getSharedDirectory(const std::string &dir1, const std::string &dir2);