6 std::unordered_map<
ParamId, std::shared_ptr<Parameter>> sources_in,
9 sources_raw(sources_in),
11 recalculateLambda(recalculateFunc),
12 saved_sources_raw(
std::move(sources_in)),
13 saved_recalculateLambda(recalculateFunc),
20 return (*sources).raw().contains(pid);
25 if (
auto me = self.lock()) {
26 for (
auto& [_, param] : (*sources).raw()) {
27 param->removeObserver(me);
33 auto dependents = std::exchange(
observers, {});
38 host->erase_local(this->
id.code);
41 for (
auto& obs : dependents) {
42 if (obs) obs->clear_below();
47 auto me_base = shared_from_this();
48 auto me_dep = std::static_pointer_cast<DependentParameter>(me_base);
51 for (
auto& [_, src] : (*sources).raw()) {
52 src->addObserver(me_base);
62 update_at_unfreeze =
true;
84 for (
const auto& [pid, src] : sources->raw()) {
85 if (!src)
LOG_ERROR(
"Error",
"Null source param for", pid.code);
89 if (!recalculateLambda) {
90 LOG_ERROR(
"Error",
"DependentParameter has no recalculateLambda");
94 auto me_dep = std::static_pointer_cast<DependentParameter>(shared_from_this());
95 recalculateLambda(*sources, me_dep);
104 this->frozen =
false;
105 if (update_at_unfreeze) {
107 update_at_unfreeze =
false;
112 std::unordered_map<
ParamId, std::shared_ptr<Parameter>> new_sources,
117 saved_sources_raw = new_sources;
118 saved_recalculateLambda = new_lambda;
120 sources_raw = std::move(new_sources);
121 sources = std::make_unique<ParamSrc>(
125 recalculateLambda = std::move(new_lambda);
127 if (
auto me = self.lock()) {
128 for (
auto& [_, src] : sources->raw()) {
129 if (src) src->addObserver(me);
134 dependency_detached =
false;
140 if (dependency_detached)
return;
146 sources = std::make_unique<ParamSrc>(
151 recalculateLambda = {};
153 dependency_detached =
true;
160 if (!dependency_detached)
return;
162 sources_raw = saved_sources_raw;
163 sources = std::make_unique<ParamSrc>(
167 recalculateLambda = saved_recalculateLambda;
169 if (
auto me = self.lock()) {
170 for (
auto& [_, src] : sources->raw()) {
171 if (src) src->addObserver(me);
176 dependency_detached =
false;
181 LOG_DEBUG(
"Destruct DependentParameter at", self.lock().get());
182 if (
auto me = self.lock()) {
183 for (
auto& [_, src] : (*sources).raw()) {
184 src->removeObserver(me);
Defines parameters whose values are lazily computed from other parameters.
std::function< void(const ParamSrc &, std::shared_ptr< DependentParameter >)> DepParamUpdateFunc
Function signature used to recompute a DependentParameter.
#define LOG_ERROR(type,...)
Macro for logging error messages and terminating the application.
#define LOG_DEBUG(...)
Macro for logging debug messages.
@ FIXED
The parameter remains fixed at its expected value.
Parameter whose value is computed from other parameters and cached lazily.
void rebind(std::unordered_map< ParamId, std::shared_ptr< Parameter > > new_sources, DepParamUpdateFunc new_lambda)
Replaces the dependency set and recomputation rule at runtime.
void clear_above() override
Detaches this parameter from all upstream source parameters.
scalar_t get_val() const override
Returns the current cached value, recomputing it on demand if needed.
void unfreeze() override
Unfreezes the dependent parameter.
void update() override
Marks the parameter as dirty and propagates the update downstream.
void freeze() override
Freezes the dependent parameter.
DependentParameter(ParamId pid, std::unordered_map< ParamId, std::shared_ptr< Parameter > > sources, DepParamUpdateFunc recalculateFunc)
Constructs a dependent parameter from its id, sources, and recomputation rule.
void clear_below() override
Clears this parameter and all downstream dependent parameters.
void ensure_up_to_date()
Recomputes the parameter if its cached value is stale.
~DependentParameter()
Destructor.
bool dependsOn(const ParamId &pid)
Checks whether this parameter depends on a given source parameter.
void reattach()
Restores previously detached dependencies and recomputation rule.
void mark_dirty()
Marks the cached value as dirty.
void init()
Initializes observer registration on all source parameters.
void detach()
Temporarily detaches all dependencies while keeping the current cached value.
static std::string str(const IdOf< ParamTypeTag > &id)
Returns the string representation associated with an identifier.
Lightweight view over a set of source parameters keyed by ParamId.
Represents a single parameter with value, uncertainties, and dependency links.
ParameterMode mode
Current operating mode.
std::vector< std::shared_ptr< Parameter > > observers
Observers notified when this parameter changes.
scalar_t expected
Expected value of the parameter.
scalar_t shift
Additive shift applied when the parameter is SHIFTABLE.
void notifyObservers()
Notifies downstream parameter observers and then the owner block.
std::weak_ptr< Block > owner_block
Owning Block (if any) that this parameter belongs to.
Hash specialization for SymbolId<Tag>.
Composite identifier for a single parameter.