RESOLVED FIXED 73371
Add a way to revert a variable to its previous value after leaving a scope.
https://bugs.webkit.org/show_bug.cgi?id=73371
Summary Add a way to revert a variable to its previous value after leaving a scope.
David Levin
Reported 2011-11-29 15:46:02 PST
Add a way to revert a variable to its previous value after leaving a scope.
Attachments
Patch (18.48 KB, patch)
2011-11-29 15:56 PST, David Levin
no flags
Patch (18.96 KB, patch)
2011-11-29 20:26 PST, David Levin
no flags
David Levin
Comment 1 2011-11-29 15:56:00 PST
Adam Barth
Comment 2 2011-11-29 16:06:10 PST
Comment on attachment 117063 [details] Patch View in context: https://bugs.webkit.org/attachment.cgi?id=117063&action=review > Source/JavaScriptCore/wtf/AutoRevert.h:39 > +// shorter lifetime than its scoped_variable, to prevent invalid memory writes scoped_variable => scopedVariable > Tools/TestWebKitAPI/Tests/WTF/AutoRevert.cpp:36 > + AutoRevert<bool> autoRevert(&originallyFalse, true); C++ isn't smart enough to imply the template value? I thought it could figure it out from the constructor.
David Levin
Comment 3 2011-11-29 16:16:13 PST
I missed adding this to header file to non-xcode build systems. (I'll fix that before landing.)
Anders Carlsson
Comment 4 2011-11-29 16:17:32 PST
Comment on attachment 117063 [details] Patch View in context: https://bugs.webkit.org/attachment.cgi?id=117063&action=review > Source/JavaScriptCore/wtf/AutoRevert.h:46 > + AutoRevert(T* scopedVariable, T newValue) Is there a reason why scopedVariable isn't a reference? >> Tools/TestWebKitAPI/Tests/WTF/AutoRevert.cpp:36 >> + AutoRevert<bool> autoRevert(&originallyFalse, true); > > C++ isn't smart enough to imply the template value? I thought it could figure it out from the constructor. Nope, that's not how class templates work - C++ first parses the variable declaration and then the constructor arguments.
David Levin
Comment 5 2011-11-29 16:19:02 PST
(In reply to comment #4) > (From update of attachment 117063 [details]) > View in context: https://bugs.webkit.org/attachment.cgi?id=117063&action=review > > > Source/JavaScriptCore/wtf/AutoRevert.h:46 > > + AutoRevert(T* scopedVariable, T newValue) > > Is there a reason why scopedVariable isn't a reference? I'll change that. It will still be stored internally in the calss as a pointer to allow for clearing it out when revertNow is called.
Anders Carlsson
Comment 6 2011-11-29 16:34:04 PST
Comment on attachment 117063 [details] Patch View in context: https://bugs.webkit.org/attachment.cgi?id=117063&action=review >>> Source/JavaScriptCore/wtf/AutoRevert.h:46 >>> + AutoRevert(T* scopedVariable, T newValue) >> >> Is there a reason why scopedVariable isn't a reference? > > I'll change that. It will still be stored internally in the calss as a pointer to allow for clearing it out when revertNow is called. Great! > Source/JavaScriptCore/wtf/AutoRevert.h:64 > + *m_scopedVariable = m_originalValue; > + m_scopedVariable = 0; You could also assert that m_scopedVariable is not null here.
David Levin
Comment 7 2011-11-29 20:26:59 PST
David Levin
Comment 8 2011-11-29 20:32:49 PST
Comment on attachment 117105 [details] Patch Ok, speak up if anyone objects. This is slightly different from before. (Different names, removed a method, changed the variable from being a T* to a T&.) Since I've received and incorporated (valuable) input from Adam, Alexey, Anders, and Dmitry Lomov, I think this bikeshed has been well painted :). I'll cq+ it after it passes enough ews bots.
David Levin
Comment 9 2011-11-29 21:36:57 PST
Darin Adler
Comment 10 2011-11-30 12:36:39 PST
I think TemporaryChange would be a good name; classes normally want nouns as their names, not verbs.
David Levin
Comment 11 2011-11-30 12:37:39 PST
(In reply to comment #10) > I think TemporaryChange would be a good name; classes normally want nouns as their names, not verbs. Makes sense. I'll do a follow up patch... :)
Note You need to log in before you can comment on or make changes to this bug.