Genericize exception classes

This commit is contained in:
Krystian Stasiowski
2020-03-09 22:55:38 -04:00
parent e7e54b2665
commit 7b27ebea33
2 changed files with 38 additions and 92 deletions

View File

@ -125,9 +125,6 @@
// Boost and non-Boost versions of utilities // Boost and non-Boost versions of utilities
#ifndef BOOST_STATIC_STRING_STANDALONE #ifndef BOOST_STATIC_STRING_STANDALONE
#ifndef BOOST_STATIC_STRING_THROW_IF
#define BOOST_STATIC_STRING_THROW_IF(cond, ex) if (cond) BOOST_STATIC_STRING_UNLIKELY BOOST_THROW_EXCEPTION(ex)
#endif
#ifndef BOOST_STATIC_STRING_THROW #ifndef BOOST_STATIC_STRING_THROW
#define BOOST_STATIC_STRING_THROW(ex) BOOST_THROW_EXCEPTION(ex) #define BOOST_STATIC_STRING_THROW(ex) BOOST_THROW_EXCEPTION(ex)
#endif #endif
@ -138,9 +135,6 @@
#define BOOST_STATIC_STRING_ASSERT(cond) BOOST_ASSERT(cond) #define BOOST_STATIC_STRING_ASSERT(cond) BOOST_ASSERT(cond)
#endif #endif
#else #else
#ifndef BOOST_STATIC_STRING_THROW_IF
#define BOOST_STATIC_STRING_THROW_IF(cond, ex) if (cond) BOOST_STATIC_STRING_UNLIKELY throw ex
#endif
#ifndef BOOST_STATIC_STRING_THROW #ifndef BOOST_STATIC_STRING_THROW
#define BOOST_STATIC_STRING_THROW(ex) throw ex #define BOOST_STATIC_STRING_THROW(ex) throw ex
#endif #endif

View File

@ -730,13 +730,12 @@ defined(BOOST_STATIC_STRING_NO_PTR_COMP_FUNCTIONS)
#endif #endif
} }
//-------------------------------------------------------------------------- // This nested type workaround is for gcc 5,
// // which prohibits throw expressions in constexpr
// exception classes // functions, but for some reason permits them in
// // constructors.
//-------------------------------------------------------------------------- template<typename Exception>
struct exception
struct length_error
{ {
#ifdef BOOST_STATIC_STRING_GCC5_BAD_CONSTEXPR #ifdef BOOST_STATIC_STRING_GCC5_BAD_CONSTEXPR
struct raise_if struct raise_if
@ -746,8 +745,8 @@ struct length_error
bool check, bool check,
const char* message) const char* message)
{ {
BOOST_STATIC_STRING_THROW_IF( if (check)
check, std::length_error(message)); BOOST_STATIC_STRING_THROW(Exception(message));
} }
}; };
@ -757,7 +756,7 @@ struct length_error
BOOST_STATIC_STRING_CPP14_CONSTEXPR BOOST_STATIC_STRING_CPP14_CONSTEXPR
raise(const char* message) raise(const char* message)
{ {
BOOST_STATIC_STRING_THROW(std::length_error(message)); BOOST_STATIC_STRING_THROW(Exception(message));
} }
}; };
#else #else
@ -768,8 +767,8 @@ struct length_error
bool check, bool check,
const char* message) const char* message)
{ {
BOOST_STATIC_STRING_THROW_IF( if (check)
check, std::length_error(message)); BOOST_STATIC_STRING_THROW(Exception(message));
} }
BOOST_STATIC_STRING_NORETURN BOOST_STATIC_STRING_NORETURN
@ -777,57 +776,10 @@ struct length_error
void void
raise(const char* message) raise(const char* message)
{ {
BOOST_STATIC_STRING_THROW(std::length_error(message)); BOOST_STATIC_STRING_THROW(Exception(message));
} }
#endif #endif
}; };
struct out_of_range
{
#ifdef BOOST_STATIC_STRING_GCC5_BAD_CONSTEXPR
struct raise_if
{
BOOST_STATIC_STRING_CPP14_CONSTEXPR
raise_if(
bool check,
const char* message)
{
BOOST_STATIC_STRING_THROW_IF(
check, std::out_of_range(message));
}
};
struct raise
{
BOOST_STATIC_STRING_NORETURN
BOOST_STATIC_STRING_CPP14_CONSTEXPR
raise(const char* message)
{
BOOST_STATIC_STRING_THROW(std::out_of_range(message));
}
};
#else
BOOST_STATIC_STRING_CPP14_CONSTEXPR
static
void
raise_if(
bool check,
const char* message)
{
BOOST_STATIC_STRING_THROW_IF(
check, std::out_of_range(message));
}
BOOST_STATIC_STRING_NORETURN
static
void
raise(const char* message)
{
BOOST_STATIC_STRING_THROW(std::out_of_range(message));
}
#endif
};
} // detail } // detail
#endif #endif
@ -1654,7 +1606,7 @@ public:
reference reference
at(size_type pos) at(size_type pos)
{ {
detail::out_of_range::raise_if( detail::exception<std::out_of_range>::raise_if(
pos >= size(), pos >= size(),
"pos >= size()"); "pos >= size()");
return data()[pos]; return data()[pos];
@ -1681,7 +1633,7 @@ public:
const_reference const_reference
at(size_type pos) const at(size_type pos) const
{ {
detail::out_of_range::raise_if( detail::exception<std::out_of_range>::raise_if(
pos >= size(), pos >= size(),
"pos >= size()"); "pos >= size()");
return data()[pos]; return data()[pos];
@ -2069,7 +2021,7 @@ public:
void void
reserve(size_type n) reserve(size_type n)
{ {
detail::length_error::raise_if( detail::exception<std::length_error>::raise_if(
n > max_size(), n > max_size(),
"n > max_size()"); "n > max_size()");
} }
@ -2153,7 +2105,7 @@ public:
size_type count, size_type count,
value_type ch) value_type ch)
{ {
detail::out_of_range::raise_if( detail::exception<std::out_of_range>::raise_if(
index > size(), "index > size()"); index > size(), "index > size()");
insert(begin() + index, count, ch); insert(begin() + index, count, ch);
return *this; return *this;
@ -2219,7 +2171,7 @@ public:
const_pointer s, const_pointer s,
size_type count) size_type count)
{ {
detail::out_of_range::raise_if( detail::exception<std::out_of_range>::raise_if(
index > size(), "index > size()"); index > size(), "index > size()");
insert(data() + index, s, s + count); insert(data() + index, s, s + count);
return *this; return *this;
@ -5294,7 +5246,7 @@ private:
basic_static_string& basic_static_string&
assign_char(value_type, std::false_type) assign_char(value_type, std::false_type)
{ {
detail::length_error::raise("max_size() == 0"); detail::exception<std::length_error>::raise("max_size() == 0");
// This eliminates any potential warnings // This eliminates any potential warnings
#ifdef BOOST_STATIC_STRING_NO_NORETURN #ifdef BOOST_STATIC_STRING_NO_NORETURN
return *this; return *this;
@ -5318,7 +5270,7 @@ private:
const_pointer s, const_pointer s,
size_type n2) size_type n2)
{ {
detail::out_of_range::raise_if( detail::exception<std::out_of_range>::raise_if(
pos > size(), "pos > size()"); pos > size(), "pos > size()");
return replace_unchecked(data() + pos, data() + pos + capped_length(pos, n1), s, n2); return replace_unchecked(data() + pos, data() + pos + capped_length(pos, n1), s, n2);
} }
@ -5338,7 +5290,7 @@ private:
const_pointer s, const_pointer s,
size_type count) size_type count)
{ {
detail::out_of_range::raise_if( detail::exception<std::out_of_range>::raise_if(
index > size(), "index > size()"); index > size(), "index > size()");
insert_unchecked(data() + index, s, count); insert_unchecked(data() + index, s, count);
return *this; return *this;
@ -5368,7 +5320,7 @@ private:
size_type index, size_type index,
size_type length) const size_type length) const
{ {
detail::out_of_range::raise_if( detail::exception<std::out_of_range>::raise_if(
index > size(), "index > size()"); index > size(), "index > size()");
return (std::min)(size() - index, length); return (std::min)(size() - index, length);
} }
@ -5994,7 +5946,7 @@ assign(
value_type ch) -> value_type ch) ->
basic_static_string& basic_static_string&
{ {
detail::length_error::raise_if( detail::exception<std::length_error>::raise_if(
count > max_size(), "count > max_size()"); count > max_size(), "count > max_size()");
this->set_size(count); this->set_size(count);
traits_type::assign(data(), size(), ch); traits_type::assign(data(), size(), ch);
@ -6010,7 +5962,7 @@ assign(
size_type count) -> size_type count) ->
basic_static_string& basic_static_string&
{ {
detail::length_error::raise_if( detail::exception<std::length_error>::raise_if(
count > max_size(), "count > max_size()"); count > max_size(), "count > max_size()");
this->set_size(count); this->set_size(count);
traits_type::move(data(), s, size()); traits_type::move(data(), s, size());
@ -6036,7 +5988,7 @@ assign(
{ {
this->set_size(i); this->set_size(i);
term(); term();
detail::length_error::raise("n > max_size()"); detail::exception<std::length_error>::raise("n > max_size()");
} }
traits_type::assign(*ptr, *first); traits_type::assign(*ptr, *first);
} }
@ -6056,7 +6008,7 @@ insert(
{ {
const auto curr_size = size(); const auto curr_size = size();
const auto curr_data = data(); const auto curr_data = data();
detail::length_error::raise_if( detail::exception<std::length_error>::raise_if(
count > max_size() - curr_size, count > max_size() - curr_size,
"count > max_size() - curr_size"); "count > max_size() - curr_size");
const auto index = pos - curr_data; const auto index = pos - curr_data;
@ -6084,7 +6036,7 @@ insert(
const std::size_t count = detail::distance(first, last); const std::size_t count = detail::distance(first, last);
const std::size_t index = pos - curr_data; const std::size_t index = pos - curr_data;
const auto first_addr = &*first; const auto first_addr = &*first;
detail::length_error::raise_if( detail::exception<std::length_error>::raise_if(
count > max_size() - curr_size, count > max_size() - curr_size,
"count > max_size() - curr_size"); "count > max_size() - curr_size");
const bool inside = detail::ptr_in_range(curr_data, curr_data + curr_size, first_addr); const bool inside = detail::ptr_in_range(curr_data, curr_data + curr_size, first_addr);
@ -6160,7 +6112,7 @@ push_back(
value_type ch) value_type ch)
{ {
const auto curr_size = size(); const auto curr_size = size();
detail::length_error::raise_if( detail::exception<std::length_error>::raise_if(
curr_size >= max_size(), "curr_size >= max_size()"); curr_size >= max_size(), "curr_size >= max_size()");
traits_type::assign(data()[curr_size], ch); traits_type::assign(data()[curr_size], ch);
this->set_size(curr_size + 1); this->set_size(curr_size + 1);
@ -6177,7 +6129,7 @@ append(
basic_static_string& basic_static_string&
{ {
const auto curr_size = size(); const auto curr_size = size();
detail::length_error::raise_if( detail::exception<std::length_error>::raise_if(
count > max_size() - curr_size, "count > max_size() - size()"); count > max_size() - curr_size, "count > max_size() - size()");
traits_type::assign(end(), count, ch); traits_type::assign(end(), count, ch);
this->set_size(curr_size + count); this->set_size(curr_size + count);
@ -6194,7 +6146,7 @@ append(
basic_static_string& basic_static_string&
{ {
const auto curr_size = size(); const auto curr_size = size();
detail::length_error::raise_if( detail::exception<std::length_error>::raise_if(
count > max_size() - curr_size, "count > max_size() - size()"); count > max_size() - curr_size, "count > max_size() - size()");
traits_type::copy(end(), s, count); traits_type::copy(end(), s, count);
this->set_size(curr_size + count); this->set_size(curr_size + count);
@ -6207,7 +6159,7 @@ void
basic_static_string<N, CharT, Traits>:: basic_static_string<N, CharT, Traits>::
resize(size_type n, value_type c) resize(size_type n, value_type c)
{ {
detail::length_error::raise_if( detail::exception<std::length_error>::raise_if(
n > max_size(), "n > max_size()"); n > max_size(), "n > max_size()");
const auto curr_size = size(); const auto curr_size = size();
if(n > curr_size) if(n > curr_size)
@ -6238,9 +6190,9 @@ basic_static_string<N, CharT, Traits>::
swap(basic_static_string<M, CharT, Traits>& s) swap(basic_static_string<M, CharT, Traits>& s)
{ {
const auto curr_size = size(); const auto curr_size = size();
detail::length_error::raise_if( detail::exception<std::length_error>::raise_if(
curr_size > s.max_size(), "curr_size > s.max_size()"); curr_size > s.max_size(), "curr_size > s.max_size()");
detail::length_error::raise_if( detail::exception<std::length_error>::raise_if(
s.size() > max_size(), "s.size() > max_size()"); s.size() > max_size(), "s.size() > max_size()");
basic_static_string tmp(s); basic_static_string tmp(s);
s.set_size(curr_size); s.set_size(curr_size);
@ -6263,7 +6215,7 @@ replace(
const auto curr_size = size(); const auto curr_size = size();
const auto curr_data = data(); const auto curr_data = data();
const std::size_t n1 = i2 - i1; const std::size_t n1 = i2 - i1;
detail::length_error::raise_if( detail::exception<std::length_error>::raise_if(
n > max_size() || n > max_size() ||
curr_size - n1 >= max_size() - n, curr_size - n1 >= max_size() - n,
"replaced string exceeds max_size()"); "replaced string exceeds max_size()");
@ -6294,7 +6246,7 @@ replace(
const std::size_t n1 = i2 - i1; const std::size_t n1 = i2 - i1;
const std::size_t n2 = detail::distance(j1, j2); const std::size_t n2 = detail::distance(j1, j2);
const std::size_t pos = i1 - curr_data; const std::size_t pos = i1 - curr_data;
detail::length_error::raise_if( detail::exception<std::length_error>::raise_if(
n2 > max_size() || n2 > max_size() ||
curr_size - (std::min)(n1, curr_size - pos) >= max_size() - n2, curr_size - (std::min)(n1, curr_size - pos) >= max_size() - n2,
"replaced string exceeds max_size()"); "replaced string exceeds max_size()");
@ -6358,7 +6310,7 @@ replace(
const std::size_t n1 = detail::distance(i1, i2); const std::size_t n1 = detail::distance(i1, i2);
const std::size_t n2 = read_back(false, j1, j2); const std::size_t n2 = read_back(false, j1, j2);
const std::size_t pos = i1 - curr_data; const std::size_t pos = i1 - curr_data;
detail::length_error::raise_if( detail::exception<std::length_error>::raise_if(
n2 > max_size() || n2 > max_size() ||
curr_size - (std::min)(n1, curr_size - pos) >= max_size() - n2, curr_size - (std::min)(n1, curr_size - pos) >= max_size() - n2,
"replaced string exceeds max_size()"); "replaced string exceeds max_size()");
@ -6510,7 +6462,7 @@ read_back(
{ {
if (overwrite_null) if (overwrite_null)
term(); term();
detail::length_error::raise( detail::exception<std::length_error>::raise(
"count > max_size() - size()"); "count > max_size() - size()");
} }
traits_type::assign(curr_data[new_size++ + (!overwrite_null)], *first); traits_type::assign(curr_data[new_size++ + (!overwrite_null)], *first);
@ -6533,7 +6485,7 @@ replace_unchecked(
const auto curr_size = size(); const auto curr_size = size();
const std::size_t pos = i1 - curr_data; const std::size_t pos = i1 - curr_data;
const std::size_t n1 = i2 - i1; const std::size_t n1 = i2 - i1;
detail::length_error::raise_if( detail::exception<std::length_error>::raise_if(
n2 > max_size() || n2 > max_size() ||
curr_size - (std::min)(n1, curr_size - pos) >= max_size() - n2, curr_size - (std::min)(n1, curr_size - pos) >= max_size() - n2,
"replaced string exceeds max_size()"); "replaced string exceeds max_size()");
@ -6555,7 +6507,7 @@ insert_unchecked(
{ {
const auto curr_data = data(); const auto curr_data = data();
const auto curr_size = size(); const auto curr_size = size();
detail::length_error::raise_if( detail::exception<std::length_error>::raise_if(
count > max_size() - curr_size, count > max_size() - curr_size,
"count > max_size() - curr_size"); "count > max_size() - curr_size");
const std::size_t index = pos - curr_data; const std::size_t index = pos - curr_data;