src/TransformsV2/extension/ParameterBinding.hpp
src/TransformsV2/extension/ParameterBinding.hpp
Utilities for applying value store bindings to transform parameters. More…
Namespaces
| Name |
|---|
| Neuralyzer |
| Neuralyzer::Transforms |
| Neuralyzer::Transforms::V2 |
Classes
| Name | |
|---|---|
| class | Neuralyzer::Transforms::V2::RegisterBindingApplicator RAII helper for registering binding applicators at static initialization. |
Detailed Description
Utilities for applying value store bindings to transform parameters.
This file provides the binding mechanism that connects PipelineValueStore values to transform parameter fields. It uses reflect-cpp’s JSON serialization to achieve type-safe binding without requiring compile-time knowledge of all parameter types.
Binding Mechanism
- Serialize parameters to JSON
- For each binding (field_name -> store_key), replace the JSON value
- Deserialize back to the parameter type
This approach leverages reflect-cpp’s existing serialization infrastructure and handles type conversions automatically via JSON.
Example
struct ZScoreParams {
float mean = 0.0f;
float std_dev = 1.0f;
bool clamp_outliers = false;
};
PipelineValueStore store;
store.set("computed_mean", 0.5f);
store.set("computed_std", 0.1f);
ZScoreParams base_params{};
std::map<std::string, std::string> bindings = {
{"mean", "computed_mean"}, // field_name -> store_key
{"std_dev", "computed_std"}
};
auto bound_params = applyBindings(base_params, bindings, store);
// bound_params.mean == 0.5f
// bound_params.std_dev == 0.1fType-Erased Binding
For runtime pipeline execution where parameter types aren’t known at compile time, use the registry-based applyBindingsErased() function. Transform registration automatically registers a binding applicator for each parameter type.
PipelineValueStore.hpp for the value storage
PipelineStep.hpp for integration with pipeline execution
Source code
#ifndef NEURALYZER_V2_PARAMETER_BINDING_HPP
#define NEURALYZER_V2_PARAMETER_BINDING_HPP
#include "PipelineValueStore/PipelineValueStore.hpp" // For PipelineValueStore
#include <rfl.hpp>
#include <rfl/DefaultIfMissing.hpp>
#include <rfl/json.hpp>
#include <nlohmann/json.hpp>
#include <any> // std::any
#include <functional> // std::function
#include <map> // std::map
#include <optional> // std::optional
#include <stdexcept> // std::runtime_error
#include <string> // std::string
#include <typeindex> // std::type_index
#include <unordered_map> // std::unordered_map
namespace Neuralyzer::Transforms::V2 {
// ============================================================================
// Binding Application (Templated - for compile-time known types)
// ============================================================================
template<typename Params>
Params applyBindings(
Params const& base_params,
std::map<std::string, std::string> const& bindings,
PipelineValueStore const& store)
{
if (bindings.empty()) {
return base_params;
}
// Serialize to JSON
std::string json_str = rfl::json::write(base_params);
nlohmann::json json_obj = nlohmann::json::parse(json_str);
// Apply bindings
for (auto const& [field_name, store_key] : bindings) {
auto value_json = store.getJson(store_key);
if (!value_json) {
throw std::runtime_error(
"Binding failed: store key '" + store_key +
"' not found for field '" + field_name + "'");
}
// Parse the value JSON and insert into the object
// This handles type conversion automatically
nlohmann::json value = nlohmann::json::parse(*value_json);
json_obj[field_name] = value;
}
// Deserialize back to Params
std::string modified_json = json_obj.dump();
auto result = rfl::json::read<Params, rfl::DefaultIfMissing>(modified_json);
if (!result) {
throw std::runtime_error(
"Failed to deserialize parameters after binding: " +
std::string(result.error().value().what()));
}
return result.value();
}
template<typename Params>
std::optional<Params> tryApplyBindings(
Params const& base_params,
std::map<std::string, std::string> const& bindings,
PipelineValueStore const& store) noexcept
{
try {
return applyBindings(base_params, bindings, store);
} catch (...) {
return std::nullopt;
}
}
// ============================================================================
// Binding Applicator Registry (for type-erased runtime binding)
// ============================================================================
using BindingApplicatorFn = std::function<std::any(
std::any const& base_params,
std::map<std::string, std::string> const& bindings,
PipelineValueStore const& store)>;
std::unordered_map<std::type_index, BindingApplicatorFn> & getBindingApplicatorRegistry();
template<typename Params>
void registerBindingApplicatorFor() {
auto& registry = getBindingApplicatorRegistry();
auto type_idx = std::type_index(typeid(Params));
if (registry.find(type_idx) == registry.end()) {
registry[type_idx] = [](
std::any const& base_params,
std::map<std::string, std::string> const& bindings,
PipelineValueStore const& store) -> std::any
{
auto const& typed_params = std::any_cast<Params const&>(base_params);
return std::any{applyBindings(typed_params, bindings, store)};
};
}
}
template<typename Params>
class RegisterBindingApplicator {
public:
RegisterBindingApplicator() {
registerBindingApplicatorFor<Params>();
}
};
// ============================================================================
// Type-Erased Binding Application
// ============================================================================
std::any applyBindingsErased(
std::type_index params_type,
std::any const& base_params,
std::map<std::string, std::string> const& bindings,
PipelineValueStore const& store);
std::any tryApplyBindingsErased(
std::type_index params_type,
std::any const& base_params,
std::map<std::string, std::string> const& bindings,
PipelineValueStore const& store) noexcept;
bool hasBindingApplicator(std::type_index params_type);
} // namespace Neuralyzer::Transforms::V2
#endif // NEURALYZER_V2_PARAMETER_BINDING_HPPUpdated on 2026-11-13 at 20:25:44 +0000