From b20730a2bb5561a709d959541ba445af2131cd3a Mon Sep 17 00:00:00 2001 From: "Kevin R. Thornton" Date: Tue, 16 Apr 2024 12:20:17 -0700 Subject: [PATCH] Add C++ ForwardGraph::demes_graph to get a string representation of the resolved graph (#1297) --- cpptests/test_forward_demes_graph.cc | 1 + lib/core/demes/forward_graph.hpp | 1 + lib/demes/forward_graph.cc | 15 +++++++++++++++ rust/fp11rust/src/demes_capi_bridge.rs | 10 +++++++++- 4 files changed, 26 insertions(+), 1 deletion(-) diff --git a/cpptests/test_forward_demes_graph.cc b/cpptests/test_forward_demes_graph.cc index 47120e168..3a421387d 100644 --- a/cpptests/test_forward_demes_graph.cc +++ b/cpptests/test_forward_demes_graph.cc @@ -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) diff --git a/lib/core/demes/forward_graph.hpp b/lib/core/demes/forward_graph.hpp index 76e4b4f9f..396cdc547 100644 --- a/lib/core/demes/forward_graph.hpp +++ b/lib/core/demes/forward_graph.hpp @@ -57,5 +57,6 @@ namespace fwdpy11_core // gets a fn to handle this std::uint32_t sum_deme_sizes_at_time_zero(); std::vector parental_deme_sizes_at_time_zero() const; + std::string demes_graph() const; }; } diff --git a/lib/demes/forward_graph.cc b/lib/demes/forward_graph.cc index 2528a431f..618f401e8 100644 --- a/lib/demes/forward_graph.cc +++ b/lib/demes/forward_graph.cc @@ -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(temp)); + } + pimpl->handle_error_code(status); + auto rv = std::string(temp); + free(const_cast(temp)); + return rv; + } } diff --git a/rust/fp11rust/src/demes_capi_bridge.rs b/rust/fp11rust/src/demes_capi_bridge.rs index 978a95c83..316f3f9b4 100644 --- a/rust/fp11rust/src/demes_capi_bridge.rs +++ b/rust/fp11rust/src/demes_capi_bridge.rs @@ -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. @@ -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() }