Add macro to disable exceptions

This commit is contained in:
Krystian Stasiowski
2019-12-16 17:36:12 -05:00
parent 77030a7e70
commit 41bb1a550d
3 changed files with 68 additions and 76 deletions

View File

@ -14,6 +14,9 @@
// Are we dependent on Boost? // Are we dependent on Boost?
// #define BOOST_STATIC_STRING_STANDALONE // #define BOOST_STATIC_STRING_STANDALONE
// Disable exceptions and their associated checks
// #define BOOST_STATIC_STRING_NO_EXCEPTIONS
// Can we have deduction guides? // Can we have deduction guides?
#ifdef __cpp_deduction_guides #ifdef __cpp_deduction_guides
#define BOOST_STATIC_STRING_USE_DEDUCT #define BOOST_STATIC_STRING_USE_DEDUCT
@ -77,8 +80,16 @@
#include <string_view> #include <string_view>
#endif #endif
#ifdef BOOST_STATIC_STRING_NO_EXCEPTIONS
#define BOOST_STATIC_STRING_THROW_IF(cond, ex)
#define BOOST_STATIC_STRING_THROW(ex)
#endif
// 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_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
@ -89,6 +100,9 @@
#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) 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

@ -77,9 +77,8 @@ basic_static_string<N, CharT, Traits>::
basic_static_string(CharT const* s) basic_static_string(CharT const* s)
{ {
auto const count = Traits::length(s); auto const count = Traits::length(s);
if(count > max_size()) BOOST_STATIC_STRING_THROW_IF(count > max_size(),
BOOST_STATIC_STRING_THROW(std::length_error{ std::length_error{"count > max_size()"});
"count > max_size()"});
this->set_size(count); this->set_size(count);
Traits::copy(data(), s, size() + 1); Traits::copy(data(), s, size() + 1);
} }
@ -157,9 +156,8 @@ assign(
CharT ch) -> CharT ch) ->
basic_static_string& basic_static_string&
{ {
if(count > max_size()) BOOST_STATIC_STRING_THROW_IF(count > max_size(),
BOOST_STATIC_STRING_THROW(std::length_error{ std::length_error{"count > max_size()"});
"count > max_size()"});
this->set_size(count); this->set_size(count);
Traits::assign(data(), size(), ch); Traits::assign(data(), size(), ch);
term(); term();
@ -206,9 +204,8 @@ assign(
size_type count) -> size_type count) ->
basic_static_string& basic_static_string&
{ {
if(count > max_size()) BOOST_STATIC_STRING_THROW_IF(count > max_size(),
BOOST_STATIC_STRING_THROW(std::length_error{ std::length_error{"count > max_size()"});
"count > max_size()"});
this->set_size(count); this->set_size(count);
Traits::move(data(), s, size()); Traits::move(data(), s, size());
term(); term();
@ -228,9 +225,8 @@ assign(
basic_static_string&>::type basic_static_string&>::type
{ {
std::size_t const n = std::distance(first, last); std::size_t const n = std::distance(first, last);
if(n > max_size()) BOOST_STATIC_STRING_THROW_IF(n > max_size(),
BOOST_STATIC_STRING_THROW(std::length_error{ std::length_error{"n > max_size()"});
"n > max_size()"});
this->set_size(n); this->set_size(n);
for(auto it = data(); first != last; ++it, ++first) for(auto it = data(); first != last; ++it, ++first)
Traits::assign(*it, *first); Traits::assign(*it, *first);
@ -251,9 +247,8 @@ basic_static_string<N, CharT, Traits>::
at(size_type pos) -> at(size_type pos) ->
reference reference
{ {
if(pos >= size()) BOOST_STATIC_STRING_THROW_IF(
BOOST_STATIC_STRING_THROW(std::out_of_range{ pos >= size(), std::out_of_range{"pos >= size()"});
"pos >= size()"});
return data()[pos]; return data()[pos];
} }
@ -264,9 +259,8 @@ basic_static_string<N, CharT, Traits>::
at(size_type pos) const -> at(size_type pos) const ->
const_reference const_reference
{ {
if(pos >= size()) BOOST_STATIC_STRING_THROW_IF(
BOOST_STATIC_STRING_THROW(std::out_of_range{ pos >= size(), std::out_of_range{"pos >= size()"});
"pos >= size()"});
return data()[pos]; return data()[pos];
} }
@ -282,9 +276,8 @@ void
basic_static_string<N, CharT, Traits>:: basic_static_string<N, CharT, Traits>::
reserve(std::size_t n) reserve(std::size_t n)
{ {
if(n > max_size()) BOOST_STATIC_STRING_THROW_IF(
BOOST_STATIC_STRING_THROW(std::length_error{ n > max_size(), std::out_of_range{"n > max_size()"});
"n > max_size()"});
} }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
@ -315,9 +308,8 @@ insert(
CharT ch) -> CharT ch) ->
basic_static_string& basic_static_string&
{ {
if(index > size()) BOOST_STATIC_STRING_THROW_IF(
BOOST_STATIC_STRING_THROW(std::out_of_range{ index > size(), std::out_of_range{"index > size()"});
"index > size()"});
insert(begin() + index, count, ch); insert(begin() + index, count, ch);
return *this; return *this;
} }
@ -334,12 +326,10 @@ insert(
{ {
const auto curr_size = size(); const auto curr_size = size();
const auto curr_data = data(); const auto curr_data = data();
if(index > curr_size) BOOST_STATIC_STRING_THROW_IF(
BOOST_STATIC_STRING_THROW(std::out_of_range{ index > curr_size, std::out_of_range{"index > size()"});
"index > size()"}); BOOST_STATIC_STRING_THROW_IF(
if(count > max_size() - curr_size) count > max_size() - curr_size, std::length_error{"count > max_size() - size()"});
BOOST_STATIC_STRING_THROW(std::length_error{
"count > max_size() - size()"});
const bool inside = s <= &curr_data[curr_size] && s >= curr_data; const bool inside = s <= &curr_data[curr_size] && s >= curr_data;
if (!inside || (inside && ((s - curr_data) + count <= index))) if (!inside || (inside && ((s - curr_data) + count <= index)))
{ {
@ -377,9 +367,8 @@ insert(
{ {
const auto curr_size = size(); const auto curr_size = size();
const auto curr_data = data(); const auto curr_data = data();
if(count > max_size() - curr_size) BOOST_STATIC_STRING_THROW_IF(
BOOST_STATIC_STRING_THROW(std::length_error{ count > max_size() - curr_size, std::length_error{"count() > max_size() - size()"});
"count() > max_size() - size()"});
auto const index = pos - curr_data; auto const index = pos - curr_data;
Traits::move(&curr_data[index + count], &curr_data[index], curr_size - index); Traits::move(&curr_data[index + count], &curr_data[index], curr_size - index);
this->set_size(curr_size + count); this->set_size(curr_size + count);
@ -458,9 +447,8 @@ erase(
{ {
const auto curr_size = size(); const auto curr_size = size();
const auto curr_data = data(); const auto curr_data = data();
if(index > curr_size) BOOST_STATIC_STRING_THROW_IF(
BOOST_STATIC_STRING_THROW(std::out_of_range{ index > curr_size, std::out_of_range{"index > size()"});
"index > size()"});
auto const n = (std::min)(count, curr_size - index); auto const n = (std::min)(count, curr_size - index);
Traits::move(&curr_data[index], &curr_data[index + n], curr_size - (index + n) + 1); Traits::move(&curr_data[index], &curr_data[index + n], curr_size - (index + n) + 1);
this->set_size(curr_size - n); this->set_size(curr_size - n);
@ -501,9 +489,8 @@ push_back(
CharT ch) CharT ch)
{ {
const auto curr_size = size(); const auto curr_size = size();
if(curr_size >= max_size()) BOOST_STATIC_STRING_THROW_IF(
BOOST_STATIC_STRING_THROW(std::length_error{ curr_size >= max_size(), std::length_error{"size() >= max_size()"});
"size() >= max_size()"});
Traits::assign(data()[curr_size], ch); Traits::assign(data()[curr_size], ch);
this->set_size(curr_size + 1); this->set_size(curr_size + 1);
term(); term();
@ -519,9 +506,8 @@ append(
basic_static_string& basic_static_string&
{ {
const auto curr_size = size(); const auto curr_size = size();
if(count > max_size() - curr_size) BOOST_STATIC_STRING_THROW_IF(
BOOST_STATIC_STRING_THROW(std::length_error{ count > max_size() - curr_size, std::length_error{"count > max_size() - size()"});
"count > max_size() - size()"});
Traits::copy(&data()[curr_size], s, count); Traits::copy(&data()[curr_size], s, count);
this->set_size(curr_size + count); this->set_size(curr_size + count);
term(); term();
@ -535,9 +521,8 @@ basic_static_string<N, CharT, Traits>::
substr(size_type pos, size_type count) const -> substr(size_type pos, size_type count) const ->
basic_static_string basic_static_string
{ {
if(pos > size()) BOOST_STATIC_STRING_THROW_IF(
BOOST_STATIC_STRING_THROW(std::out_of_range{ pos > size(), std::out_of_range{"pos > size()"});
"pos > size()"});
return {&data()[pos], (std::min)(count, size() - pos)}; return {&data()[pos], (std::min)(count, size() - pos)};
} }
@ -548,9 +533,8 @@ basic_static_string<N, CharT, Traits>::
subview(size_type pos, size_type count) const -> subview(size_type pos, size_type count) const ->
string_view_type string_view_type
{ {
if (pos > size()) BOOST_STATIC_STRING_THROW_IF(
BOOST_STATIC_STRING_THROW(std::out_of_range{ pos > size(), std::out_of_range{"pos > size()"});
"pos > size()"});
return {&data()[pos], (std::min)(count, size() - pos)}; return {&data()[pos], (std::min)(count, size() - pos)};
} }
@ -573,9 +557,8 @@ basic_static_string<N, CharT, Traits>::
resize(std::size_t n) resize(std::size_t n)
{ {
const auto curr_size = size(); const auto curr_size = size();
if(n > max_size()) BOOST_STATIC_STRING_THROW_IF(
BOOST_STATIC_STRING_THROW(std::length_error{ n > max_size(), std::length_error{"n > max_size()"});
"n > max_size()"});
if(n > curr_size) if(n > curr_size)
Traits::assign(&data()[curr_size], n - curr_size, CharT{}); Traits::assign(&data()[curr_size], n - curr_size, CharT{});
this->set_size(n); this->set_size(n);
@ -589,9 +572,8 @@ basic_static_string<N, CharT, Traits>::
resize(std::size_t n, CharT c) resize(std::size_t n, CharT c)
{ {
const auto curr_size = size(); const auto curr_size = size();
if(n > max_size()) BOOST_STATIC_STRING_THROW_IF(
BOOST_STATIC_STRING_THROW(std::length_error{ n > max_size(), std::length_error{"n > max_size()"});
"n > max_size()"});
if(n > curr_size) if(n > curr_size)
Traits::assign(&data()[curr_size], n - curr_size, c); Traits::assign(&data()[curr_size], n - curr_size, c);
this->set_size(n); this->set_size(n);
@ -620,12 +602,10 @@ 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();
if(curr_size > s.max_size()) BOOST_STATIC_STRING_THROW_IF(
BOOST_STATIC_STRING_THROW(std::length_error{ curr_size > s.max_size(), std::length_error{"size() > s.max_size()"});
"size() > s.max_size()"}); BOOST_STATIC_STRING_THROW_IF(
if(s.size() > max_size()) s.size() > max_size(), std::length_error{"s.size() > max_size()"});
BOOST_STATIC_STRING_THROW(std::length_error{
"s.size() > max_size()"});
basic_static_string tmp(s); basic_static_string tmp(s);
s.set_size(curr_size); s.set_size(curr_size);
Traits::copy(&s.data()[0], data(), curr_size + 1); Traits::copy(&s.data()[0], data(), curr_size + 1);
@ -645,12 +625,11 @@ replace(
{ {
const auto curr_size = size(); const auto curr_size = size();
const auto curr_data = data(); const auto curr_data = data();
if (pos > curr_size) BOOST_STATIC_STRING_THROW_IF(
BOOST_STATIC_STRING_THROW(std::out_of_range{ pos > curr_size, std::out_of_range{"pos > size()"});
"pos > size()"}); BOOST_STATIC_STRING_THROW_IF(
if (curr_size - (std::min)(n1, curr_size - pos) >= max_size() - n2) curr_size - (std::min)(n1, curr_size - pos) >= max_size() - n2,
BOOST_STATIC_STRING_THROW(std::length_error{ std::length_error{"replaced string exceeds max_size()"});
"replaced string exceeds max_size()"});
if (pos + n1 >= curr_size) if (pos + n1 >= curr_size)
n1 = curr_size - pos; n1 = curr_size - pos;
const bool inside = s <= &curr_data[curr_size] && s >= curr_data; const bool inside = s <= &curr_data[curr_size] && s >= curr_data;
@ -702,12 +681,11 @@ replace(
{ {
const auto curr_size = size(); const auto curr_size = size();
const auto curr_data = data(); const auto curr_data = data();
if (pos > curr_size) BOOST_STATIC_STRING_THROW_IF(
BOOST_STATIC_STRING_THROW(std::out_of_range{ pos > curr_size, std::out_of_range{"pos > size()"});
"pos > size()"}); BOOST_STATIC_STRING_THROW_IF(
if (curr_size - (std::min)(n1, curr_size - pos) >= max_size() - n2) curr_size - (std::min)(n1, curr_size - pos) >= max_size() - n2,
BOOST_STATIC_STRING_THROW(std::length_error{ std::length_error{"replaced string exceeds max_size()"});
"replaced string exceeds max_size()"});
if (pos + n1 >= curr_size) if (pos + n1 >= curr_size)
n1 = curr_size - pos; n1 = curr_size - pos;
Traits::move(&curr_data[pos + n2], &curr_data[pos + n1], curr_size - pos - n1 + 1); Traits::move(&curr_data[pos + n2], &curr_data[pos + n1], curr_size - pos - n1 + 1);
@ -854,8 +832,8 @@ basic_static_string<N, CharT, Traits>::
assign_char(CharT, std::false_type) -> assign_char(CharT, std::false_type) ->
basic_static_string& basic_static_string&
{ {
BOOST_STATIC_STRING_THROW(std::length_error{ BOOST_STATIC_STRING_THROW(std::length_error{"max_size() == 0"});
"max_size() == 0"}); return *this;
} }
template<class Integer, class> template<class Integer, class>

View File

@ -285,7 +285,7 @@ public:
CharT ch) CharT ch)
{ {
return assign_char(ch, return assign_char(ch,
std::integral_constant<bool, (N>0)>{}); std::integral_constant<bool, (N > 0)>{});
} }
/** Assign to the string. /** Assign to the string.