nmodl
nmodl copied to clipboard
Introduce a RAII state-guard.
While printing code we have the following pattern:
void print_net_receive() {
printing_net_receive = true;
// ...
printing_net_receive = false;
}
The problem is that:
- We need to be sure that
print_net_receiveis not re-entrant otherwiseprinting_net_receivewill be toggled back off. This is easy in this particular case, but can be non-obvious in other cases. - We must not
return, because otherwiseprinting_net_receivewill staytrueindefinitely.
The solution is to use a RAII guard:
struct ToggleGuard {
bool * flag;
bool previous_value;
ToggleGuard(bool* flag, bool new_value) : flag(flag), old_value(*flag) {}
~ToggleGuard() { *flag = old_value; }
};
Maybe we can check too for reentrant and throw?