Skip to content

Commit 8bf4efe

Browse files
Implement WTF::Expected
https://bugs.webkit.org/show_bug.cgi?id=164526 Reviewed by Yusuke Suzuki. std::expected isn't in C++17, and may be in C++20. It's a nice complement to std::any / std::optional because it's a type-tagged union which has a single expected result but could also contain an error. This would be useful in the WebAssembly parser, for example. Using this implementation will allow us to provide feedback to the standards committee and guide std::expected's design before it gets standardized. I've already sent a bunch of feedback to the author based on my experience implementing this. This could supplement WTF::Either and WTF::ExceptionOr. Source/WTF: * WTF.xcodeproj/project.pbxproj: * wtf/Compiler.h: Add RELAXED_CONSTEXPR * wtf/Expected.h: Added. (WTF::UnexpectedType::UnexpectedType): (WTF::UnexpectedType::value): (WTF::operator==): (WTF::operator!=): (WTF::operator<): (WTF::operator>): (WTF::operator<=): (WTF::operator>=): (WTF::makeUnexpected): (WTF::ExpectedDetail::Throw): (WTF::ExpectedDetail::ConstexprBase::ConstexprBase): (WTF::ExpectedDetail::Base::Base): (WTF::ExpectedDetail::Base::~Base): (WTF::Expected::Expected): (WTF::Expected::operator=): (WTF::Expected::swap): (WTF::Expected::operator->): (WTF::Expected::operator*): (WTF::Expected::operator bool): (WTF::Expected::hasValue): (WTF::Expected::value): (WTF::Expected::error): (WTF::Expected::getUnexpected): (WTF::Expected::valueOr): (WTF::swap): (WTF::makeExpected): (WTF::makeExpectedFromError): Tools: * TestWebKitAPI/CMakeLists.txt: * TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj: * TestWebKitAPI/Tests/WTF/Expected.cpp: Added. (WTF::operator<<): (TestWebKitAPI::TEST): (TestWebKitAPI::foo::foo): (TestWebKitAPI::foo::~foo): (TestWebKitAPI::foo::operator==): (TestWebKitAPI::operator<<): git-svn-id: http://svn.webkit.org/repository/webkit/trunk@208670 268f45cc-cd09-0410-ab3c-d52691b4dbfc
1 parent acf7f02 commit 8bf4efe

File tree

9 files changed

+1021
-2
lines changed

9 files changed

+1021
-2
lines changed

Source/WTF/ChangeLog

+52
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,55 @@
1+
2016-11-13 JF Bastien <[email protected]>
2+
3+
Implement WTF::Expected
4+
https://bugs.webkit.org/show_bug.cgi?id=164526
5+
6+
Reviewed by Yusuke Suzuki.
7+
8+
std::expected isn't in C++17, and may be in C++20. It's a nice
9+
complement to std::any / std::optional because it's a type-tagged
10+
union which has a single expected result but could also contain an
11+
error.
12+
13+
This would be useful in the WebAssembly parser, for example.
14+
15+
Using this implementation will allow us to provide feedback to the
16+
standards committee and guide std::expected's design before it
17+
gets standardized. I've already sent a bunch of feedback to the
18+
author based on my experience implementing this.
19+
20+
This could supplement WTF::Either and WTF::ExceptionOr.
21+
22+
* WTF.xcodeproj/project.pbxproj:
23+
* wtf/Compiler.h: Add RELAXED_CONSTEXPR
24+
* wtf/Expected.h: Added.
25+
(WTF::UnexpectedType::UnexpectedType):
26+
(WTF::UnexpectedType::value):
27+
(WTF::operator==):
28+
(WTF::operator!=):
29+
(WTF::operator<):
30+
(WTF::operator>):
31+
(WTF::operator<=):
32+
(WTF::operator>=):
33+
(WTF::makeUnexpected):
34+
(WTF::ExpectedDetail::Throw):
35+
(WTF::ExpectedDetail::ConstexprBase::ConstexprBase):
36+
(WTF::ExpectedDetail::Base::Base):
37+
(WTF::ExpectedDetail::Base::~Base):
38+
(WTF::Expected::Expected):
39+
(WTF::Expected::operator=):
40+
(WTF::Expected::swap):
41+
(WTF::Expected::operator->):
42+
(WTF::Expected::operator*):
43+
(WTF::Expected::operator bool):
44+
(WTF::Expected::hasValue):
45+
(WTF::Expected::value):
46+
(WTF::Expected::error):
47+
(WTF::Expected::getUnexpected):
48+
(WTF::Expected::valueOr):
49+
(WTF::swap):
50+
(WTF::makeExpected):
51+
(WTF::makeExpectedFromError):
52+
153
2016-11-11 Alex Christensen <[email protected]>
254

355
Allow mutable lambdas in HashMap::ensure

Source/WTF/WTF.xcodeproj/project.pbxproj

+4
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,7 @@
328328
A8A47486151A825B004123FF /* WTFThreadData.cpp in Sources */ = {isa = PBXBuildFile; fileRef = A8A4737A151A825B004123FF /* WTFThreadData.cpp */; };
329329
A8A47487151A825B004123FF /* WTFThreadData.h in Headers */ = {isa = PBXBuildFile; fileRef = A8A4737B151A825B004123FF /* WTFThreadData.h */; };
330330
A8A4748C151A8264004123FF /* config.h in Headers */ = {isa = PBXBuildFile; fileRef = A8A4748B151A8264004123FF /* config.h */; };
331+
AD7C434B1DD2A4A70026888B /* Expected.h in Headers */ = {isa = PBXBuildFile; fileRef = AD7C434A1DD2A4A70026888B /* Expected.h */; };
331332
B38FD7BD168953E80065C969 /* FeatureDefines.h in Headers */ = {isa = PBXBuildFile; fileRef = B38FD7BC168953E80065C969 /* FeatureDefines.h */; };
332333
C4F8A93719C65EB400B2B15D /* Stopwatch.h in Headers */ = {isa = PBXBuildFile; fileRef = C4F8A93619C65EB400B2B15D /* Stopwatch.h */; };
333334
C8B0E1A1E01A486EB95E0D11 /* IndexSet.h in Headers */ = {isa = PBXBuildFile; fileRef = 3137E1D7DBD84AC38FAE4D34 /* IndexSet.h */; };
@@ -695,6 +696,7 @@
695696
A8A4737A151A825B004123FF /* WTFThreadData.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = WTFThreadData.cpp; sourceTree = "<group>"; };
696697
A8A4737B151A825B004123FF /* WTFThreadData.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WTFThreadData.h; sourceTree = "<group>"; };
697698
A8A4748B151A8264004123FF /* config.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = config.h; sourceTree = "<group>"; };
699+
AD7C434A1DD2A4A70026888B /* Expected.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Expected.h; sourceTree = "<group>"; };
698700
B38FD7BC168953E80065C969 /* FeatureDefines.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = FeatureDefines.h; sourceTree = "<group>"; };
699701
C4F8A93619C65EB400B2B15D /* Stopwatch.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Stopwatch.h; sourceTree = "<group>"; };
700702
CD5497AA15857D0300B5BC30 /* MediaTime.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = MediaTime.cpp; sourceTree = "<group>"; };
@@ -848,6 +850,7 @@
848850
A876DBD6151816E500DADB95 /* wtf */ = {
849851
isa = PBXGroup;
850852
children = (
853+
AD7C434A1DD2A4A70026888B /* Expected.h */,
851854
2CDED0F018115C3F004DBA70 /* cf */,
852855
E4A0AD3B1A96251900536DF6 /* cocoa */,
853856
A8A47281151A825A004123FF /* dtoa */,
@@ -1457,6 +1460,7 @@
14571460
A8A4743E151A825B004123FF /* StringConcatenate.h in Headers */,
14581461
A8A4742C151A825B004123FF /* StringExtras.h in Headers */,
14591462
A8A4743F151A825B004123FF /* StringHash.h in Headers */,
1463+
AD7C434B1DD2A4A70026888B /* Expected.h in Headers */,
14601464
A748745417A0BDAE00FA04CB /* StringHashDumpContext.h in Headers */,
14611465
A8A47441151A825B004123FF /* StringImpl.h in Headers */,
14621466
A8A47442151A825B004123FF /* StringOperators.h in Headers */,

Source/WTF/wtf/Compiler.h

+15-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@
7272
#endif
7373
#endif
7474

75-
#endif
75+
#endif // defined(__clang__)
7676

7777
/* COMPILER(GCC_OR_CLANG) - GNU Compiler Collection or Clang */
7878
#if defined(__GNUC__)
@@ -144,6 +144,20 @@
144144
#define WTF_COMPILER_SUPPORTS_EABI 1
145145
#endif
146146

147+
/* RELAXED_CONSTEXPR */
148+
149+
#if defined(__cpp_constexpr) && __cpp_constexpr >= 201304
150+
#define WTF_COMPILER_SUPPORTS_RELAXED_CONSTEXPR 1
151+
#endif
152+
153+
#if !defined(RELAXED_CONSTEXPR)
154+
#if COMPILER_SUPPORTS(RELAXED_CONSTEXPR)
155+
#define RELAXED_CONSTEXPR constexpr
156+
#else
157+
#define RELAXED_CONSTEXPR
158+
#endif
159+
#endif
160+
147161
#define ASAN_ENABLED COMPILER_HAS_CLANG_FEATURE(address_sanitizer)
148162

149163
#if ASAN_ENABLED

0 commit comments

Comments
 (0)