Skip to content

Commit

Permalink
Add C++ ForwardGraph::demes_graph to get a string representation of t…
Browse files Browse the repository at this point in the history
…he resolved graph (#1297)
  • Loading branch information
molpopgen authored Apr 16, 2024
1 parent c71253b commit b20730a
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 1 deletion.
1 change: 1 addition & 0 deletions cpptests/test_forward_demes_graph.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ BOOST_FIXTURE_TEST_CASE(test_zero_length_single_deme_model, SingleDemeModel)
pop.generation += 1;
}
BOOST_REQUIRE_EQUAL(pop.generation, 0);
auto graph = g.demes_graph();
}

BOOST_FIXTURE_TEST_CASE(single_deme_model_with_burn_in, SingleDemeModel)
Expand Down
1 change: 1 addition & 0 deletions lib/core/demes/forward_graph.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,6 @@ namespace fwdpy11_core
// gets a fn to handle this
std::uint32_t sum_deme_sizes_at_time_zero();
std::vector<std::uint32_t> parental_deme_sizes_at_time_zero() const;
std::string demes_graph() const;
};
}
15 changes: 15 additions & 0 deletions lib/demes/forward_graph.cc
Original file line number Diff line number Diff line change
Expand Up @@ -312,4 +312,19 @@ namespace fwdpy11_core

return rv;
}

std::string
ForwardDemesGraph::demes_graph() const
{
std::int32_t status;
auto temp = demes_forward_graph_get_demes_graph(&status, pimpl->graph.get());
if (status != 0)
{
free(const_cast<char *>(temp));
}
pimpl->handle_error_code(status);
auto rv = std::string(temp);
free(const_cast<char *>(temp));
return rv;
}
}
10 changes: 9 additions & 1 deletion rust/fp11rust/src/demes_capi_bridge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,14 @@ pub unsafe extern "C" fn demes_forward_graph_model_end_time(
forward_graph_model_end_time(status, graph)
}

#[no_mangle]
pub unsafe extern "C" fn demes_forward_graph_get_demes_graph(
status: *mut i32,
graph: *const OpaqueForwardGraph,
) -> *const c_char {
forward_graph_get_demes_graph(graph, status)
}

// Below are functions defined only for fwdpy11.
// These make use of demes_forward_capi.

Expand Down Expand Up @@ -175,6 +183,6 @@ pub unsafe extern "C" fn demes_forward_graph_sum_sizes_at_time_zero(
return f64::NAN;
}
let size_slice = std::slice::from_raw_parts(ptr, num_demes as usize);
assert_eq!(size_slice.len(), num_demes as usize);
assert_eq!(size_slice.len(), num_demes as usize);
size_slice.iter().sum()
}

0 comments on commit b20730a

Please sign in to comment.