Add WTF::Optional class
authorandersca@apple.com <andersca@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Fri, 20 Jun 2014 16:35:36 +0000 (16:35 +0000)
committerandersca@apple.com <andersca@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Fri, 20 Jun 2014 16:35:36 +0000 (16:35 +0000)
https://bugs.webkit.org/show_bug.cgi?id=134083

Reviewed by Andreas Kling.

Source/WTF:
* WTF.xcodeproj/project.pbxproj:
* wtf/Optional.h: Added.
(WTF::Optional::Optional):
(WTF::Optional::~Optional):
(WTF::Optional::operator bool):
(WTF::Optional::value):

Tools:
* TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj:
* TestWebKitAPI/Tests/WTF/Optional.cpp: Added.
(TestWebKitAPI::TEST):

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@170187 268f45cc-cd09-0410-ab3c-d52691b4dbfc

Source/WTF/ChangeLog
Source/WTF/WTF.xcodeproj/project.pbxproj
Source/WTF/wtf/Optional.h [new file with mode: 0644]
Tools/ChangeLog
Tools/TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj
Tools/TestWebKitAPI/Tests/WTF/Optional.cpp [new file with mode: 0644]

index 793b777..351d0e7 100644 (file)
@@ -1,3 +1,17 @@
+2014-06-19  Anders Carlsson  <andersca@apple.com>
+
+        Add WTF::Optional class
+        https://bugs.webkit.org/show_bug.cgi?id=134083
+
+        Reviewed by Andreas Kling.
+
+        * WTF.xcodeproj/project.pbxproj:
+        * wtf/Optional.h: Added.
+        (WTF::Optional::Optional):
+        (WTF::Optional::~Optional):
+        (WTF::Optional::operator bool):
+        (WTF::Optional::value):
+
 2014-06-19  Dániel Bátyai  <dbatyai.u-szeged@partner.samsung.com>
 
         Remove ENABLE(LLINT) and ENABLE(LLINT_C_LOOP) guards
index fc913ba..6e2efc8 100644 (file)
@@ -63,6 +63,7 @@
                1A6EB1E0187D0BD30030126F /* StringView.h in Headers */ = {isa = PBXBuildFile; fileRef = 1A6EB1DF187D0BD30030126F /* StringView.h */; };
                1ACADD841884480100D8B71D /* DeprecatedSymbolsUsedBySafari.mm in Sources */ = {isa = PBXBuildFile; fileRef = 1ACADD821884480100D8B71D /* DeprecatedSymbolsUsedBySafari.mm */; };
                1AFDE648195201C300C48FFA /* TypeCasts.h in Headers */ = {isa = PBXBuildFile; fileRef = 1AFDE647195201C300C48FFA /* TypeCasts.h */; };
+               1AFDE6531953B23D00C48FFA /* Optional.h in Headers */ = {isa = PBXBuildFile; fileRef = 1AFDE6521953B23D00C48FFA /* Optional.h */; };
                1FA47C8A152502DA00568D1B /* WebCoreThread.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 1FA47C88152502DA00568D1B /* WebCoreThread.cpp */; };
                1FA47C8B152502DA00568D1B /* WebCoreThread.h in Headers */ = {isa = PBXBuildFile; fileRef = 1FA47C89152502DA00568D1B /* WebCoreThread.h */; };
                26147B0A15DDCCDC00DDB907 /* IntegerToStringConversion.h in Headers */ = {isa = PBXBuildFile; fileRef = 26147B0815DDCCDC00DDB907 /* IntegerToStringConversion.h */; };
                1A6EB1DF187D0BD30030126F /* StringView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = StringView.h; sourceTree = "<group>"; };
                1ACADD821884480100D8B71D /* DeprecatedSymbolsUsedBySafari.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = DeprecatedSymbolsUsedBySafari.mm; sourceTree = "<group>"; };
                1AFDE647195201C300C48FFA /* TypeCasts.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TypeCasts.h; sourceTree = "<group>"; };
+               1AFDE6521953B23D00C48FFA /* Optional.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Optional.h; sourceTree = "<group>"; };
                1FA47C88152502DA00568D1B /* WebCoreThread.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = WebCoreThread.cpp; sourceTree = "<group>"; };
                1FA47C89152502DA00568D1B /* WebCoreThread.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WebCoreThread.h; sourceTree = "<group>"; };
                26147B0815DDCCDC00DDB907 /* IntegerToStringConversion.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = IntegerToStringConversion.h; sourceTree = "<group>"; };
                                A8A472D8151A825B004123FF /* OSAllocatorPosix.cpp */,
                                A8A472DA151A825B004123FF /* OSRandomSource.cpp */,
                                A8A472DB151A825B004123FF /* OSRandomSource.h */,
+                               1AFDE6521953B23D00C48FFA /* Optional.h */,
                                A8A472DD151A825B004123FF /* OwnPtr.h */,
                                A8A472DE151A825B004123FF /* OwnPtrCommon.h */,
                                A8A472DF151A825B004123FF /* PackedIntVector.h */,
                                A8A47441151A825B004123FF /* StringImpl.h in Headers */,
                                A8A47442151A825B004123FF /* StringOperators.h in Headers */,
                                0FDDBFA81666DFA300C55FEF /* StringPrintStream.h in Headers */,
+                               1AFDE6531953B23D00C48FFA /* Optional.h in Headers */,
                                A8A473B8151A825B004123FF /* strtod.h in Headers */,
                                A8A4742E151A825B004123FF /* TCPackedCache.h in Headers */,
                                A8A4742F151A825B004123FF /* TCPageMap.h in Headers */,
diff --git a/Source/WTF/wtf/Optional.h b/Source/WTF/wtf/Optional.h
new file mode 100644 (file)
index 0000000..f3d68cb
--- /dev/null
@@ -0,0 +1,107 @@
+/*
+ * Copyright (C) 2014 Apple 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.
+ */
+
+#ifndef Optional_h
+#define Optional_h
+
+#include <type_traits>
+#include <wtf/Assertions.h>
+
+// WTF::Optional is a class based on std::optional, described here:
+// http://www.open-std.org/JTC1/SC22/WG21/docs/papers/2013/n3527.html
+// If this ends up in a C++ standard, we should replace our implementation with it.
+
+namespace WTF {
+
+enum InPlaceTag { InPlace };
+enum NulloptTag { Nullopt };
+
+template<typename T>
+class Optional {
+public:
+    Optional()
+        : m_isEngaged(false)
+    {
+    }
+
+    Optional(NulloptTag)
+        : m_isEngaged(false)
+    {
+    }
+
+    Optional(const T& value)
+        : m_isEngaged(true)
+        , m_value(value)
+    {
+    }
+
+    Optional(T&& value)
+        : m_isEngaged(true)
+        , m_value(std::move(value))
+    {
+    }
+
+    template<typename... Args>
+    Optional(InPlaceTag, Args&&... args)
+        : m_isEngaged(true)
+        , m_value(std::forward<Args>(args)...)
+    {
+
+    }
+
+    ~Optional()
+    {
+        if (m_isEngaged)
+            m_value.~T();
+    }
+
+    explicit operator bool() const { return m_isEngaged; }
+
+    T& value()
+    {
+        ASSERT(m_isEngaged);
+        return m_value;
+    }
+
+    const T& value() const
+    {
+        ASSERT(m_isEngaged);
+        return m_value;
+    }
+
+private:
+    bool m_isEngaged;
+    union {
+        T m_value;
+    };
+};
+
+} // namespace WTF
+
+using WTF::InPlace;
+using WTF::Nullopt;
+using WTF::Optional;
+
+#endif // Optional_h
index 480de4c..40e9361 100644 (file)
@@ -1,3 +1,14 @@
+2014-06-19  Anders Carlsson  <andersca@apple.com>
+
+        Add WTF::Optional class
+        https://bugs.webkit.org/show_bug.cgi?id=134083
+
+        Reviewed by Andreas Kling.
+
+        * TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj:
+        * TestWebKitAPI/Tests/WTF/Optional.cpp: Added.
+        (TestWebKitAPI::TEST):
+
 2014-06-20  Ion Rosca  <rosca@adobe.com>
 
         Unreviewed. Adding myself to Committers list.
index a468303..007776f 100644 (file)
@@ -32,6 +32,7 @@
                1AE72F48173EB214006362F0 /* TerminateTwice.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 1AE72F47173EB214006362F0 /* TerminateTwice.cpp */; };
                1AEDE22613E5E7E700E62FE8 /* InjectedBundleControllerMac.mm in Sources */ = {isa = PBXBuildFile; fileRef = 1AEDE22413E5E7A000E62FE8 /* InjectedBundleControllerMac.mm */; };
                1AEF994917A09F5400998EF0 /* GetPIDAfterAbortedProcessLaunch.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 1AEF994817A09F5300998EF0 /* GetPIDAfterAbortedProcessLaunch.cpp */; };
+               1AFDE6561953B2C000C48FFA /* Optional.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 1AFDE6541953B2C000C48FFA /* Optional.cpp */; };
                261516D615B0E60500A2C201 /* SetAndUpdateCacheModel.mm in Sources */ = {isa = PBXBuildFile; fileRef = 261516D515B0E60500A2C201 /* SetAndUpdateCacheModel.mm */; };
                26300B1816755CD90066886D /* ListHashSet.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 26300B1716755CD90066886D /* ListHashSet.cpp */; };
                265AF55015D1E48A00B0CB4A /* WTFString.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 265AF54F15D1E48A00B0CB4A /* WTFString.cpp */; };
                1AE72F47173EB214006362F0 /* TerminateTwice.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = TerminateTwice.cpp; sourceTree = "<group>"; };
                1AEDE22413E5E7A000E62FE8 /* InjectedBundleControllerMac.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = InjectedBundleControllerMac.mm; sourceTree = "<group>"; };
                1AEF994817A09F5300998EF0 /* GetPIDAfterAbortedProcessLaunch.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = GetPIDAfterAbortedProcessLaunch.cpp; sourceTree = "<group>"; };
+               1AFDE6541953B2C000C48FFA /* Optional.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = Optional.cpp; sourceTree = "<group>"; };
                261516D515B0E60500A2C201 /* SetAndUpdateCacheModel.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = SetAndUpdateCacheModel.mm; sourceTree = "<group>"; };
                26300B1716755CD90066886D /* ListHashSet.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ListHashSet.cpp; sourceTree = "<group>"; };
                265AF54F15D1E48A00B0CB4A /* WTFString.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = WTFString.cpp; sourceTree = "<group>"; };
                                CD5497B315857F0C00B5BC30 /* MediaTime.cpp */,
                                0FC6C4CE141034AD005B7F0C /* MetaAllocator.cpp */,
                                93A427AC180DA60F00CD24D7 /* MoveOnly.h */,
+                               1AFDE6541953B2C000C48FFA /* Optional.cpp */,
                                0FC6C4CB141027E0005B7F0C /* RedBlackTree.cpp */,
                                93A427AA180DA26400CD24D7 /* Ref.cpp */,
                                93A427AD180DA60F00CD24D7 /* RefLogger.h */,
                                7C8DDAAB1735DEEE00EA5AC0 /* CloseThenTerminate.cpp in Sources */,
                                CDC2C71517970DDB00E627FB /* TimeRanges.cpp in Sources */,
                                51FCF79A1534AC6D00104491 /* ShouldGoToBackForwardListItem.cpp in Sources */,
+                               1AFDE6561953B2C000C48FFA /* Optional.cpp in Sources */,
                                C540F776152E4DA000A40C8C /* SimplifyMarkup.mm in Sources */,
                                C02B77F2126612140026BF0F /* SpacebarScrolling.cpp in Sources */,
                                93D3D19E17B1A84200C7C415 /* LayoutMilestonesWithAllContentInFrame.cpp in Sources */,
diff --git a/Tools/TestWebKitAPI/Tests/WTF/Optional.cpp b/Tools/TestWebKitAPI/Tests/WTF/Optional.cpp
new file mode 100644 (file)
index 0000000..64d2019
--- /dev/null
@@ -0,0 +1,75 @@
+/*
+ * Copyright (C) 2014 Apple 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/Optional.h>
+
+namespace TestWebKitAPI {
+
+TEST(WTF_Optional, Disengaged)
+{
+    {
+        Optional<int> optional;
+
+        EXPECT_FALSE(static_cast<bool>(optional));
+    }
+
+    {
+        Optional<int> optional { Nullopt };
+
+        EXPECT_FALSE(static_cast<bool>(optional));
+    }
+}
+
+TEST(WTF_Optional, Engaged)
+{
+    Optional<int> optional { 10 };
+
+    EXPECT_TRUE(static_cast<bool>(optional));
+    EXPECT_EQ(10, optional.value());
+}
+
+TEST(WTF_Optional, Destructor)
+{
+    static bool didCallDestructor = false;
+    struct A {
+        ~A()
+        {
+            EXPECT_FALSE(didCallDestructor);
+            didCallDestructor = true;
+        }
+    };
+
+    {
+        Optional<A> optional { InPlace };
+
+        EXPECT_TRUE(static_cast<bool>(optional));
+    }
+
+    EXPECT_TRUE(didCallDestructor);
+}
+
+} // namespace TestWebKitAPI