Skip to content

Commit

Permalink
Turning assertions into exceptions
Browse files Browse the repository at this point in the history
- flyby: refactor future_serialization_save to reduce code duplication
  • Loading branch information
hkaiser committed Jun 2, 2017
1 parent 9df93c3 commit fd5a37b
Showing 1 changed file with 16 additions and 54 deletions.
70 changes: 16 additions & 54 deletions hpx/lcos/future.hpp
Expand Up @@ -89,7 +89,9 @@ namespace hpx { namespace lcos { namespace detail
} else if (state == future_state::invalid) {
f = Future();
} else {
HPX_ASSERT(false);
HPX_THROW_EXCEPTION(invalid_status,
"serialize_future_load",
"attempting to deserialize a future with an invalid state");
}
}

Expand Down Expand Up @@ -120,7 +122,9 @@ namespace hpx { namespace lcos { namespace detail
} else if (state == future_state::invalid) {
f = Future();
} else {
HPX_ASSERT(false);
HPX_THROW_EXCEPTION(invalid_status,
"serialize_future_load",
"attempting to deserialize a future with an invalid state");
}
}

Expand All @@ -132,9 +136,9 @@ namespace hpx { namespace lcos { namespace detail
typedef typename hpx::traits::future_traits<Future>::result_type value_type;

int state = future_state::invalid;
if(ar.is_preprocessing())
if (f.is_valid() && !f.is_ready())
{
if(!f.is_ready())
if (ar.is_preprocessing())
{
typename hpx::traits::detail::shared_state_ptr_for<Future>::type state
= hpx::traits::future_access<Future>::get_shared_state(f);
Expand All @@ -145,35 +149,13 @@ namespace hpx { namespace lcos { namespace detail
}
else
{
if(f.is_ready())
{
if (f.has_value())
{
value_type const & value =
*hpx::traits::future_access<Future>::
get_shared_state(f)->get_result();
state = future_state::has_value;
ar << state << value; //-V128
} else if (f.has_exception()) {
state = future_state::has_exception;
boost::exception_ptr exception = f.get_exception_ptr();
ar << state << exception;
} else {
state = future_state::invalid;
ar << state;
}
}
HPX_THROW_EXCEPTION(invalid_status,
"serialize_future_save",
"future must be ready in order for it to be serialized");
}
return;
}

#if defined(HPX_DEBUG)
if (f.valid())
{
HPX_ASSERT(f.is_ready());
}
#endif

if (f.has_value())
{
state = future_state::has_value;
Expand All @@ -197,9 +179,9 @@ namespace hpx { namespace lcos { namespace detail
>::type serialize_future_save(Archive& ar, Future const& f) //-V659
{
int state = future_state::invalid;
if(ar.is_preprocessing())
if (f.is_valid() && !f.is_ready())
{
if(!f.is_ready())
if (ar.is_preprocessing())
{
typename
hpx::traits::detail::shared_state_ptr_for<Future>::type state
Expand All @@ -211,33 +193,13 @@ namespace hpx { namespace lcos { namespace detail
}
else
{
if (f.has_value())
{
state = future_state::has_value;
ar << state;
}
else if (f.has_exception())
{
state = future_state::has_exception;
boost::exception_ptr exception = f.get_exception_ptr();
ar << state << exception;
}
else
{
state = future_state::invalid;
ar << state;
}
HPX_THROW_EXCEPTION(invalid_status,
"serialize_future_save",
"future must be ready in order for it to be serialized");
}
return;
}

#if defined(HPX_DEBUG)
if (f.valid())
{
HPX_ASSERT(f.is_ready());
}
#endif

if (f.has_value())
{
state = future_state::has_value;
Expand Down

0 comments on commit fd5a37b

Please sign in to comment.