Skip to content

Commit

Permalink
default source_location argument for throwing functions
Browse files Browse the repository at this point in the history
  • Loading branch information
grisumbras committed Jan 9, 2023
1 parent 36552f0 commit 607b21d
Show file tree
Hide file tree
Showing 16 changed files with 63 additions and 138 deletions.
10 changes: 5 additions & 5 deletions include/boost/json/detail/except.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ namespace detail {
BOOST_STATIC_CONSTEXPR source_location loc = BOOST_CURRENT_LOCATION; \
(ec).assign(e, &loc);

BOOST_JSON_DECL void BOOST_NORETURN throw_bad_alloc(source_location const& loc);
BOOST_JSON_DECL void BOOST_NORETURN throw_invalid_argument(char const* what, source_location const& loc);
BOOST_JSON_DECL void BOOST_NORETURN throw_length_error(char const* what, source_location const& loc);
BOOST_JSON_DECL void BOOST_NORETURN throw_out_of_range(source_location const& loc);
BOOST_JSON_DECL void BOOST_NORETURN throw_system_error(error_code const& ec, source_location const& loc);
BOOST_JSON_DECL void BOOST_NORETURN throw_bad_alloc(source_location const& loc = BOOST_CURRENT_LOCATION);
BOOST_JSON_DECL void BOOST_NORETURN throw_invalid_argument(char const* what, source_location const& loc = BOOST_CURRENT_LOCATION);
BOOST_JSON_DECL void BOOST_NORETURN throw_length_error(char const* what, source_location const& loc = BOOST_CURRENT_LOCATION);
BOOST_JSON_DECL void BOOST_NORETURN throw_out_of_range(source_location const& loc = BOOST_CURRENT_LOCATION);
BOOST_JSON_DECL void BOOST_NORETURN throw_system_error(error_code const& ec, source_location const& loc = BOOST_CURRENT_LOCATION);

} // detail
} // namespace json
Expand Down
36 changes: 10 additions & 26 deletions include/boost/json/detail/impl/string_impl.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,7 @@ growth(
std::size_t capacity)
{
if(new_size > max_size())
detail::throw_length_error(
"string too large",
BOOST_CURRENT_LOCATION);
detail::throw_length_error( "string too large" );
// growth factor 2
if( capacity >
max_size() - capacity)
Expand Down Expand Up @@ -158,9 +156,7 @@ append(
storage_ptr const& sp)
{
if(n > max_size() - size())
detail::throw_length_error(
"string too large",
BOOST_CURRENT_LOCATION);
detail::throw_length_error( "string too large" );
if(n <= capacity() - size())
{
term(size() + n);
Expand All @@ -186,8 +182,7 @@ insert(
{
const auto curr_size = size();
if(pos > curr_size)
detail::throw_out_of_range(
BOOST_CURRENT_LOCATION);
detail::throw_out_of_range();
const auto curr_data = data();
if(n <= capacity() - curr_size)
{
Expand Down Expand Up @@ -217,9 +212,7 @@ insert(
else
{
if(n > max_size() - curr_size)
detail::throw_length_error(
"string too large",
BOOST_CURRENT_LOCATION);
detail::throw_length_error( "string too large" );
string_impl tmp(growth(
curr_size + n, capacity()), sp);
tmp.size(curr_size + n);
Expand Down Expand Up @@ -249,8 +242,7 @@ insert_unchecked(
{
const auto curr_size = size();
if(pos > curr_size)
detail::throw_out_of_range(
BOOST_CURRENT_LOCATION);
detail::throw_out_of_range();
const auto curr_data = data();
if(n <= capacity() - size())
{
Expand All @@ -264,9 +256,7 @@ insert_unchecked(
return dest;
}
if(n > max_size() - curr_size)
detail::throw_length_error(
"string too large",
BOOST_CURRENT_LOCATION);
detail::throw_length_error( "string too large" );
string_impl tmp(growth(
curr_size + n, capacity()), sp);
tmp.size(curr_size + n);
Expand Down Expand Up @@ -294,8 +284,7 @@ replace(
{
const auto curr_size = size();
if (pos > curr_size)
detail::throw_out_of_range(
BOOST_CURRENT_LOCATION);
detail::throw_out_of_range();
const auto curr_data = data();
n1 = (std::min)(n1, curr_size - pos);
const auto delta = (std::max)(n1, n2) -
Expand Down Expand Up @@ -343,9 +332,7 @@ replace(
else
{
if (delta > max_size() - curr_size)
detail::throw_length_error(
"string too large",
BOOST_CURRENT_LOCATION);
detail::throw_length_error( "string too large" );
// would exceed capacity, reallocate
string_impl tmp(growth(
curr_size + delta, capacity()), sp);
Expand Down Expand Up @@ -379,8 +366,7 @@ replace_unchecked(
{
const auto curr_size = size();
if(pos > curr_size)
detail::throw_out_of_range(
BOOST_CURRENT_LOCATION);
detail::throw_out_of_range();
const auto curr_data = data();
const auto delta = (std::max)(n1, n2) -
(std::min)(n1, n2);
Expand All @@ -401,9 +387,7 @@ replace_unchecked(
return replace_pos;
}
if(delta > max_size() - curr_size)
detail::throw_length_error(
"string too large",
BOOST_CURRENT_LOCATION);
detail::throw_length_error( "string too large" );
// would exceed capacity, reallocate
string_impl tmp(growth(
curr_size + delta, capacity()), sp);
Expand Down
14 changes: 7 additions & 7 deletions include/boost/json/detail/value_to.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -300,13 +300,13 @@ value_to_impl(
if( !obj )
{
BOOST_JSON_FAIL(ec, error::not_object);
throw_system_error(ec, BOOST_CURRENT_LOCATION);
throw_system_error( ec );
}

T result;
ec = detail::try_reserve(result, obj->size(), reserve_implementation<T>());
if( ec.failed() )
throw_system_error(ec, BOOST_CURRENT_LOCATION);
throw_system_error( ec );

auto ins = detail::inserter(result, inserter_implementation<T>());
for( key_value_pair const& kv: *obj )
Expand Down Expand Up @@ -362,13 +362,13 @@ value_to_impl(
if( !arr )
{
BOOST_JSON_FAIL(ec, error::not_array);
throw_system_error(ec, BOOST_CURRENT_LOCATION);
throw_system_error( ec );
}

T result;
ec = detail::try_reserve(result, arr->size(), reserve_implementation<T>());
if( ec.failed() )
throw_system_error(ec, BOOST_CURRENT_LOCATION);
throw_system_error( ec );

auto ins = detail::inserter(result, inserter_implementation<T>());
for( value const& val: *arr )
Expand Down Expand Up @@ -452,14 +452,14 @@ value_to_impl(
if( !arr )
{
BOOST_JSON_FAIL(ec, error::not_array);
throw_system_error(ec, BOOST_CURRENT_LOCATION);
throw_system_error( ec );
}

constexpr std::size_t N = std::tuple_size<remove_cvref<T>>::value;
if( N != arr->size() )
{
BOOST_JSON_FAIL(ec, error::size_mismatch);
throw_system_error(ec, BOOST_CURRENT_LOCATION);
throw_system_error( ec );
}

return make_tuple_like<T>(
Expand Down Expand Up @@ -627,7 +627,7 @@ value_to_impl(
{
auto res = tag_invoke(try_value_to_tag<T>(), jv);
if( res.has_error() )
throw_system_error(res.error(), BOOST_CURRENT_LOCATION);
throw_system_error( res.error() );
return std::move(*res);
}

Expand Down
6 changes: 2 additions & 4 deletions include/boost/json/impl/array.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,7 @@ array::
at(std::size_t pos) &
{
if(pos >= t_->size)
detail::throw_out_of_range(
BOOST_CURRENT_LOCATION);
detail::throw_out_of_range();
return (*t_)[pos];
}

Expand All @@ -224,8 +223,7 @@ array::
at(std::size_t pos) const&
{
if(pos >= t_->size)
detail::throw_out_of_range(
BOOST_CURRENT_LOCATION);
detail::throw_out_of_range();
return (*t_)[pos];
}

Expand Down
12 changes: 3 additions & 9 deletions include/boost/json/impl/array.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,7 @@ allocate(
{
BOOST_ASSERT(capacity > 0);
if(capacity > array::max_size())
detail::throw_length_error(
"array too large",
BOOST_CURRENT_LOCATION);
detail::throw_length_error( "array too large" );
auto p = reinterpret_cast<
table*>(sp->allocate(
sizeof(table) +
Expand Down Expand Up @@ -100,9 +98,7 @@ revert_insert(
return;
}
if(n_ > max_size() - arr_->size())
detail::throw_length_error(
"array too large",
BOOST_CURRENT_LOCATION);
detail::throw_length_error( "array too large" );
auto t = table::allocate(
arr_->growth(arr_->size() + n_),
arr_->sp_);
Expand Down Expand Up @@ -626,9 +622,7 @@ growth(
std::size_t new_size) const
{
if(new_size > max_size())
detail::throw_length_error(
"array too large",
BOOST_CURRENT_LOCATION);
detail::throw_length_error( "array too large" );
std::size_t const old = capacity();
if(old > max_size() - old / 2)
return new_size;
Expand Down
3 changes: 1 addition & 2 deletions include/boost/json/impl/null_resource.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,7 @@ protected:
std::size_t,
std::size_t) override
{
detail::throw_bad_alloc(
BOOST_CURRENT_LOCATION);
detail::throw_bad_alloc();
}

void
Expand Down
10 changes: 3 additions & 7 deletions include/boost/json/impl/object.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -342,8 +342,7 @@ at(string_view key) & ->
{
auto it = find(key);
if(it == end())
detail::throw_out_of_range(
BOOST_CURRENT_LOCATION);
detail::throw_out_of_range();
return it->value();
}

Expand All @@ -362,8 +361,7 @@ at(string_view key) const& ->
{
auto it = find(key);
if(it == end())
detail::throw_out_of_range(
BOOST_CURRENT_LOCATION);
detail::throw_out_of_range();
return it->value();
}

Expand Down Expand Up @@ -499,9 +497,7 @@ insert(
std::distance(first, last));
auto const n0 = size();
if(n > max_size() - n0)
detail::throw_length_error(
"object too large",
BOOST_CURRENT_LOCATION);
detail::throw_length_error( "object too large" );
reserve(n0 + n);
revert_insert r(*this);
while(first != last)
Expand Down
8 changes: 2 additions & 6 deletions include/boost/json/impl/object.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -448,9 +448,7 @@ insert(
{
auto const n0 = size();
if(init.size() > max_size() - n0)
detail::throw_length_error(
"object too large",
BOOST_CURRENT_LOCATION);
detail::throw_length_error( "object too large" );
reserve(n0 + init.size());
revert_insert r(*this);
if(t_->is_small())
Expand Down Expand Up @@ -786,9 +784,7 @@ growth(
std::size_t new_size) const
{
if(new_size > max_size())
detail::throw_length_error(
"object too large",
BOOST_CURRENT_LOCATION);
detail::throw_length_error( "object too large" );
std::size_t const old = capacity();
if(old > max_size() - old / 2)
return new_size;
Expand Down
6 changes: 2 additions & 4 deletions include/boost/json/impl/parse.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,7 @@ parse(
auto jv = parse(
s, ec, std::move(sp), opt);
if(ec)
detail::throw_system_error(ec,
BOOST_CURRENT_LOCATION);
detail::throw_system_error( ec );
return jv;
}

Expand Down Expand Up @@ -127,8 +126,7 @@ parse(
auto jv = parse(
is, ec, std::move(sp), opt);
if(ec)
detail::throw_system_error(ec,
BOOST_CURRENT_LOCATION);
detail::throw_system_error( ec );
return jv;
}

Expand Down
9 changes: 3 additions & 6 deletions include/boost/json/impl/parser.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,7 @@ write_some(
auto const n = write_some(
data, size, ec);
if(ec)
detail::throw_system_error(ec,
BOOST_CURRENT_LOCATION);
detail::throw_system_error( ec );
return n;
}

Expand Down Expand Up @@ -137,8 +136,7 @@ write(
auto const n = write(
data, size, ec);
if(ec)
detail::throw_system_error(ec,
BOOST_CURRENT_LOCATION);
detail::throw_system_error( ec );
return n;
}

Expand All @@ -156,8 +154,7 @@ release()
p_.fail(ec);
}
detail::throw_system_error(
p_.last_error(),
BOOST_CURRENT_LOCATION);
p_.last_error());
}
return p_.handler().st.release();
}
Expand Down
4 changes: 2 additions & 2 deletions include/boost/json/impl/pointer.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ value::at_pointer(string_view ptr) const&
error_code ec;
auto const found = find_pointer(ptr, ec);
if( !found )
detail::throw_system_error(ec, BOOST_CURRENT_LOCATION);
detail::throw_system_error( ec );
return *found;
}

Expand Down Expand Up @@ -491,7 +491,7 @@ value::set_at_pointer(
error_code ec;
value* result = set_at_pointer( sv, ref, ec, opts );
if( !result )
detail::throw_system_error(ec, BOOST_CURRENT_LOCATION);
detail::throw_system_error( ec );
return *result;
}

Expand Down
3 changes: 1 addition & 2 deletions include/boost/json/impl/static_resource.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ do_allocate(
auto p = detail::align(
align, n, p_, n_);
if(! p)
detail::throw_bad_alloc(
BOOST_CURRENT_LOCATION);
detail::throw_bad_alloc();
p_ = reinterpret_cast<char*>(p) + n;
n_ -= n;
return p;
Expand Down
Loading

0 comments on commit 607b21d

Please sign in to comment.