From 7b27ebea33049eac75244e63ee4497553390afe7 Mon Sep 17 00:00:00 2001 From: Krystian Stasiowski Date: Mon, 9 Mar 2020 22:55:38 -0400 Subject: [PATCH] Genericize exception classes --- include/boost/static_string/config.hpp | 6 - include/boost/static_string/static_string.hpp | 124 ++++++------------ 2 files changed, 38 insertions(+), 92 deletions(-) diff --git a/include/boost/static_string/config.hpp b/include/boost/static_string/config.hpp index 3f406f5..b2f502c 100644 --- a/include/boost/static_string/config.hpp +++ b/include/boost/static_string/config.hpp @@ -125,9 +125,6 @@ // Boost and non-Boost versions of utilities #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 #define BOOST_STATIC_STRING_THROW(ex) BOOST_THROW_EXCEPTION(ex) #endif @@ -138,9 +135,6 @@ #define BOOST_STATIC_STRING_ASSERT(cond) BOOST_ASSERT(cond) #endif #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 #define BOOST_STATIC_STRING_THROW(ex) throw ex #endif diff --git a/include/boost/static_string/static_string.hpp b/include/boost/static_string/static_string.hpp index 4ae139c..50f228f 100644 --- a/include/boost/static_string/static_string.hpp +++ b/include/boost/static_string/static_string.hpp @@ -730,13 +730,12 @@ defined(BOOST_STATIC_STRING_NO_PTR_COMP_FUNCTIONS) #endif } -//-------------------------------------------------------------------------- -// -// exception classes -// -//-------------------------------------------------------------------------- - -struct length_error +// This nested type workaround is for gcc 5, +// which prohibits throw expressions in constexpr +// functions, but for some reason permits them in +// constructors. +template +struct exception { #ifdef BOOST_STATIC_STRING_GCC5_BAD_CONSTEXPR struct raise_if @@ -746,8 +745,8 @@ struct length_error bool check, const char* message) { - BOOST_STATIC_STRING_THROW_IF( - check, std::length_error(message)); + if (check) + BOOST_STATIC_STRING_THROW(Exception(message)); } }; @@ -757,7 +756,7 @@ struct length_error BOOST_STATIC_STRING_CPP14_CONSTEXPR raise(const char* message) { - BOOST_STATIC_STRING_THROW(std::length_error(message)); + BOOST_STATIC_STRING_THROW(Exception(message)); } }; #else @@ -768,8 +767,8 @@ struct length_error bool check, const char* message) { - BOOST_STATIC_STRING_THROW_IF( - check, std::length_error(message)); + if (check) + BOOST_STATIC_STRING_THROW(Exception(message)); } BOOST_STATIC_STRING_NORETURN @@ -777,57 +776,10 @@ struct length_error void raise(const char* message) { - BOOST_STATIC_STRING_THROW(std::length_error(message)); + BOOST_STATIC_STRING_THROW(Exception(message)); } #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 #endif @@ -1654,7 +1606,7 @@ public: reference at(size_type pos) { - detail::out_of_range::raise_if( + detail::exception::raise_if( pos >= size(), "pos >= size()"); return data()[pos]; @@ -1681,7 +1633,7 @@ public: const_reference at(size_type pos) const { - detail::out_of_range::raise_if( + detail::exception::raise_if( pos >= size(), "pos >= size()"); return data()[pos]; @@ -2069,7 +2021,7 @@ public: void reserve(size_type n) { - detail::length_error::raise_if( + detail::exception::raise_if( n > max_size(), "n > max_size()"); } @@ -2153,7 +2105,7 @@ public: size_type count, value_type ch) { - detail::out_of_range::raise_if( + detail::exception::raise_if( index > size(), "index > size()"); insert(begin() + index, count, ch); return *this; @@ -2219,7 +2171,7 @@ public: const_pointer s, size_type count) { - detail::out_of_range::raise_if( + detail::exception::raise_if( index > size(), "index > size()"); insert(data() + index, s, s + count); return *this; @@ -5294,7 +5246,7 @@ private: basic_static_string& assign_char(value_type, std::false_type) { - detail::length_error::raise("max_size() == 0"); + detail::exception::raise("max_size() == 0"); // This eliminates any potential warnings #ifdef BOOST_STATIC_STRING_NO_NORETURN return *this; @@ -5318,7 +5270,7 @@ private: const_pointer s, size_type n2) { - detail::out_of_range::raise_if( + detail::exception::raise_if( pos > size(), "pos > size()"); return replace_unchecked(data() + pos, data() + pos + capped_length(pos, n1), s, n2); } @@ -5338,7 +5290,7 @@ private: const_pointer s, size_type count) { - detail::out_of_range::raise_if( + detail::exception::raise_if( index > size(), "index > size()"); insert_unchecked(data() + index, s, count); return *this; @@ -5368,7 +5320,7 @@ private: size_type index, size_type length) const { - detail::out_of_range::raise_if( + detail::exception::raise_if( index > size(), "index > size()"); return (std::min)(size() - index, length); } @@ -5994,7 +5946,7 @@ assign( value_type ch) -> basic_static_string& { - detail::length_error::raise_if( + detail::exception::raise_if( count > max_size(), "count > max_size()"); this->set_size(count); traits_type::assign(data(), size(), ch); @@ -6010,7 +5962,7 @@ assign( size_type count) -> basic_static_string& { - detail::length_error::raise_if( + detail::exception::raise_if( count > max_size(), "count > max_size()"); this->set_size(count); traits_type::move(data(), s, size()); @@ -6036,7 +5988,7 @@ assign( { this->set_size(i); term(); - detail::length_error::raise("n > max_size()"); + detail::exception::raise("n > max_size()"); } traits_type::assign(*ptr, *first); } @@ -6056,7 +6008,7 @@ insert( { const auto curr_size = size(); const auto curr_data = data(); - detail::length_error::raise_if( + detail::exception::raise_if( count > max_size() - curr_size, "count > max_size() - curr_size"); const auto index = pos - curr_data; @@ -6084,7 +6036,7 @@ insert( const std::size_t count = detail::distance(first, last); const std::size_t index = pos - curr_data; const auto first_addr = &*first; - detail::length_error::raise_if( + detail::exception::raise_if( 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); @@ -6160,7 +6112,7 @@ push_back( value_type ch) { const auto curr_size = size(); - detail::length_error::raise_if( + detail::exception::raise_if( curr_size >= max_size(), "curr_size >= max_size()"); traits_type::assign(data()[curr_size], ch); this->set_size(curr_size + 1); @@ -6177,7 +6129,7 @@ append( basic_static_string& { const auto curr_size = size(); - detail::length_error::raise_if( + detail::exception::raise_if( count > max_size() - curr_size, "count > max_size() - size()"); traits_type::assign(end(), count, ch); this->set_size(curr_size + count); @@ -6194,7 +6146,7 @@ append( basic_static_string& { const auto curr_size = size(); - detail::length_error::raise_if( + detail::exception::raise_if( count > max_size() - curr_size, "count > max_size() - size()"); traits_type::copy(end(), s, count); this->set_size(curr_size + count); @@ -6207,7 +6159,7 @@ void basic_static_string:: resize(size_type n, value_type c) { - detail::length_error::raise_if( + detail::exception::raise_if( n > max_size(), "n > max_size()"); const auto curr_size = size(); if(n > curr_size) @@ -6238,9 +6190,9 @@ basic_static_string:: swap(basic_static_string& s) { const auto curr_size = size(); - detail::length_error::raise_if( + detail::exception::raise_if( curr_size > s.max_size(), "curr_size > s.max_size()"); - detail::length_error::raise_if( + detail::exception::raise_if( s.size() > max_size(), "s.size() > max_size()"); basic_static_string tmp(s); s.set_size(curr_size); @@ -6263,7 +6215,7 @@ replace( const auto curr_size = size(); const auto curr_data = data(); const std::size_t n1 = i2 - i1; - detail::length_error::raise_if( + detail::exception::raise_if( n > max_size() || curr_size - n1 >= max_size() - n, "replaced string exceeds max_size()"); @@ -6294,7 +6246,7 @@ replace( const std::size_t n1 = i2 - i1; const std::size_t n2 = detail::distance(j1, j2); const std::size_t pos = i1 - curr_data; - detail::length_error::raise_if( + detail::exception::raise_if( n2 > max_size() || curr_size - (std::min)(n1, curr_size - pos) >= max_size() - n2, "replaced string exceeds max_size()"); @@ -6358,7 +6310,7 @@ replace( const std::size_t n1 = detail::distance(i1, i2); const std::size_t n2 = read_back(false, j1, j2); const std::size_t pos = i1 - curr_data; - detail::length_error::raise_if( + detail::exception::raise_if( n2 > max_size() || curr_size - (std::min)(n1, curr_size - pos) >= max_size() - n2, "replaced string exceeds max_size()"); @@ -6510,7 +6462,7 @@ read_back( { if (overwrite_null) term(); - detail::length_error::raise( + detail::exception::raise( "count > max_size() - size()"); } traits_type::assign(curr_data[new_size++ + (!overwrite_null)], *first); @@ -6533,7 +6485,7 @@ replace_unchecked( const auto curr_size = size(); const std::size_t pos = i1 - curr_data; const std::size_t n1 = i2 - i1; - detail::length_error::raise_if( + detail::exception::raise_if( n2 > max_size() || curr_size - (std::min)(n1, curr_size - pos) >= max_size() - n2, "replaced string exceeds max_size()"); @@ -6555,7 +6507,7 @@ insert_unchecked( { const auto curr_data = data(); const auto curr_size = size(); - detail::length_error::raise_if( + detail::exception::raise_if( count > max_size() - curr_size, "count > max_size() - curr_size"); const std::size_t index = pos - curr_data;