6 std::unordered_map<
ParameterType, std::vector<std::string>> source_names,
10 std::unordered_map<std::string, std::shared_ptr<Block>> sources;
11 std::vector<std::string> missing;
13 for (
const auto& [k, v] : source_names) {
15 for (
const auto& src_name : v) {
16 if (!ba->contains(src_name)) {
20 sources.emplace(src_name, ba->at(src_name));
24 if (!missing.empty()) {
25 std::ostringstream oss;
26 oss <<
"addDependentBlock '" << name <<
"': missing source blocks: ";
27 for (
size_t i = 0; i < missing.size(); ++i) {
if (i) oss <<
", "; oss << missing[i]; }
28 oss <<
". Available per model:\n";
29 for (
const auto& [k, _] : source_names) {
33 for (
auto& bn : ba->get_block_names()) {
if (!first) oss <<
", "; first =
false; oss << bn; }
36 throw std::invalid_argument(oss.str());
39 auto dependentBlock = std::make_shared<DependentBlock>(sources, recalculateFunc);
40 dependentBlock->blockname = name;
42 dependentBlock->init();
43 dependentBlock->update();
48 std::unordered_set<ParamId> source_pids,
51 std::unordered_map<ParamId, std::shared_ptr<Parameter>> sources;
52 std::vector<std::string> missing;
54 for (
const auto&
id : source_pids) {
57 auto blk = ba->at(
id.block);
60 auto p = blk->retrieve(
id.code);
61 sources.emplace(
id, p);
64 "::" +
id.block +
"::" +
id.code.to_string());
69 if (!missing.empty()) {
70 std::ostringstream oss;
71 oss <<
"addDependentParameter '"
73 <<
"': missing sources: ";
74 for (
size_t i = 0; i < missing.size(); ++i) {
if (i) oss <<
", "; oss << missing[i]; }
75 throw std::invalid_argument(oss.str());
79 if (!ba->contains(pid.
block)) {
80 ba->emplace(pid.
block, std::make_shared<Block>());
84 auto blk = ba->at(pid.
block);
86 if (blk->contains(pid.
code)) {
87 auto existing = blk->retrieve(pid.
code);
88 if (
auto dep = std::dynamic_pointer_cast<DependentParameter>(existing)) {
89 dep->rebind(std::move(sources), recalculateFunc);
96 auto dependentParam = std::make_shared<DependentParameter>(pid, std::move(sources), recalculateFunc);
97 dependentParam->init();
98 dependentParam->update();
99 blk->store(pid.
code, dependentParam);
106 auto accessor = params->blockAccessor;
108 if (!accessor->contains(name)) {
112 std::shared_ptr<Block> dep_block = accessor->at(name);
114 dep_block->destroy();
117 accessor->erase_block(name);
std::function< void(const BlockSrc &, std::shared_ptr< DependentBlock >)> DepUpdateFunc
Function type used to recompute a DependentBlock from its sources.
Provides static utilities to create, manage, and update dependent blocks and dependent parameters.
std::function< void(const ParamSrc &, std::shared_ptr< DependentParameter >)> DepParamUpdateFunc
Function signature used to recompute a DependentParameter.
Model-dependent parameter repository and initialization strategies.
static void update(const std::string &name, ParameterType src)
Manually triggers an update on a DependentBlock.
static void removeDependentBlock(const std::string &name, ParameterType src)
Removes a DependentBlock from a specified Parameters instance.
static void addDependentBlock(std::string name, std::unordered_map< ParameterType, std::vector< std::string > > source_names, ParameterType dest, DepUpdateFunc recalculateFunc)
Adds a DependentBlock with multiple sources coming from various Parameters instances.
static void addDependentParameter(ParamId pid, std::unordered_set< ParamId > source_pids, DepParamUpdateFunc recalculateFunc)
Adds a DependentParameter based on multiple source parameters.
static std::string str(const IdOf< ParamTypeTag > &id)
Returns the string representation associated with an identifier.
static std::shared_ptr< Parameters > GetInstance(ParameterType id=ParameterType::SM)
Returns the singleton-like repository for a given parameter type.
std::string to_string() const
Returns the canonical string representation of this LhaID.
Composite identifier for a single parameter.
std::optional< ParameterType > type
Optional high-level parameter category.
BlockName block
Name of the block where the parameter is stored.
LhaID code
Index or multi-index of the parameter inside the block.