@@ -46,25 +46,51 @@ class DisableCachingRAII {
4646 bool _oldState;
4747};
4848
49- // / Struct to temporarily change the operation mode of a RooAbsArg until it
50- // / goes out of scope.
49+ // / Scope guard that temporarily changes the operation mode of one or more
50+ // / RooAbsArg instances. Each call to change() records the arg's current
51+ // / operMode before flipping it to the requested mode (non-recursively, i.e.
52+ // / value clients are not touched). Destruction (or an explicit clear())
53+ // / restores every recorded mode in LIFO order.
54+ // /
55+ // / The class is movable but not copyable, so it can be returned from
56+ // / functions that build up a batch of changes to hand to the caller.
5157class ChangeOperModeRAII {
5258public:
53- ChangeOperModeRAII (RooAbsArg *arg, RooAbsArg::OperMode opMode) : _arg{arg}, _oldOpMode(arg->operMode ())
59+ ChangeOperModeRAII () = default ;
60+
61+ // / Convenience ctor: behaves like a scope guard for a single arg.
62+ ChangeOperModeRAII (RooAbsArg *arg, RooAbsArg::OperMode opMode) { change (arg, opMode); }
63+
64+ ~ChangeOperModeRAII () { clear (); }
65+
66+ ChangeOperModeRAII (ChangeOperModeRAII &&) = default ;
67+ ChangeOperModeRAII &operator =(ChangeOperModeRAII &&) = default ;
68+ ChangeOperModeRAII (ChangeOperModeRAII const &) = delete ;
69+ ChangeOperModeRAII &operator =(ChangeOperModeRAII const &) = delete ;
70+
71+ // / Record arg's current operMode and flip it to opMode. If the current
72+ // / mode already equals opMode, this is a no-op (nothing to restore).
73+ void change (RooAbsArg *arg, RooAbsArg::OperMode opMode)
5474 {
55- arg->setOperMode (opMode, /* recurse=*/ false );
75+ if (opMode == arg->operMode ())
76+ return ;
77+ _entries.emplace_back (arg, arg->operMode ());
78+ arg->setOperMode (opMode, /* recurseADirty=*/ false );
5679 }
5780
58- ChangeOperModeRAII (ChangeOperModeRAII const &other) = delete ;
59- ChangeOperModeRAII &operator =(ChangeOperModeRAII const &other) = delete ;
60- ChangeOperModeRAII (ChangeOperModeRAII &&other) = delete ;
61- ChangeOperModeRAII &operator =(ChangeOperModeRAII &&other) = delete ;
81+ // / Restore every recorded change right away, emptying this guard.
82+ void clear ()
83+ {
84+ for (auto it = _entries.rbegin (); it != _entries.rend (); ++it) {
85+ it->first ->setOperMode (it->second , /* recurseADirty=*/ false );
86+ }
87+ _entries.clear ();
88+ }
6289
63- ~ChangeOperModeRAII () { _arg-> setOperMode (_oldOpMode, /* recurse= */ false ); }
90+ bool empty () const { return _entries. empty ( ); }
6491
6592private:
66- RooAbsArg *_arg = nullptr ;
67- RooAbsArg::OperMode _oldOpMode;
93+ std::vector<std::pair<RooAbsArg *, RooAbsArg::OperMode>> _entries;
6894};
6995
7096namespace RooHelpers {
0 commit comments