nmodl icon indicating copy to clipboard operation
nmodl copied to clipboard

Introduce a RAII state-guard.

Open 1uc opened this issue 1 year ago • 1 comments

While printing code we have the following pattern:

void print_net_receive() {
  printing_net_receive = true;
  // ...
  printing_net_receive = false;
}

The problem is that:

  1. We need to be sure that print_net_receive is not re-entrant otherwise printing_net_receive will be toggled back off. This is easy in this particular case, but can be non-obvious in other cases.
  2. We must not return, because otherwise printing_net_receive will stay true indefinitely.

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; }
};

1uc avatar Sep 18 '24 09:09 1uc

Maybe we can check too for reentrant and throw?

alkino avatar Sep 18 '24 10:09 alkino