From 67e846c4d53e8ee3d350421a68674415ce349c0e Mon Sep 17 00:00:00 2001 From: Dan Short Date: Fri, 31 Jul 2020 11:48:00 +0100 Subject: [PATCH] Load new serializable form of source function Updates the SourceDistribution to optionally look for a function template accepting char* as the second argument. If serialization is defined as an attribute on the source element provided in the settings.xml then the new form with serialization will be used. Otherwise the existing form to load the custom source as-is from the library will be used. --- src/source.cpp | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/src/source.cpp b/src/source.cpp index 3f31541db..86dac6b3f 100644 --- a/src/source.cpp +++ b/src/source.cpp @@ -46,6 +46,9 @@ namespace { using sample_t = Particle::Bank (*)(uint64_t* seed); sample_t custom_source_function; +std::string serialization; +using serialized_sample_t = Particle::Bank (*)(uint64_t* seed, const char* serialization); +serialized_sample_t custom_serialized_source_function; void* custom_source_library; } @@ -94,6 +97,10 @@ SourceDistribution::SourceDistribution(pugi::xml_node node) fatal_error(fmt::format("Source library '{}' does not exist.", settings::path_source_library)); } + + if (check_for_node(node, "serialization")) { + serialization = get_node_value(node, "serialization", false, true); + } } else { // Spatial distribution for external source @@ -367,9 +374,15 @@ void load_custom_source_library() // reset errors dlerror(); - // get the function from the library - using sample_t = Particle::Bank (*)(uint64_t* seed); - custom_source_function = reinterpret_cast(dlsym(custom_source_library, "sample_source")); + if (serialization.empty()) { + // get the function from the library + using sample_t = Particle::Bank (*)(uint64_t* seed); + custom_source_function = reinterpret_cast(dlsym(custom_source_library, "sample_source")); + } else { + // get the function from the library using the provided serialization + using sample_t = Particle::Bank (*)(uint64_t* seed, const char* serialization); + custom_serialized_source_function = reinterpret_cast(dlsym(custom_source_library, "sample_source")); + } // check for any dlsym errors auto dlsym_error = dlerror(); @@ -396,7 +409,11 @@ void close_custom_source_library() Particle::Bank sample_custom_source_library(uint64_t* seed) { - return custom_source_function(seed); + if (serialization.empty()) { + return custom_source_function(seed); + } else { + return custom_serialized_source_function(seed, serialization.c_str()); + } } void fill_source_bank_custom_source()