Skip to content

Commit

Permalink
error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
molpopgen committed Sep 1, 2022
1 parent b2c4605 commit 2ec43ca
Showing 1 changed file with 30 additions and 21 deletions.
51 changes: 30 additions & 21 deletions lib/demes/forward_graph.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,51 +19,61 @@ namespace fwdpy11_core
{
auto code = demes_forward_graph_initialize_from_yaml(
yaml.c_str(), static_cast<double>(burnin), graph.get());
if (code < 0)
{
// NOTE: the rust lib currently doesn't
// set the status code. Rather, it returns
// nullptr if the model is not, in fact,
// in an error state.
std::int32_t status;
auto message
= demes_forward_graph_get_error_message(graph.get(), &status);
if (message == nullptr)
{
throw std::runtime_error(
"graph in error state but message is nullptr");
}
throw std::invalid_argument(message);
}
handle_error_code(code);
}

void handle_error_code(std::int32_t code) const;
void update_model_state_to_time(std::uint32_t);
void initialize_time_iteration();
const double *iterate();
bool offspring_demes_exist() const;
void update_internal_state(double time);
};

void
ForwardDemesGraph::forward_graph_implementation::handle_error_code(
std::int32_t code) const
{
if (code < 0)
{
// NOTE: the rust lib currently doesn't
// set the status code. Rather, it returns
// nullptr if the model is not, in fact,
// in an error state.
std::int32_t status;
auto message
= demes_forward_graph_get_error_message(graph.get(), &status);
if (message == nullptr)
{
throw std::runtime_error(
"graph in error state but message is nullptr");
}
throw std::invalid_argument(message);
}
}

void
ForwardDemesGraph::forward_graph_implementation::update_model_state_to_time(
std::uint32_t time)
{
auto code
= demes_forward_graph_update_state(static_cast<double>(time), graph.get());
handle_error_code(code);
}

void
ForwardDemesGraph::forward_graph_implementation::initialize_time_iteration()
{
auto code = demes_forward_graph_initialize_time_iteration(graph.get());
handle_error_code(code);
}

const double *
ForwardDemesGraph::forward_graph_implementation::iterate()
{
std::int32_t code;
auto rv = demes_forward_graph_iterate_time(graph.get(), &code);
assert(code == 0);
handle_error_code(code);
return rv;
}

Expand All @@ -72,17 +82,17 @@ namespace fwdpy11_core
{
std::int32_t code;
auto rv = demes_forward_graph_any_extant_offspring_demes(graph.get(), &code);
assert(code == 0);
handle_error_code(code);
return rv;
}

void
ForwardDemesGraph::forward_graph_implementation::update_internal_state(double time)
{
std::int32_t code = demes_forward_graph_update_state(time, graph.get());
handle_error_code(code);
}


ForwardDemesGraph::~ForwardDemesGraph()
{
}
Expand All @@ -92,13 +102,12 @@ namespace fwdpy11_core
{
}


std::uint32_t
ForwardDemesGraph::model_end_time() const
{
std::int32_t code;
auto end_time = demes_forward_graph_model_end_time(&code, pimpl->graph.get());
assert(code == 0);
pimpl->handle_error_code(code);
return static_cast<std::uint32_t>(end_time);
}

Expand Down

0 comments on commit 2ec43ca

Please sign in to comment.