Skip to content

Commit

Permalink
Add a way to revert a variable to its previous value after leaving a …
Browse files Browse the repository at this point in the history
…scope.

https://bugs.webkit.org/show_bug.cgi?id=73371

Reviewed by Adam Barth.

Source/JavaScriptCore:

In case anyone from Chromium sees this, it is nearly identical to AutoReset
but if the same name were used, it causes unnecessary ambiguity.

* JavaScriptCore.xcodeproj/project.pbxproj:
* wtf/TemporarilyChange.h: Added.
(WTF::TemporarilyChange::TemporarilyChange):
(WTF::TemporarilyChange::~TemporarilyChange):

Source/JavaScriptGlue:

* ForwardingHeaders/wtf/TemporarilyChange.h: Added.

Source/WebCore:

* ForwardingHeaders/wtf/TemporarilyChange.h: Added.

Source/WebKit/mac:

* ForwardingHeaders/wtf/TemporarilyChange.h: Added.

Tools:

* DumpRenderTree/ForwardingHeaders/wtf/TemporarilyChange.h: Added.
* TestWebKitAPI/TestWebKitAPI.gypi: Added test file to the build.
* TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj: Ditto.
* TestWebKitAPI/win/TestWebKitAPI.vcproj: Ditto.
* TestWebKitAPI/Tests/WTF/TemporarilyChange.cpp: Added.
(TestWebKitAPI::TEST): Added a test for TemporarilyChange.

git-svn-id: http://svn.webkit.org/repository/webkit/trunk@101446 268f45cc-cd09-0410-ab3c-d52691b4dbfc
  • Loading branch information
David Levin committed Nov 30, 2011
1 parent 075a228 commit f4ec26d
Show file tree
Hide file tree
Showing 17 changed files with 196 additions and 0 deletions.
15 changes: 15 additions & 0 deletions Source/JavaScriptCore/ChangeLog
@@ -1,3 +1,18 @@
2011-11-29 David Levin <levin@chromium.org>

Add a way to revert a variable to its previous value after leaving a scope.
https://bugs.webkit.org/show_bug.cgi?id=73371

Reviewed by Adam Barth.

In case anyone from Chromium sees this, it is nearly identical to AutoReset
but if the same name were used, it causes unnecessary ambiguity.

* JavaScriptCore.xcodeproj/project.pbxproj:
* wtf/TemporarilyChange.h: Added.
(WTF::TemporarilyChange::TemporarilyChange):
(WTF::TemporarilyChange::~TemporarilyChange):

2011-11-29 Sam Weinig <sam@webkit.org>

Add COMPILER_SUPPORTS macro to allow for compiler feature testing
Expand Down
1 change: 1 addition & 0 deletions Source/JavaScriptCore/JavaScriptCore.gypi
Expand Up @@ -207,6 +207,7 @@
'wtf/StdLibExtras.h',
'wtf/StringExtras.h',
'wtf/StringHasher.h',
'wtf/TemporarilyChange.h',
'wtf/ThreadSafeRefCounted.h',
'wtf/ThreadSpecific.h',
'wtf/ThreadRestrictionVerifier.h',
Expand Down
4 changes: 4 additions & 0 deletions Source/JavaScriptCore/JavaScriptCore.vcproj/WTF/WTF.vcproj
Expand Up @@ -1152,6 +1152,10 @@
RelativePath="..\..\wtf\TCSystemAlloc.h"
>
</File>
<File
RelativePath="..\..\wtf\TemporarilyChange.h"
>
</File>
<File
RelativePath="..\..\wtf\Threading.cpp"
>
Expand Down
Expand Up @@ -46,6 +46,7 @@
0B330C270F38C62300692DE3 /* TypeTraits.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 0B330C260F38C62300692DE3 /* TypeTraits.cpp */; };
0B4D7E630F319AC800AD7E58 /* TypeTraits.h in Headers */ = {isa = PBXBuildFile; fileRef = 0B4D7E620F319AC800AD7E58 /* TypeTraits.h */; settings = {ATTRIBUTES = (Private, ); }; };
0BAC94A01338728400CF135B /* ThreadRestrictionVerifier.h in Headers */ = {isa = PBXBuildFile; fileRef = 0BAC949E1338728400CF135B /* ThreadRestrictionVerifier.h */; settings = {ATTRIBUTES = (Private, ); }; };
0BCD83571485845200EA2003 /* TemporarilyChange.h in Headers */ = {isa = PBXBuildFile; fileRef = 0BCD83541485841200EA2003 /* TemporarilyChange.h */; settings = {ATTRIBUTES = (Private, ); }; };
0BDFFAE10FC6193100D69EF4 /* OwnFastMallocPtr.h in Headers */ = {isa = PBXBuildFile; fileRef = 0BDFFAD10FC616EC00D69EF4 /* OwnFastMallocPtr.h */; settings = {ATTRIBUTES = (Private, ); }; };
0BF28A2911A33DC300638F84 /* SizeLimits.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 0BF28A2811A33DC300638F84 /* SizeLimits.cpp */; };
0F16D726142C39C000CF784A /* BitVector.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 0F16D724142C39A200CF784A /* BitVector.cpp */; };
Expand Down Expand Up @@ -822,6 +823,7 @@
0B330C260F38C62300692DE3 /* TypeTraits.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = TypeTraits.cpp; sourceTree = "<group>"; };
0B4D7E620F319AC800AD7E58 /* TypeTraits.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TypeTraits.h; sourceTree = "<group>"; };
0BAC949E1338728400CF135B /* ThreadRestrictionVerifier.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ThreadRestrictionVerifier.h; sourceTree = "<group>"; };
0BCD83541485841200EA2003 /* TemporarilyChange.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TemporarilyChange.h; sourceTree = "<group>"; };
0BDFFAD10FC616EC00D69EF4 /* OwnFastMallocPtr.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = OwnFastMallocPtr.h; sourceTree = "<group>"; };
0BF28A2811A33DC300638F84 /* SizeLimits.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = SizeLimits.cpp; sourceTree = "<group>"; };
0F16D724142C39A200CF784A /* BitVector.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = BitVector.cpp; sourceTree = "<group>"; };
Expand Down Expand Up @@ -2026,6 +2028,7 @@
6541BD6F08E80A17002CBEE7 /* TCSpinLock.h */,
6541BD7008E80A17002CBEE7 /* TCSystemAlloc.cpp */,
6541BD7108E80A17002CBEE7 /* TCSystemAlloc.h */,
0BCD83541485841200EA2003 /* TemporarilyChange.h */,
8656573E115BE35200291F40 /* text */,
18BAB52710DADFCD000D945B /* ThreadIdentifierDataPthreads.cpp */,
18BAB52810DADFCD000D945B /* ThreadIdentifierDataPthreads.h */,
Expand Down Expand Up @@ -2872,6 +2875,7 @@
BC18C46C0E16F5CD00B34460 /* TCPackedCache.h in Headers */,
BC18C46D0E16F5CD00B34460 /* TCPageMap.h in Headers */,
BC18C46F0E16F5CD00B34460 /* TCSystemAlloc.h in Headers */,
0BCD83571485845200EA2003 /* TemporarilyChange.h in Headers */,
971EDEA61169E0D3005E4262 /* Terminator.h in Headers */,
F3BD31ED126735770065467F /* TextPosition.h in Headers */,
18BAB55410DAE066000D945B /* ThreadIdentifierDataPthreads.h in Headers */,
Expand Down
68 changes: 68 additions & 0 deletions Source/JavaScriptCore/wtf/TemporarilyChange.h
@@ -0,0 +1,68 @@
/*
* Copyright (C) 2011 Google Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#ifndef TemporarilyChange_h
#define TemporarilyChange_h

#include "Noncopyable.h"

namespace WTF {

// TemporarilyChange<> is useful for setting a variable to a new value only within a
// particular scope. An TemporarilyChange<> object changes a variable to its original
// value upon destruction, making it an alternative to writing "var = false;"
// or "var = oldVal;" at all of a block's exit points.
//
// This should be obvious, but note that an TemporarilyChange<> instance should have a
// shorter lifetime than its scopedVariable, to prevent invalid memory writes
// when the TemporarilyChange<> object is destroyed.

template<typename T>
class TemporarilyChange {
WTF_MAKE_NONCOPYABLE(TemporarilyChange);
public:
TemporarilyChange(T& scopedVariable, T newValue)
: m_scopedVariable(scopedVariable)
, m_originalValue(scopedVariable)
{
m_scopedVariable = newValue;
}

~TemporarilyChange()
{
m_scopedVariable = m_originalValue;
}


private:
T& m_scopedVariable;
T m_originalValue;
};

}

using WTF::TemporarilyChange;

#endif
9 changes: 9 additions & 0 deletions Source/JavaScriptGlue/ChangeLog
@@ -1,3 +1,12 @@
2011-11-29 David Levin <levin@chromium.org>

Add a way to revert a variable to its previous value after leaving a scope.
https://bugs.webkit.org/show_bug.cgi?id=73371

Reviewed by Adam Barth.

* ForwardingHeaders/wtf/TemporarilyChange.h: Added.

2011-11-11 Mark Hahnenberg <mhahnenberg@apple.com>

Add jsCast to replace static_cast
Expand Down
@@ -0,0 +1 @@
#include <JavaScriptCore/TemporarilyChange.h>
9 changes: 9 additions & 0 deletions Source/WebCore/ChangeLog
@@ -1,3 +1,12 @@
2011-11-29 David Levin <levin@chromium.org>

Add a way to revert a variable to its previous value after leaving a scope.
https://bugs.webkit.org/show_bug.cgi?id=73371

Reviewed by Adam Barth.

* ForwardingHeaders/wtf/TemporarilyChange.h: Added.

2011-11-28 Kentaro Hara <haraken@chromium.org>

Implement [Supplemental] IDL and support it in run-bindings-tests
Expand Down
4 changes: 4 additions & 0 deletions Source/WebCore/ForwardingHeaders/wtf/TemporarilyChange.h
@@ -0,0 +1,4 @@
#ifndef WebCore_FWD_TemporarilyChange_h
#define WebCore_FWD_TemporarilyChange_h
#include <JavaScriptCore/TemporarilyChange.h>
#endif
9 changes: 9 additions & 0 deletions Source/WebKit/mac/ChangeLog
@@ -1,3 +1,12 @@
2011-11-29 David Levin <levin@chromium.org>

Add a way to revert a variable to its previous value after leaving a scope.
https://bugs.webkit.org/show_bug.cgi?id=73371

Reviewed by Adam Barth.

* ForwardingHeaders/wtf/TemporarilyChange.h: Added.

2011-11-29 Jessie Berlin <jberlin@apple.com>

WKKeyValueStorageManagerGetKeyValueStorageOrigins may not report the correct list of origins
Expand Down
@@ -0,0 +1 @@
#include <JavaScriptCore/TemporarilyChange.h>
14 changes: 14 additions & 0 deletions Tools/ChangeLog
@@ -1,3 +1,17 @@
2011-11-29 David Levin <levin@chromium.org>

Add a way to revert a variable to its previous value after leaving a scope.
https://bugs.webkit.org/show_bug.cgi?id=73371

Reviewed by Adam Barth.

* DumpRenderTree/ForwardingHeaders/wtf/TemporarilyChange.h: Added.
* TestWebKitAPI/TestWebKitAPI.gypi: Added test file to the build.
* TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj: Ditto.
* TestWebKitAPI/win/TestWebKitAPI.vcproj: Ditto.
* TestWebKitAPI/Tests/WTF/TemporarilyChange.cpp: Added.
(TestWebKitAPI::TEST): Added a test for TemporarilyChange.

2011-11-28 Kentaro Hara <haraken@chromium.org>

Implement [Supplemental] IDL and support it in run-bindings-tests
Expand Down
@@ -0,0 +1 @@
#include <JavaScriptCore/TemporarilyChange.h>
1 change: 1 addition & 0 deletions Tools/TestWebKitAPI/TestWebKitAPI.gypi
Expand Up @@ -35,6 +35,7 @@
'Tests/WTF/RedBlackTree.cpp',
'Tests/WTF/StringBuilder.cpp',
'Tests/WTF/StringOperators.cpp',
'Tests/WTF/TemporarilyChange.cpp',
'Tests/WTF/VectorBasic.cpp',
'Tests/WTF/VectorReverse.cpp',
],
Expand Down
4 changes: 4 additions & 0 deletions Tools/TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj
Expand Up @@ -7,6 +7,7 @@
objects = {

/* Begin PBXBuildFile section */
0BCD856A1485C98B00EA2003 /* TemporarilyChange.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 0BCD85691485C98B00EA2003 /* TemporarilyChange.cpp */; };
0FC6C4CC141027E0005B7F0C /* RedBlackTree.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 0FC6C4CB141027E0005B7F0C /* RedBlackTree.cpp */; };
0FC6C4CF141034AD005B7F0C /* MetaAllocator.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 0FC6C4CE141034AD005B7F0C /* MetaAllocator.cpp */; };
1A02C84F125D4A8400E3F4BD /* Find.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 1A02C84E125D4A8400E3F4BD /* Find.cpp */; };
Expand Down Expand Up @@ -145,6 +146,7 @@
/* End PBXCopyFilesBuildPhase section */

/* Begin PBXFileReference section */
0BCD85691485C98B00EA2003 /* TemporarilyChange.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = TemporarilyChange.cpp; path = WTF/TemporarilyChange.cpp; sourceTree = "<group>"; };
0FC6C4CB141027E0005B7F0C /* RedBlackTree.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = RedBlackTree.cpp; path = WTF/RedBlackTree.cpp; sourceTree = "<group>"; };
0FC6C4CE141034AD005B7F0C /* MetaAllocator.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = MetaAllocator.cpp; path = WTF/MetaAllocator.cpp; sourceTree = "<group>"; };
1A02C84B125D4A5E00E3F4BD /* find.html */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.html; path = find.html; sourceTree = "<group>"; };
Expand Down Expand Up @@ -416,6 +418,7 @@
A7A966DA140ECCC8005EF9B4 /* CheckedArithmeticOperations.cpp */,
81B50192140F232300D9EB58 /* StringBuilder.cpp */,
C01363C713C3997300EF3964 /* StringOperators.cpp */,
0BCD85691485C98B00EA2003 /* TemporarilyChange.cpp */,
BC90964B125561BF00083756 /* VectorBasic.cpp */,
37200B9113A16230007A4FAD /* VectorReverse.cpp */,
);
Expand Down Expand Up @@ -660,6 +663,7 @@
BC3C4C7F14587AA60025FB62 /* WKBrowsingContextGroupTest.mm in Sources */,
C0C5D3BE14598B6F00A802A6 /* GetBackingScaleFactor.mm in Sources */,
3722C8691461E03E00C45D00 /* RenderedImageFromDOMRange.mm in Sources */,
0BCD856A1485C98B00EA2003 /* TemporarilyChange.cpp in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
47 changes: 47 additions & 0 deletions Tools/TestWebKitAPI/Tests/WTF/TemporarilyChange.cpp
@@ -0,0 +1,47 @@
/*
* Copyright (C) 2011 Google Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/

#include "config.h"

#include <wtf/TemporarilyChange.h>

namespace TestWebKitAPI {

TEST(WTF, TemporarilyChangeNested)
{
bool originallyFalse = false;
{
TemporarilyChange<bool> change1OriginallyFalse(originallyFalse, true);
EXPECT_TRUE(originallyFalse);
{
TemporarilyChange<bool> change2OriginallyFalse(originallyFalse, false);
EXPECT_FALSE(originallyFalse);
}
EXPECT_TRUE(originallyFalse);
}
EXPECT_FALSE(originallyFalse);
}

} // namespace TestWebKitAPI
4 changes: 4 additions & 0 deletions Tools/TestWebKitAPI/win/TestWebKitAPI.vcproj
Expand Up @@ -639,6 +639,10 @@
RelativePath="..\Tests\WTF\StringOperators.cpp"
>
</File>
<File
RelativePath="..\Tests\WTF\TemporarilyChange.cpp"
>
</File>
<File
RelativePath="..\Tests\WTF\VectorBasic.cpp"
>
Expand Down

0 comments on commit f4ec26d

Please sign in to comment.