[RFA 02/22] Use RAII to save and restore scalars
Trevor Saunders
tbsaunde@tbsaunde.org
Tue Sep 27 10:24:00 GMT 2016
On Mon, Sep 26, 2016 at 10:08:30PM -0600, Tom Tromey wrote:
> This patch replaces many (but not all) uses of
> make_cleanup_restore_integer with a simple RAII-based template class.
> It also removes the similar restore_execution_direction cleanup in
> favor of this new class. Subsequent patches will replace other
> similar cleanups with this class.
>
> I chose the name "scoped_restore" for this class. I considered that
> perhaps "auto_restore" might be clearer. Or maybe something else;
> it's simple enough to change.
Its worth noting dmalcolm wants to add something similar to gcc for his
rtl front end work.
> I had hoped that template parameter deduction would work here, but it
> did not, and so the patch uses explicit template parameters
> everywhere.
yeah, you can't deduce template args from a constructor in C++
> + // Create a new scoped_restore object that saves the current value
> + // of *VAR, and then sets *VAR to VALUE. *VAR will be restored when
> + // this scoped_restore object is destroyed.
> + scoped_restore (T *var, T value)
> + : saved_var (var),
> + saved_value (*var)
> + {
> + *var = value;
I wonder if it isn't clearer to create the scoped_restore and then set
the variable yourself.
> + // The saved variable.
> + T *saved_var;
> +
> + // The saved value.
> + T saved_value;
I think you could use const T, but I doubt it matters, these objects
really shouldn't have there address taken so the compiler should see
everything about them.
Trev
More information about the Gdb-patches
mailing list