Expected.h 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529
  1. /*
  2. * Copyright 2016-present Facebook, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /**
  17. * Like folly::Optional, but can store a value *or* an error.
  18. *
  19. * @author Eric Niebler (eniebler@fb.com)
  20. */
  21. #pragma once
  22. #include <cstddef>
  23. #include <initializer_list>
  24. #include <new>
  25. #include <stdexcept>
  26. #include <type_traits>
  27. #include <utility>
  28. #include <folly/CPortability.h>
  29. #include <folly/CppAttributes.h>
  30. #include <folly/Likely.h>
  31. #include <folly/Optional.h>
  32. #include <folly/Portability.h>
  33. #include <folly/Preprocessor.h>
  34. #include <folly/Traits.h>
  35. #include <folly/Unit.h>
  36. #include <folly/Utility.h>
  37. #include <folly/lang/ColdClass.h>
  38. #include <folly/lang/Exception.h>
  39. #define FOLLY_EXPECTED_ID(X) FB_CONCATENATE(FB_CONCATENATE(Folly, X), __LINE__)
  40. #define FOLLY_REQUIRES_IMPL(...) \
  41. bool FOLLY_EXPECTED_ID(Requires) = false, \
  42. typename std::enable_if< \
  43. (FOLLY_EXPECTED_ID(Requires) || static_cast<bool>(__VA_ARGS__)), \
  44. int>::type = 0
  45. #define FOLLY_REQUIRES_TRAILING(...) , FOLLY_REQUIRES_IMPL(__VA_ARGS__)
  46. #define FOLLY_REQUIRES(...) template <FOLLY_REQUIRES_IMPL(__VA_ARGS__)>
  47. /**
  48. * gcc-4.7 warns about use of uninitialized memory around the use of storage_
  49. * even though this is explicitly initialized at each point.
  50. */
  51. #if defined(__GNUC__) && !defined(__clang__)
  52. #pragma GCC diagnostic push
  53. #pragma GCC diagnostic ignored "-Wuninitialized"
  54. #pragma GCC diagnostic ignored "-Wpragmas"
  55. #pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
  56. #endif // __GNUC__
  57. namespace folly {
  58. /**
  59. * Forward declarations
  60. */
  61. template <class Error>
  62. class Unexpected;
  63. template <class Error>
  64. constexpr Unexpected<typename std::decay<Error>::type> makeUnexpected(Error&&);
  65. template <class Value, class Error>
  66. class Expected;
  67. template <class Error, class Value>
  68. constexpr Expected<typename std::decay<Value>::type, Error> makeExpected(
  69. Value&&);
  70. /**
  71. * Alias for an Expected type's assiciated value_type
  72. */
  73. template <class Expected>
  74. using ExpectedValueType =
  75. typename std::remove_reference<Expected>::type::value_type;
  76. /**
  77. * Alias for an Expected type's assiciated error_type
  78. */
  79. template <class Expected>
  80. using ExpectedErrorType =
  81. typename std::remove_reference<Expected>::type::error_type;
  82. // Details...
  83. namespace expected_detail {
  84. template <typename Value, typename Error>
  85. struct PromiseReturn;
  86. #ifdef _MSC_VER
  87. // MSVC 2015 can't handle the StrictConjunction, so we have
  88. // to use std::conjunction instead.
  89. template <template <class...> class Trait, class... Ts>
  90. using StrictAllOf = std::conjunction<Trait<Ts>...>;
  91. #else
  92. template <template <class...> class Trait, class... Ts>
  93. using StrictAllOf = StrictConjunction<Trait<Ts>...>;
  94. #endif
  95. template <class T>
  96. using IsCopyable = StrictConjunction<
  97. std::is_copy_constructible<T>,
  98. std::is_copy_assignable<T>>;
  99. template <class T>
  100. using IsMovable = StrictConjunction<
  101. std::is_move_constructible<T>,
  102. std::is_move_assignable<T>>;
  103. template <class T>
  104. using IsNothrowCopyable = StrictConjunction<
  105. std::is_nothrow_copy_constructible<T>,
  106. std::is_nothrow_copy_assignable<T>>;
  107. template <class T>
  108. using IsNothrowMovable = StrictConjunction<
  109. std::is_nothrow_move_constructible<T>,
  110. std::is_nothrow_move_assignable<T>>;
  111. template <class From, class To>
  112. using IsConvertible = StrictConjunction<
  113. std::is_constructible<To, From>,
  114. std::is_assignable<To&, From>>;
  115. template <class T, class U>
  116. auto doEmplaceAssign(int, T& t, U&& u)
  117. -> decltype(void(t = static_cast<U&&>(u))) {
  118. t = static_cast<U&&>(u);
  119. }
  120. template <class T, class U>
  121. auto doEmplaceAssign(long, T& t, U&& u)
  122. -> decltype(void(T(static_cast<U&&>(u)))) {
  123. t.~T();
  124. ::new ((void*)std::addressof(t)) T(static_cast<U&&>(u));
  125. }
  126. template <class T, class... Us>
  127. auto doEmplaceAssign(int, T& t, Us&&... us)
  128. -> decltype(void(t = T(static_cast<Us&&>(us)...))) {
  129. t = T(static_cast<Us&&>(us)...);
  130. }
  131. template <class T, class... Us>
  132. auto doEmplaceAssign(long, T& t, Us&&... us)
  133. -> decltype(void(T(static_cast<Us&&>(us)...))) {
  134. t.~T();
  135. ::new ((void*)std::addressof(t)) T(static_cast<Us&&>(us)...);
  136. }
  137. struct EmptyTag {};
  138. struct ValueTag {};
  139. struct ErrorTag {};
  140. enum class Which : unsigned char { eEmpty, eValue, eError };
  141. enum class StorageType { ePODStruct, ePODUnion, eUnion };
  142. template <class Value, class Error>
  143. constexpr StorageType getStorageType() {
  144. return StrictAllOf<is_trivially_copyable, Value, Error>::value
  145. ? (sizeof(std::pair<Value, Error>) <= sizeof(void * [2]) &&
  146. StrictAllOf<std::is_trivial, Value, Error>::value
  147. ? StorageType::ePODStruct
  148. : StorageType::ePODUnion)
  149. : StorageType::eUnion;
  150. }
  151. template <
  152. class Value,
  153. class Error,
  154. StorageType = expected_detail::getStorageType<Value, Error>()> // ePODUnion
  155. struct ExpectedStorage {
  156. using value_type = Value;
  157. using error_type = Error;
  158. union {
  159. Value value_;
  160. Error error_;
  161. char ch_;
  162. };
  163. Which which_;
  164. template <class E = Error, class = decltype(E{})>
  165. constexpr ExpectedStorage() noexcept(noexcept(E{}))
  166. : error_{}, which_(Which::eError) {}
  167. explicit constexpr ExpectedStorage(EmptyTag) noexcept
  168. : ch_{}, which_(Which::eEmpty) {}
  169. template <class... Vs>
  170. explicit constexpr ExpectedStorage(ValueTag, Vs&&... vs) noexcept(
  171. noexcept(Value(static_cast<Vs&&>(vs)...)))
  172. : value_(static_cast<Vs&&>(vs)...), which_(Which::eValue) {}
  173. template <class... Es>
  174. explicit constexpr ExpectedStorage(ErrorTag, Es&&... es) noexcept(
  175. noexcept(Error(static_cast<Es&&>(es)...)))
  176. : error_(static_cast<Es&&>(es)...), which_(Which::eError) {}
  177. void clear() noexcept {}
  178. static constexpr bool uninitializedByException() noexcept {
  179. // Although which_ may temporarily be eEmpty during construction, it
  180. // is always either eValue or eError for a fully-constructed Expected.
  181. return false;
  182. }
  183. template <class... Vs>
  184. void assignValue(Vs&&... vs) {
  185. expected_detail::doEmplaceAssign(0, value_, static_cast<Vs&&>(vs)...);
  186. which_ = Which::eValue;
  187. }
  188. template <class... Es>
  189. void assignError(Es&&... es) {
  190. expected_detail::doEmplaceAssign(0, error_, static_cast<Es&&>(es)...);
  191. which_ = Which::eError;
  192. }
  193. template <class Other>
  194. void assign(Other&& that) {
  195. switch (that.which_) {
  196. case Which::eValue:
  197. this->assignValue(static_cast<Other&&>(that).value());
  198. break;
  199. case Which::eError:
  200. this->assignError(static_cast<Other&&>(that).error());
  201. break;
  202. default:
  203. this->clear();
  204. break;
  205. }
  206. }
  207. Value& value() & {
  208. return value_;
  209. }
  210. const Value& value() const& {
  211. return value_;
  212. }
  213. Value&& value() && {
  214. return std::move(value_);
  215. }
  216. // TODO (t17322426): remove when VS2015 support is deprecated
  217. // VS2015 static analyzer incorrectly flags these as unreachable in certain
  218. // circumstances. VS2017 does not have this problem on the same code.
  219. FOLLY_PUSH_WARNING
  220. FOLLY_MSVC_DISABLE_WARNING(4702) // unreachable code
  221. Error& error() & {
  222. return error_;
  223. }
  224. const Error& error() const& {
  225. return error_;
  226. }
  227. Error&& error() && {
  228. return std::move(error_);
  229. }
  230. FOLLY_POP_WARNING
  231. };
  232. template <class Value, class Error>
  233. struct ExpectedUnion {
  234. union {
  235. Value value_;
  236. Error error_;
  237. char ch_{};
  238. };
  239. Which which_ = Which::eEmpty;
  240. explicit constexpr ExpectedUnion(EmptyTag) noexcept {}
  241. template <class... Vs>
  242. explicit constexpr ExpectedUnion(ValueTag, Vs&&... vs) noexcept(
  243. noexcept(Value(static_cast<Vs&&>(vs)...)))
  244. : value_(static_cast<Vs&&>(vs)...), which_(Which::eValue) {}
  245. template <class... Es>
  246. explicit constexpr ExpectedUnion(ErrorTag, Es&&... es) noexcept(
  247. noexcept(Error(static_cast<Es&&>(es)...)))
  248. : error_(static_cast<Es&&>(es)...), which_(Which::eError) {}
  249. ExpectedUnion(const ExpectedUnion&) {}
  250. ExpectedUnion(ExpectedUnion&&) noexcept {}
  251. ExpectedUnion& operator=(const ExpectedUnion&) {
  252. return *this;
  253. }
  254. ExpectedUnion& operator=(ExpectedUnion&&) noexcept {
  255. return *this;
  256. }
  257. ~ExpectedUnion() {}
  258. Value& value() & {
  259. return value_;
  260. }
  261. const Value& value() const& {
  262. return value_;
  263. }
  264. Value&& value() && {
  265. return std::move(value_);
  266. }
  267. Error& error() & {
  268. return error_;
  269. }
  270. const Error& error() const& {
  271. return error_;
  272. }
  273. Error&& error() && {
  274. return std::move(error_);
  275. }
  276. };
  277. template <class Derived, bool, bool Noexcept>
  278. struct CopyConstructible {
  279. constexpr CopyConstructible() = default;
  280. CopyConstructible(const CopyConstructible& that) noexcept(Noexcept) {
  281. static_cast<Derived*>(this)->assign(static_cast<const Derived&>(that));
  282. }
  283. constexpr CopyConstructible(CopyConstructible&&) = default;
  284. CopyConstructible& operator=(const CopyConstructible&) = default;
  285. CopyConstructible& operator=(CopyConstructible&&) = default;
  286. };
  287. template <class Derived, bool Noexcept>
  288. struct CopyConstructible<Derived, false, Noexcept> {
  289. constexpr CopyConstructible() = default;
  290. CopyConstructible(const CopyConstructible&) = delete;
  291. constexpr CopyConstructible(CopyConstructible&&) = default;
  292. CopyConstructible& operator=(const CopyConstructible&) = default;
  293. CopyConstructible& operator=(CopyConstructible&&) = default;
  294. };
  295. template <class Derived, bool, bool Noexcept>
  296. struct MoveConstructible {
  297. constexpr MoveConstructible() = default;
  298. constexpr MoveConstructible(const MoveConstructible&) = default;
  299. MoveConstructible(MoveConstructible&& that) noexcept(Noexcept) {
  300. static_cast<Derived*>(this)->assign(std::move(static_cast<Derived&>(that)));
  301. }
  302. MoveConstructible& operator=(const MoveConstructible&) = default;
  303. MoveConstructible& operator=(MoveConstructible&&) = default;
  304. };
  305. template <class Derived, bool Noexcept>
  306. struct MoveConstructible<Derived, false, Noexcept> {
  307. constexpr MoveConstructible() = default;
  308. constexpr MoveConstructible(const MoveConstructible&) = default;
  309. MoveConstructible(MoveConstructible&&) = delete;
  310. MoveConstructible& operator=(const MoveConstructible&) = default;
  311. MoveConstructible& operator=(MoveConstructible&&) = default;
  312. };
  313. template <class Derived, bool, bool Noexcept>
  314. struct CopyAssignable {
  315. constexpr CopyAssignable() = default;
  316. constexpr CopyAssignable(const CopyAssignable&) = default;
  317. constexpr CopyAssignable(CopyAssignable&&) = default;
  318. CopyAssignable& operator=(const CopyAssignable& that) noexcept(Noexcept) {
  319. static_cast<Derived*>(this)->assign(static_cast<const Derived&>(that));
  320. return *this;
  321. }
  322. CopyAssignable& operator=(CopyAssignable&&) = default;
  323. };
  324. template <class Derived, bool Noexcept>
  325. struct CopyAssignable<Derived, false, Noexcept> {
  326. constexpr CopyAssignable() = default;
  327. constexpr CopyAssignable(const CopyAssignable&) = default;
  328. constexpr CopyAssignable(CopyAssignable&&) = default;
  329. CopyAssignable& operator=(const CopyAssignable&) = delete;
  330. CopyAssignable& operator=(CopyAssignable&&) = default;
  331. };
  332. template <class Derived, bool, bool Noexcept>
  333. struct MoveAssignable {
  334. constexpr MoveAssignable() = default;
  335. constexpr MoveAssignable(const MoveAssignable&) = default;
  336. constexpr MoveAssignable(MoveAssignable&&) = default;
  337. MoveAssignable& operator=(const MoveAssignable&) = default;
  338. MoveAssignable& operator=(MoveAssignable&& that) noexcept(Noexcept) {
  339. static_cast<Derived*>(this)->assign(std::move(static_cast<Derived&>(that)));
  340. return *this;
  341. }
  342. };
  343. template <class Derived, bool Noexcept>
  344. struct MoveAssignable<Derived, false, Noexcept> {
  345. constexpr MoveAssignable() = default;
  346. constexpr MoveAssignable(const MoveAssignable&) = default;
  347. constexpr MoveAssignable(MoveAssignable&&) = default;
  348. MoveAssignable& operator=(const MoveAssignable&) = default;
  349. MoveAssignable& operator=(MoveAssignable&& that) = delete;
  350. };
  351. template <class Value, class Error>
  352. struct ExpectedStorage<Value, Error, StorageType::eUnion>
  353. : ExpectedUnion<Value, Error>,
  354. CopyConstructible<
  355. ExpectedStorage<Value, Error, StorageType::eUnion>,
  356. StrictAllOf<std::is_copy_constructible, Value, Error>::value,
  357. StrictAllOf<std::is_nothrow_copy_constructible, Value, Error>::value>,
  358. MoveConstructible<
  359. ExpectedStorage<Value, Error, StorageType::eUnion>,
  360. StrictAllOf<std::is_move_constructible, Value, Error>::value,
  361. StrictAllOf<std::is_nothrow_move_constructible, Value, Error>::value>,
  362. CopyAssignable<
  363. ExpectedStorage<Value, Error, StorageType::eUnion>,
  364. StrictAllOf<IsCopyable, Value, Error>::value,
  365. StrictAllOf<IsNothrowCopyable, Value, Error>::value>,
  366. MoveAssignable<
  367. ExpectedStorage<Value, Error, StorageType::eUnion>,
  368. StrictAllOf<IsMovable, Value, Error>::value,
  369. StrictAllOf<IsNothrowMovable, Value, Error>::value> {
  370. using value_type = Value;
  371. using error_type = Error;
  372. using Base = ExpectedUnion<Value, Error>;
  373. template <class E = Error, class = decltype(E{})>
  374. constexpr ExpectedStorage() noexcept(noexcept(E{})) : Base{ErrorTag{}} {}
  375. ExpectedStorage(const ExpectedStorage&) = default;
  376. ExpectedStorage(ExpectedStorage&&) = default;
  377. ExpectedStorage& operator=(const ExpectedStorage&) = default;
  378. ExpectedStorage& operator=(ExpectedStorage&&) = default;
  379. using ExpectedUnion<Value, Error>::ExpectedUnion;
  380. ~ExpectedStorage() {
  381. clear();
  382. }
  383. void clear() noexcept {
  384. switch (this->which_) {
  385. case Which::eValue:
  386. this->value().~Value();
  387. break;
  388. case Which::eError:
  389. this->error().~Error();
  390. break;
  391. default:
  392. break;
  393. }
  394. this->which_ = Which::eEmpty;
  395. }
  396. bool uninitializedByException() const noexcept {
  397. return this->which_ == Which::eEmpty;
  398. }
  399. template <class... Vs>
  400. void assignValue(Vs&&... vs) {
  401. if (this->which_ == Which::eValue) {
  402. expected_detail::doEmplaceAssign(
  403. 0, this->value(), static_cast<Vs&&>(vs)...);
  404. } else {
  405. this->clear();
  406. ::new ((void*)std::addressof(this->value()))
  407. Value(static_cast<Vs&&>(vs)...);
  408. this->which_ = Which::eValue;
  409. }
  410. }
  411. template <class... Es>
  412. void assignError(Es&&... es) {
  413. if (this->which_ == Which::eError) {
  414. expected_detail::doEmplaceAssign(
  415. 0, this->error(), static_cast<Es&&>(es)...);
  416. } else {
  417. this->clear();
  418. ::new ((void*)std::addressof(this->error()))
  419. Error(static_cast<Es&&>(es)...);
  420. this->which_ = Which::eError;
  421. }
  422. }
  423. bool isSelfAssign(const ExpectedStorage* that) const {
  424. return this == that;
  425. }
  426. constexpr bool isSelfAssign(const void*) const {
  427. return false;
  428. }
  429. template <class Other>
  430. void assign(Other&& that) {
  431. if (isSelfAssign(&that)) {
  432. return;
  433. }
  434. switch (that.which_) {
  435. case Which::eValue:
  436. this->assignValue(static_cast<Other&&>(that).value());
  437. break;
  438. case Which::eError:
  439. this->assignError(static_cast<Other&&>(that).error());
  440. break;
  441. default:
  442. this->clear();
  443. break;
  444. }
  445. }
  446. };
  447. // For small (pointer-sized) trivial types, a struct is faster than a union.
  448. template <class Value, class Error>
  449. struct ExpectedStorage<Value, Error, StorageType::ePODStruct> {
  450. using value_type = Value;
  451. using error_type = Error;
  452. Which which_;
  453. Error error_;
  454. Value value_;
  455. constexpr ExpectedStorage() noexcept
  456. : which_(Which::eError), error_{}, value_{} {}
  457. explicit constexpr ExpectedStorage(EmptyTag) noexcept
  458. : which_(Which::eEmpty), error_{}, value_{} {}
  459. template <class... Vs>
  460. explicit constexpr ExpectedStorage(ValueTag, Vs&&... vs) noexcept(
  461. noexcept(Value(static_cast<Vs&&>(vs)...)))
  462. : which_(Which::eValue), error_{}, value_(static_cast<Vs&&>(vs)...) {}
  463. template <class... Es>
  464. explicit constexpr ExpectedStorage(ErrorTag, Es&&... es) noexcept(
  465. noexcept(Error(static_cast<Es&&>(es)...)))
  466. : which_(Which::eError), error_(static_cast<Es&&>(es)...), value_{} {}
  467. void clear() noexcept {}
  468. constexpr static bool uninitializedByException() noexcept {
  469. return false;
  470. }
  471. template <class... Vs>
  472. void assignValue(Vs&&... vs) {
  473. expected_detail::doEmplaceAssign(0, value_, static_cast<Vs&&>(vs)...);
  474. which_ = Which::eValue;
  475. }
  476. template <class... Es>
  477. void assignError(Es&&... es) {
  478. expected_detail::doEmplaceAssign(0, error_, static_cast<Es&&>(es)...);
  479. which_ = Which::eError;
  480. }
  481. template <class Other>
  482. void assign(Other&& that) {
  483. switch (that.which_) {
  484. case Which::eValue:
  485. this->assignValue(static_cast<Other&&>(that).value());
  486. break;
  487. case Which::eError:
  488. this->assignError(static_cast<Other&&>(that).error());
  489. break;
  490. default:
  491. this->clear();
  492. break;
  493. }
  494. }
  495. Value& value() & {
  496. return value_;
  497. }
  498. const Value& value() const& {
  499. return value_;
  500. }
  501. Value&& value() && {
  502. return std::move(value_);
  503. }
  504. // TODO (t17322426): remove when VS2015 support is deprecated
  505. // VS2015 static analyzer incorrectly flags these as unreachable in certain
  506. // circumstances. VS2017 does not have this problem on the same code.
  507. FOLLY_PUSH_WARNING
  508. FOLLY_MSVC_DISABLE_WARNING(4702) // unreachable code
  509. Error& error() & {
  510. return error_;
  511. }
  512. const Error& error() const& {
  513. return error_;
  514. }
  515. Error&& error() && {
  516. return std::move(error_);
  517. }
  518. FOLLY_POP_WARNING
  519. };
  520. namespace expected_detail_ExpectedHelper {
  521. // Tricky hack so that Expected::then can handle lambdas that return void
  522. template <class T>
  523. inline T&& operator,(T&& t, Unit) noexcept {
  524. return static_cast<T&&>(t);
  525. }
  526. struct ExpectedHelper {
  527. template <class Error, class T>
  528. static constexpr Expected<T, Error> return_(T t) {
  529. return folly::makeExpected<Error>(t);
  530. }
  531. template <
  532. class Error,
  533. class T,
  534. class U FOLLY_REQUIRES_TRAILING(
  535. expected_detail::IsConvertible<U&&, Error>::value)>
  536. static constexpr Expected<T, Error> return_(Expected<T, U> t) {
  537. return t;
  538. }
  539. template <class This>
  540. static typename std::decay<This>::type then_(This&& ex) {
  541. return static_cast<This&&>(ex);
  542. }
  543. FOLLY_PUSH_WARNING
  544. // Don't warn about not using the overloaded comma operator.
  545. FOLLY_MSVC_DISABLE_WARNING(4913)
  546. template <
  547. class This,
  548. class Fn,
  549. class... Fns,
  550. class E = ExpectedErrorType<This>,
  551. class T = ExpectedHelper>
  552. static auto then_(This&& ex, Fn&& fn, Fns&&... fns) -> decltype(T::then_(
  553. T::template return_<E>(
  554. (std::declval<Fn>()(std::declval<This>().value()), unit)),
  555. std::declval<Fns>()...)) {
  556. if (LIKELY(ex.which_ == expected_detail::Which::eValue)) {
  557. return T::then_(
  558. T::template return_<E>(
  559. // Uses the comma operator defined above IFF the lambda
  560. // returns non-void.
  561. (static_cast<Fn&&>(fn)(static_cast<This&&>(ex).value()), unit)),
  562. static_cast<Fns&&>(fns)...);
  563. }
  564. return makeUnexpected(static_cast<This&&>(ex).error());
  565. }
  566. template <
  567. class This,
  568. class Yes,
  569. class No,
  570. class Ret = decltype(std::declval<Yes>()(std::declval<This>().value())),
  571. class Err = decltype(std::declval<No>()(std::declval<This>().error()))
  572. FOLLY_REQUIRES_TRAILING(!std::is_void<Err>::value)>
  573. static Ret thenOrThrow_(This&& ex, Yes&& yes, No&& no) {
  574. if (LIKELY(ex.which_ == expected_detail::Which::eValue)) {
  575. return Ret(static_cast<Yes&&>(yes)(static_cast<This&&>(ex).value()));
  576. }
  577. throw_exception(static_cast<No&&>(no)(static_cast<This&&>(ex).error()));
  578. }
  579. template <
  580. class This,
  581. class Yes,
  582. class No,
  583. class Ret = decltype(std::declval<Yes>()(std::declval<This>().value())),
  584. class Err = decltype(std::declval<No>()(std::declval<This&>().error()))
  585. FOLLY_REQUIRES_TRAILING(std::is_void<Err>::value)>
  586. static Ret thenOrThrow_(This&& ex, Yes&& yes, No&& no) {
  587. if (LIKELY(ex.which_ == expected_detail::Which::eValue)) {
  588. return Ret(static_cast<Yes&&>(yes)(static_cast<This&&>(ex).value()));
  589. }
  590. static_cast<No&&>(no)(ex.error());
  591. typename Unexpected<ExpectedErrorType<This>>::MakeBadExpectedAccess bad;
  592. throw_exception(bad(static_cast<This&&>(ex).error()));
  593. }
  594. FOLLY_POP_WARNING
  595. };
  596. } // namespace expected_detail_ExpectedHelper
  597. /* using override */ using expected_detail_ExpectedHelper::ExpectedHelper;
  598. struct UnexpectedTag {};
  599. } // namespace expected_detail
  600. using unexpected_t =
  601. expected_detail::UnexpectedTag (&)(expected_detail::UnexpectedTag);
  602. inline expected_detail::UnexpectedTag unexpected(
  603. expected_detail::UnexpectedTag = {}) {
  604. return {};
  605. }
  606. /**
  607. * An exception type thrown by Expected on catastrophic logic errors.
  608. */
  609. class FOLLY_EXPORT BadExpectedAccess : public std::logic_error {
  610. public:
  611. BadExpectedAccess() : std::logic_error("bad Expected access") {}
  612. };
  613. namespace expected_detail {
  614. // empty
  615. } // namespace expected_detail
  616. /**
  617. * Unexpected - a helper type used to disambiguate the construction of
  618. * Expected objects in the error state.
  619. */
  620. template <class Error>
  621. class Unexpected final : ColdClass {
  622. template <class E>
  623. friend class Unexpected;
  624. template <class V, class E>
  625. friend class Expected;
  626. friend struct expected_detail::ExpectedHelper;
  627. public:
  628. /**
  629. * Unexpected::BadExpectedAccess - An exception type thrown by Expected
  630. * when the user tries to access the nested value but the Expected object is
  631. * actually storing an error code.
  632. */
  633. class FOLLY_EXPORT BadExpectedAccess : public folly::BadExpectedAccess {
  634. public:
  635. explicit BadExpectedAccess(Error err)
  636. : folly::BadExpectedAccess{}, error_(std::move(err)) {}
  637. /**
  638. * The error code that was held by the Expected object when the user
  639. * erroneously requested the value.
  640. */
  641. Error error() const {
  642. return error_;
  643. }
  644. private:
  645. Error error_;
  646. };
  647. /**
  648. * Constructors
  649. */
  650. Unexpected() = default;
  651. Unexpected(const Unexpected&) = default;
  652. Unexpected(Unexpected&&) = default;
  653. Unexpected& operator=(const Unexpected&) = default;
  654. Unexpected& operator=(Unexpected&&) = default;
  655. constexpr /* implicit */ Unexpected(const Error& err) : error_(err) {}
  656. constexpr /* implicit */ Unexpected(Error&& err) : error_(std::move(err)) {}
  657. template <class Other FOLLY_REQUIRES_TRAILING(
  658. std::is_constructible<Error, Other&&>::value)>
  659. constexpr /* implicit */ Unexpected(Unexpected<Other> that)
  660. : error_(std::move(that.error())) {}
  661. /**
  662. * Assignment
  663. */
  664. template <class Other FOLLY_REQUIRES_TRAILING(
  665. std::is_assignable<Error&, Other&&>::value)>
  666. Unexpected& operator=(Unexpected<Other> that) {
  667. error_ = std::move(that.error());
  668. }
  669. /**
  670. * Observers
  671. */
  672. Error& error() & {
  673. return error_;
  674. }
  675. const Error& error() const& {
  676. return error_;
  677. }
  678. Error&& error() && {
  679. return std::move(error_);
  680. }
  681. private:
  682. struct MakeBadExpectedAccess {
  683. template <class E>
  684. BadExpectedAccess operator()(E&& err) const {
  685. return BadExpectedAccess(static_cast<E&&>(err));
  686. }
  687. };
  688. Error error_;
  689. };
  690. template <
  691. class Error FOLLY_REQUIRES_TRAILING(IsEqualityComparable<Error>::value)>
  692. inline bool operator==(
  693. const Unexpected<Error>& lhs,
  694. const Unexpected<Error>& rhs) {
  695. return lhs.error() == rhs.error();
  696. }
  697. template <
  698. class Error FOLLY_REQUIRES_TRAILING(IsEqualityComparable<Error>::value)>
  699. inline bool operator!=(
  700. const Unexpected<Error>& lhs,
  701. const Unexpected<Error>& rhs) {
  702. return !(lhs == rhs);
  703. }
  704. /**
  705. * For constructing an Unexpected object from an error code. Unexpected objects
  706. * are implicitly convertible to Expected object in the error state. Usage is
  707. * as follows:
  708. *
  709. * enum class MyErrorCode { BAD_ERROR, WORSE_ERROR };
  710. * Expected<int, MyErrorCode> myAPI() {
  711. * int i = // ...;
  712. * return i ? makeExpected<MyErrorCode>(i)
  713. * : makeUnexpected(MyErrorCode::BAD_ERROR);
  714. * }
  715. */
  716. template <class Error>
  717. constexpr Unexpected<typename std::decay<Error>::type> makeUnexpected(
  718. Error&& err) {
  719. return Unexpected<typename std::decay<Error>::type>{
  720. static_cast<Error&&>(err)};
  721. }
  722. /**
  723. * Expected - For holding a value or an error. Useful as an alternative to
  724. * exceptions, for APIs where throwing on failure would be too expensive.
  725. *
  726. * Expected<Value, Error> is a variant over the types Value and Error.
  727. *
  728. * Expected does not offer support for references. Use
  729. * Expected<std::reference_wrapper<T>, Error> if your API needs to return a
  730. * reference or an error.
  731. *
  732. * Expected offers a continuation-based interface to reduce the boilerplate
  733. * of checking error codes. The Expected::then member function takes a lambda
  734. * that is to execute should the Expected object contain a value. The return
  735. * value of the lambda is wrapped in an Expected and returned. If the lambda is
  736. * not executed because the Expected contains an error, the error is returned
  737. * immediately in a new Expected object.
  738. *
  739. * Expected<int, Error> funcTheFirst();
  740. * Expected<std::string, Error> funcTheSecond() {
  741. * return funcTheFirst().then([](int i) { return std::to_string(i); });
  742. * }
  743. *
  744. * The above line of code could more verbosely written as:
  745. *
  746. * Expected<std::string, Error> funcTheSecond() {
  747. * if (auto ex = funcTheFirst()) {
  748. * return std::to_string(*ex);
  749. * }
  750. * return makeUnexpected(ex.error());
  751. * }
  752. *
  753. * Continuations can chain, like:
  754. *
  755. * Expected<D, Error> maybeD = someFunc()
  756. * .then([](A a){return B(a);})
  757. * .then([](B b){return C(b);})
  758. * .then([](C c){return D(c);});
  759. *
  760. * To avoid the redundant error checking that would happen if a call at the
  761. * front of the chain returns an error, these call chains can be collaped into
  762. * a single call to .then:
  763. *
  764. * Expected<D, Error> maybeD = someFunc()
  765. * .then([](A a){return B(a);},
  766. * [](B b){return C(b);},
  767. * [](C c){return D(c);});
  768. *
  769. * The result of .then() is wrapped into Expected< ~, Error > if it isn't
  770. * of that form already. Consider the following code:
  771. *
  772. * extern Expected<std::string, Error> readLineFromIO();
  773. * extern Expected<int, Error> parseInt(std::string);
  774. * extern int increment(int);
  775. *
  776. * Expected<int, Error> x = readLineFromIO().then(parseInt).then(increment);
  777. *
  778. * From the code above, we see that .then() works both with functions that
  779. * return an Expected< ~, Error > (like parseInt) and with ones that return
  780. * a plain value (like increment). In the case of parseInt, .then() returns
  781. * the result of parseInt as-is. In the case of increment, it wraps the int
  782. * that increment returns into an Expected< int, Error >.
  783. *
  784. * Sometimes when using a continuation you would prefer an exception to be
  785. * thrown for a value-less Expected. For that you can use .thenOrThrow, as
  786. * follows:
  787. *
  788. * B b = someFunc()
  789. * .thenOrThrow([](A a){return B(a);});
  790. *
  791. * The above call to thenOrThrow will invoke the lambda if the Expected returned
  792. * by someFunc() contains a value. Otherwise, it will throw an exception of type
  793. * Unexpected<Error>::BadExpectedAccess. If you prefer it throw an exception of
  794. * a different type, you can pass a second lambda to thenOrThrow:
  795. *
  796. * B b = someFunc()
  797. * .thenOrThrow([](A a){return B(a);},
  798. * [](Error e) {throw MyException(e);});
  799. *
  800. * Like C++17's std::variant, Expected offers the almost-never-empty guarantee;
  801. * that is, an Expected<Value, Error> almost always contains either a Value or
  802. * and Error. Partially-formed Expected objects occur when an assignment to
  803. * an Expected object that would change the type of the contained object (Value-
  804. * to-Error or vice versa) throws. Trying to access either the contained value
  805. * or error object causes Expected to throw folly::BadExpectedAccess.
  806. *
  807. * Expected models OptionalPointee, so calling 'get_pointer(ex)' will return a
  808. * pointer to nullptr if the 'ex' is in the error state, and a pointer to the
  809. * value otherwise:
  810. *
  811. * Expected<int, Error> maybeInt = ...;
  812. * if (int* v = get_pointer(maybeInt)) {
  813. * cout << *v << endl;
  814. * }
  815. */
  816. template <class Value, class Error>
  817. class Expected final : expected_detail::ExpectedStorage<Value, Error> {
  818. template <class, class>
  819. friend class Expected;
  820. template <class, class, expected_detail::StorageType>
  821. friend struct expected_detail::ExpectedStorage;
  822. friend struct expected_detail::ExpectedHelper;
  823. using Base = expected_detail::ExpectedStorage<Value, Error>;
  824. using MakeBadExpectedAccess =
  825. typename Unexpected<Error>::MakeBadExpectedAccess;
  826. Base& base() & {
  827. return *this;
  828. }
  829. const Base& base() const& {
  830. return *this;
  831. }
  832. Base&& base() && {
  833. return std::move(*this);
  834. }
  835. public:
  836. using value_type = Value;
  837. using error_type = Error;
  838. template <class U>
  839. using rebind = Expected<U, Error>;
  840. static_assert(
  841. !std::is_reference<Value>::value,
  842. "Expected may not be used with reference types");
  843. static_assert(
  844. !std::is_abstract<Value>::value,
  845. "Expected may not be used with abstract types");
  846. /*
  847. * Constructors
  848. */
  849. template <class B = Base, class = decltype(B{})>
  850. Expected() noexcept(noexcept(B{})) : Base{} {}
  851. Expected(const Expected& that) = default;
  852. Expected(Expected&& that) = default;
  853. template <
  854. class V,
  855. class E FOLLY_REQUIRES_TRAILING(
  856. !std::is_same<Expected<V, E>, Expected>::value &&
  857. std::is_constructible<Value, V&&>::value &&
  858. std::is_constructible<Error, E&&>::value)>
  859. Expected(Expected<V, E> that) : Base{expected_detail::EmptyTag{}} {
  860. *this = std::move(that);
  861. }
  862. FOLLY_REQUIRES(std::is_copy_constructible<Value>::value)
  863. constexpr /* implicit */ Expected(const Value& val) noexcept(
  864. noexcept(Value(val)))
  865. : Base{expected_detail::ValueTag{}, val} {}
  866. FOLLY_REQUIRES(std::is_move_constructible<Value>::value)
  867. constexpr /* implicit */ Expected(Value&& val) noexcept(
  868. noexcept(Value(std::move(val))))
  869. : Base{expected_detail::ValueTag{}, std::move(val)} {}
  870. template <class T FOLLY_REQUIRES_TRAILING(
  871. std::is_convertible<T, Value>::value &&
  872. !std::is_convertible<T, Error>::value)>
  873. constexpr /* implicit */ Expected(T&& val) noexcept(
  874. noexcept(Value(static_cast<T&&>(val))))
  875. : Base{expected_detail::ValueTag{}, static_cast<T&&>(val)} {}
  876. template <class... Ts FOLLY_REQUIRES_TRAILING(
  877. std::is_constructible<Value, Ts&&...>::value)>
  878. explicit constexpr Expected(in_place_t, Ts&&... ts) noexcept(
  879. noexcept(Value(std::declval<Ts>()...)))
  880. : Base{expected_detail::ValueTag{}, static_cast<Ts&&>(ts)...} {}
  881. template <
  882. class U,
  883. class... Ts FOLLY_REQUIRES_TRAILING(
  884. std::is_constructible<Value, std::initializer_list<U>&, Ts&&...>::
  885. value)>
  886. explicit constexpr Expected(
  887. in_place_t,
  888. std::initializer_list<U> il,
  889. Ts&&... ts) noexcept(noexcept(Value(std::declval<Ts>()...)))
  890. : Base{expected_detail::ValueTag{}, il, static_cast<Ts&&>(ts)...} {}
  891. // If overload resolution selects one of these deleted functions, that
  892. // means you need to use makeUnexpected
  893. /* implicit */ Expected(const Error&) = delete;
  894. /* implicit */ Expected(Error&&) = delete;
  895. FOLLY_REQUIRES(std::is_copy_constructible<Error>::value)
  896. constexpr Expected(unexpected_t, const Error& err) noexcept(
  897. noexcept(Error(err)))
  898. : Base{expected_detail::ErrorTag{}, err} {}
  899. FOLLY_REQUIRES(std::is_move_constructible<Error>::value)
  900. constexpr Expected(unexpected_t, Error&& err) noexcept(
  901. noexcept(Error(std::move(err))))
  902. : Base{expected_detail::ErrorTag{}, std::move(err)} {}
  903. FOLLY_REQUIRES(std::is_copy_constructible<Error>::value)
  904. constexpr /* implicit */ Expected(const Unexpected<Error>& err) noexcept(
  905. noexcept(Error(err.error())))
  906. : Base{expected_detail::ErrorTag{}, err.error()} {}
  907. FOLLY_REQUIRES(std::is_move_constructible<Error>::value)
  908. constexpr /* implicit */ Expected(Unexpected<Error>&& err) noexcept(
  909. noexcept(Error(std::move(err.error()))))
  910. : Base{expected_detail::ErrorTag{}, std::move(err.error())} {}
  911. /*
  912. * Assignment operators
  913. */
  914. Expected& operator=(const Expected& that) = default;
  915. Expected& operator=(Expected&& that) = default;
  916. template <
  917. class V,
  918. class E FOLLY_REQUIRES_TRAILING(
  919. !std::is_same<Expected<V, E>, Expected>::value &&
  920. expected_detail::IsConvertible<V&&, Value>::value &&
  921. expected_detail::IsConvertible<E&&, Error>::value)>
  922. Expected& operator=(Expected<V, E> that) {
  923. this->assign(std::move(that));
  924. return *this;
  925. }
  926. FOLLY_REQUIRES(expected_detail::IsCopyable<Value>::value)
  927. Expected& operator=(const Value& val) noexcept(
  928. expected_detail::IsNothrowCopyable<Value>::value) {
  929. this->assignValue(val);
  930. return *this;
  931. }
  932. FOLLY_REQUIRES(expected_detail::IsMovable<Value>::value)
  933. Expected& operator=(Value&& val) noexcept(
  934. expected_detail::IsNothrowMovable<Value>::value) {
  935. this->assignValue(std::move(val));
  936. return *this;
  937. }
  938. template <class T FOLLY_REQUIRES_TRAILING(
  939. std::is_convertible<T, Value>::value &&
  940. !std::is_convertible<T, Error>::value)>
  941. Expected& operator=(T&& val) {
  942. this->assignValue(static_cast<T&&>(val));
  943. return *this;
  944. }
  945. FOLLY_REQUIRES(expected_detail::IsCopyable<Error>::value)
  946. Expected& operator=(const Unexpected<Error>& err) noexcept(
  947. expected_detail::IsNothrowCopyable<Error>::value) {
  948. this->assignError(err.error());
  949. return *this;
  950. }
  951. FOLLY_REQUIRES(expected_detail::IsMovable<Error>::value)
  952. Expected& operator=(Unexpected<Error>&& err) noexcept(
  953. expected_detail::IsNothrowMovable<Error>::value) {
  954. this->assignError(std::move(err.error()));
  955. return *this;
  956. }
  957. // Used only when an Expected is used with coroutines on MSVC
  958. /* implicit */ Expected(const expected_detail::PromiseReturn<Value, Error>& p)
  959. : Expected{} {
  960. p.promise_->value_ = this;
  961. }
  962. template <class... Ts FOLLY_REQUIRES_TRAILING(
  963. std::is_constructible<Value, Ts&&...>::value)>
  964. void emplace(Ts&&... ts) {
  965. this->assignValue(static_cast<Ts&&>(ts)...);
  966. }
  967. /**
  968. * swap
  969. */
  970. void swap(Expected& that) noexcept(
  971. expected_detail::StrictAllOf<IsNothrowSwappable, Value, Error>::value) {
  972. if (this->uninitializedByException() || that.uninitializedByException()) {
  973. throw_exception<BadExpectedAccess>();
  974. }
  975. using std::swap;
  976. if (*this) {
  977. if (that) {
  978. swap(this->value_, that.value_);
  979. } else {
  980. Error e(std::move(that.error_));
  981. that.assignValue(std::move(this->value_));
  982. this->assignError(std::move(e));
  983. }
  984. } else {
  985. if (!that) {
  986. swap(this->error_, that.error_);
  987. } else {
  988. Error e(std::move(this->error_));
  989. this->assignValue(std::move(that.value_));
  990. that.assignError(std::move(e));
  991. }
  992. }
  993. }
  994. // If overload resolution selects one of these deleted functions, that
  995. // means you need to use makeUnexpected
  996. /* implicit */ Expected& operator=(const Error&) = delete;
  997. /* implicit */ Expected& operator=(Error&&) = delete;
  998. /**
  999. * Relational Operators
  1000. */
  1001. template <class Val, class Err>
  1002. friend typename std::enable_if<IsEqualityComparable<Val>::value, bool>::type
  1003. operator==(const Expected<Val, Err>& lhs, const Expected<Val, Err>& rhs);
  1004. template <class Val, class Err>
  1005. friend typename std::enable_if<IsLessThanComparable<Val>::value, bool>::type
  1006. operator<(const Expected<Val, Err>& lhs, const Expected<Val, Err>& rhs);
  1007. /*
  1008. * Accessors
  1009. */
  1010. constexpr bool hasValue() const noexcept {
  1011. return LIKELY(expected_detail::Which::eValue == this->which_);
  1012. }
  1013. constexpr bool hasError() const noexcept {
  1014. return UNLIKELY(expected_detail::Which::eError == this->which_);
  1015. }
  1016. using Base::uninitializedByException;
  1017. const Value& value() const& {
  1018. requireValue();
  1019. return this->Base::value();
  1020. }
  1021. Value& value() & {
  1022. requireValue();
  1023. return this->Base::value();
  1024. }
  1025. Value&& value() && {
  1026. requireValue();
  1027. return std::move(this->Base::value());
  1028. }
  1029. const Error& error() const& {
  1030. requireError();
  1031. return this->Base::error();
  1032. }
  1033. Error& error() & {
  1034. requireError();
  1035. return this->Base::error();
  1036. }
  1037. Error&& error() && {
  1038. requireError();
  1039. return std::move(this->Base::error());
  1040. }
  1041. // Return a copy of the value if set, or a given default if not.
  1042. template <class U>
  1043. Value value_or(U&& dflt) const& {
  1044. if (LIKELY(this->which_ == expected_detail::Which::eValue)) {
  1045. return this->value_;
  1046. }
  1047. return static_cast<U&&>(dflt);
  1048. }
  1049. template <class U>
  1050. Value value_or(U&& dflt) && {
  1051. if (LIKELY(this->which_ == expected_detail::Which::eValue)) {
  1052. return std::move(this->value_);
  1053. }
  1054. return static_cast<U&&>(dflt);
  1055. }
  1056. explicit constexpr operator bool() const noexcept {
  1057. return hasValue();
  1058. }
  1059. const Value& operator*() const& {
  1060. return this->value();
  1061. }
  1062. Value& operator*() & {
  1063. return this->value();
  1064. }
  1065. Value&& operator*() && {
  1066. return std::move(this->value());
  1067. }
  1068. const Value* operator->() const {
  1069. return std::addressof(this->value());
  1070. }
  1071. Value* operator->() {
  1072. return std::addressof(this->value());
  1073. }
  1074. const Value* get_pointer() const& noexcept {
  1075. return hasValue() ? std::addressof(this->value_) : nullptr;
  1076. }
  1077. Value* get_pointer() & noexcept {
  1078. return hasValue() ? std::addressof(this->value_) : nullptr;
  1079. }
  1080. Value* get_pointer() && = delete;
  1081. /**
  1082. * then
  1083. */
  1084. template <class... Fns FOLLY_REQUIRES_TRAILING(sizeof...(Fns) >= 1)>
  1085. auto then(Fns&&... fns) const& -> decltype(
  1086. expected_detail::ExpectedHelper::then_(
  1087. std::declval<const Base&>(),
  1088. std::declval<Fns>()...)) {
  1089. if (this->uninitializedByException()) {
  1090. throw_exception<BadExpectedAccess>();
  1091. }
  1092. return expected_detail::ExpectedHelper::then_(
  1093. base(), static_cast<Fns&&>(fns)...);
  1094. }
  1095. template <class... Fns FOLLY_REQUIRES_TRAILING(sizeof...(Fns) >= 1)>
  1096. auto then(Fns&&... fns) & -> decltype(expected_detail::ExpectedHelper::then_(
  1097. std::declval<Base&>(),
  1098. std::declval<Fns>()...)) {
  1099. if (this->uninitializedByException()) {
  1100. throw_exception<BadExpectedAccess>();
  1101. }
  1102. return expected_detail::ExpectedHelper::then_(
  1103. base(), static_cast<Fns&&>(fns)...);
  1104. }
  1105. template <class... Fns FOLLY_REQUIRES_TRAILING(sizeof...(Fns) >= 1)>
  1106. auto then(Fns&&... fns) && -> decltype(expected_detail::ExpectedHelper::then_(
  1107. std::declval<Base&&>(),
  1108. std::declval<Fns>()...)) {
  1109. if (this->uninitializedByException()) {
  1110. throw_exception<BadExpectedAccess>();
  1111. }
  1112. return expected_detail::ExpectedHelper::then_(
  1113. std::move(base()), static_cast<Fns&&>(fns)...);
  1114. }
  1115. /**
  1116. * thenOrThrow
  1117. */
  1118. template <class Yes, class No = MakeBadExpectedAccess>
  1119. auto thenOrThrow(Yes&& yes, No&& no = No{}) const& -> decltype(
  1120. std::declval<Yes>()(std::declval<const Value&>())) {
  1121. using Ret = decltype(std::declval<Yes>()(std::declval<const Value&>()));
  1122. if (this->uninitializedByException()) {
  1123. throw_exception<BadExpectedAccess>();
  1124. }
  1125. return Ret(expected_detail::ExpectedHelper::thenOrThrow_(
  1126. base(), static_cast<Yes&&>(yes), static_cast<No&&>(no)));
  1127. }
  1128. template <class Yes, class No = MakeBadExpectedAccess>
  1129. auto thenOrThrow(Yes&& yes, No&& no = No{}) & -> decltype(
  1130. std::declval<Yes>()(std::declval<Value&>())) {
  1131. using Ret = decltype(std::declval<Yes>()(std::declval<Value&>()));
  1132. if (this->uninitializedByException()) {
  1133. throw_exception<BadExpectedAccess>();
  1134. }
  1135. return Ret(expected_detail::ExpectedHelper::thenOrThrow_(
  1136. base(), static_cast<Yes&&>(yes), static_cast<No&&>(no)));
  1137. }
  1138. template <class Yes, class No = MakeBadExpectedAccess>
  1139. auto thenOrThrow(Yes&& yes, No&& no = No{}) && -> decltype(
  1140. std::declval<Yes>()(std::declval<Value&&>())) {
  1141. using Ret = decltype(std::declval<Yes>()(std::declval<Value&&>()));
  1142. if (this->uninitializedByException()) {
  1143. throw_exception<BadExpectedAccess>();
  1144. }
  1145. return Ret(expected_detail::ExpectedHelper::thenOrThrow_(
  1146. std::move(base()), static_cast<Yes&&>(yes), static_cast<No&&>(no)));
  1147. }
  1148. private:
  1149. void requireValue() const {
  1150. if (UNLIKELY(!hasValue())) {
  1151. if (LIKELY(hasError())) {
  1152. using Err = typename Unexpected<Error>::BadExpectedAccess;
  1153. throw_exception<Err>(this->error_);
  1154. }
  1155. throw_exception<BadExpectedAccess>();
  1156. }
  1157. }
  1158. void requireError() const {
  1159. if (UNLIKELY(!hasError())) {
  1160. throw_exception<BadExpectedAccess>();
  1161. }
  1162. }
  1163. expected_detail::Which which() const noexcept {
  1164. return this->which_;
  1165. }
  1166. };
  1167. template <class Value, class Error>
  1168. inline typename std::enable_if<IsEqualityComparable<Value>::value, bool>::type
  1169. operator==(
  1170. const Expected<Value, Error>& lhs,
  1171. const Expected<Value, Error>& rhs) {
  1172. if (UNLIKELY(lhs.uninitializedByException())) {
  1173. throw_exception<BadExpectedAccess>();
  1174. }
  1175. if (UNLIKELY(lhs.which_ != rhs.which_)) {
  1176. return false;
  1177. }
  1178. if (UNLIKELY(lhs.hasError())) {
  1179. return true; // All error states are considered equal
  1180. }
  1181. return lhs.value_ == rhs.value_;
  1182. }
  1183. template <
  1184. class Value,
  1185. class Error FOLLY_REQUIRES_TRAILING(IsEqualityComparable<Value>::value)>
  1186. inline bool operator!=(
  1187. const Expected<Value, Error>& lhs,
  1188. const Expected<Value, Error>& rhs) {
  1189. return !(rhs == lhs);
  1190. }
  1191. template <class Value, class Error>
  1192. inline typename std::enable_if<IsLessThanComparable<Value>::value, bool>::type
  1193. operator<(
  1194. const Expected<Value, Error>& lhs,
  1195. const Expected<Value, Error>& rhs) {
  1196. if (UNLIKELY(
  1197. lhs.uninitializedByException() || rhs.uninitializedByException())) {
  1198. throw_exception<BadExpectedAccess>();
  1199. }
  1200. if (UNLIKELY(lhs.hasError())) {
  1201. return !rhs.hasError();
  1202. }
  1203. if (UNLIKELY(rhs.hasError())) {
  1204. return false;
  1205. }
  1206. return lhs.value_ < rhs.value_;
  1207. }
  1208. template <
  1209. class Value,
  1210. class Error FOLLY_REQUIRES_TRAILING(IsLessThanComparable<Value>::value)>
  1211. inline bool operator<=(
  1212. const Expected<Value, Error>& lhs,
  1213. const Expected<Value, Error>& rhs) {
  1214. return !(rhs < lhs);
  1215. }
  1216. template <
  1217. class Value,
  1218. class Error FOLLY_REQUIRES_TRAILING(IsLessThanComparable<Value>::value)>
  1219. inline bool operator>(
  1220. const Expected<Value, Error>& lhs,
  1221. const Expected<Value, Error>& rhs) {
  1222. return rhs < lhs;
  1223. }
  1224. template <
  1225. class Value,
  1226. class Error FOLLY_REQUIRES_TRAILING(IsLessThanComparable<Value>::value)>
  1227. inline bool operator>=(
  1228. const Expected<Value, Error>& lhs,
  1229. const Expected<Value, Error>& rhs) {
  1230. return !(lhs < rhs);
  1231. }
  1232. /**
  1233. * swap Expected values
  1234. */
  1235. template <class Value, class Error>
  1236. void swap(Expected<Value, Error>& lhs, Expected<Value, Error>& rhs) noexcept(
  1237. expected_detail::StrictAllOf<IsNothrowSwappable, Value, Error>::value) {
  1238. lhs.swap(rhs);
  1239. }
  1240. template <class Value, class Error>
  1241. const Value* get_pointer(const Expected<Value, Error>& ex) noexcept {
  1242. return ex.get_pointer();
  1243. }
  1244. template <class Value, class Error>
  1245. Value* get_pointer(Expected<Value, Error>& ex) noexcept {
  1246. return ex.get_pointer();
  1247. }
  1248. /**
  1249. * For constructing an Expected object from a value, with the specified
  1250. * Error type. Usage is as follows:
  1251. *
  1252. * enum MyErrorCode { BAD_ERROR, WORSE_ERROR };
  1253. * Expected<int, MyErrorCode> myAPI() {
  1254. * int i = // ...;
  1255. * return i ? makeExpected<MyErrorCode>(i) : makeUnexpected(BAD_ERROR);
  1256. * }
  1257. */
  1258. template <class Error, class Value>
  1259. constexpr Expected<typename std::decay<Value>::type, Error> makeExpected(
  1260. Value&& val) {
  1261. return Expected<typename std::decay<Value>::type, Error>{
  1262. in_place, static_cast<Value&&>(val)};
  1263. }
  1264. // Suppress comparability of Optional<T> with T, despite implicit conversion.
  1265. template <class Value, class Error>
  1266. bool operator==(const Expected<Value, Error>&, const Value& other) = delete;
  1267. template <class Value, class Error>
  1268. bool operator!=(const Expected<Value, Error>&, const Value& other) = delete;
  1269. template <class Value, class Error>
  1270. bool operator<(const Expected<Value, Error>&, const Value& other) = delete;
  1271. template <class Value, class Error>
  1272. bool operator<=(const Expected<Value, Error>&, const Value& other) = delete;
  1273. template <class Value, class Error>
  1274. bool operator>=(const Expected<Value, Error>&, const Value& other) = delete;
  1275. template <class Value, class Error>
  1276. bool operator>(const Expected<Value, Error>&, const Value& other) = delete;
  1277. template <class Value, class Error>
  1278. bool operator==(const Value& other, const Expected<Value, Error>&) = delete;
  1279. template <class Value, class Error>
  1280. bool operator!=(const Value& other, const Expected<Value, Error>&) = delete;
  1281. template <class Value, class Error>
  1282. bool operator<(const Value& other, const Expected<Value, Error>&) = delete;
  1283. template <class Value, class Error>
  1284. bool operator<=(const Value& other, const Expected<Value, Error>&) = delete;
  1285. template <class Value, class Error>
  1286. bool operator>=(const Value& other, const Expected<Value, Error>&) = delete;
  1287. template <class Value, class Error>
  1288. bool operator>(const Value& other, const Expected<Value, Error>&) = delete;
  1289. } // namespace folly
  1290. #if defined(__GNUC__) && !defined(__clang__)
  1291. #pragma GCC diagnostic pop
  1292. #endif
  1293. #undef FOLLY_REQUIRES
  1294. #undef FOLLY_REQUIRES_TRAILING
  1295. // Enable the use of folly::Expected with `co_await`
  1296. // Inspired by https://github.com/toby-allsopp/coroutine_monad
  1297. #if FOLLY_HAS_COROUTINES
  1298. #include <experimental/coroutine>
  1299. namespace folly {
  1300. namespace expected_detail {
  1301. template <typename Value, typename Error>
  1302. struct Promise;
  1303. template <typename Value, typename Error>
  1304. struct PromiseReturn {
  1305. Optional<Expected<Value, Error>> storage_;
  1306. Promise<Value, Error>* promise_;
  1307. /* implicit */ PromiseReturn(Promise<Value, Error>& promise) noexcept
  1308. : promise_(&promise) {
  1309. promise_->value_ = &storage_;
  1310. }
  1311. PromiseReturn(PromiseReturn&& that) noexcept
  1312. : PromiseReturn{*that.promise_} {}
  1313. ~PromiseReturn() {}
  1314. /* implicit */ operator Expected<Value, Error>() & {
  1315. return std::move(*storage_);
  1316. }
  1317. };
  1318. template <typename Value, typename Error>
  1319. struct Promise {
  1320. Optional<Expected<Value, Error>>* value_ = nullptr;
  1321. Promise() = default;
  1322. Promise(Promise const&) = delete;
  1323. // This should work regardless of whether the compiler generates:
  1324. // folly::Expected<Value, Error> retobj{ p.get_return_object(); } // MSVC
  1325. // or:
  1326. // auto retobj = p.get_return_object(); // clang
  1327. PromiseReturn<Value, Error> get_return_object() noexcept {
  1328. return *this;
  1329. }
  1330. std::experimental::suspend_never initial_suspend() const noexcept {
  1331. return {};
  1332. }
  1333. std::experimental::suspend_never final_suspend() const {
  1334. return {};
  1335. }
  1336. template <typename U>
  1337. void return_value(U&& u) {
  1338. value_->emplace(static_cast<U&&>(u));
  1339. }
  1340. void unhandled_exception() {
  1341. // Technically, throwing from unhandled_exception is underspecified:
  1342. // https://github.com/GorNishanov/CoroutineWording/issues/17
  1343. throw;
  1344. }
  1345. };
  1346. template <typename Value, typename Error>
  1347. struct Awaitable {
  1348. Expected<Value, Error> o_;
  1349. explicit Awaitable(Expected<Value, Error> o) : o_(std::move(o)) {}
  1350. bool await_ready() const noexcept {
  1351. return o_.hasValue();
  1352. }
  1353. Value await_resume() {
  1354. return std::move(o_.value());
  1355. }
  1356. // Explicitly only allow suspension into a Promise
  1357. template <typename U>
  1358. void await_suspend(std::experimental::coroutine_handle<Promise<U, Error>> h) {
  1359. *h.promise().value_ = makeUnexpected(std::move(o_.error()));
  1360. // Abort the rest of the coroutine. resume() is not going to be called
  1361. h.destroy();
  1362. }
  1363. };
  1364. } // namespace expected_detail
  1365. template <typename Value, typename Error>
  1366. expected_detail::Awaitable<Value, Error>
  1367. /* implicit */ operator co_await(Expected<Value, Error> o) {
  1368. return expected_detail::Awaitable<Value, Error>{std::move(o)};
  1369. }
  1370. } // namespace folly
  1371. // This makes folly::Expected<Value> useable as a coroutine return type...
  1372. namespace std {
  1373. namespace experimental {
  1374. template <typename Value, typename Error, typename... Args>
  1375. struct coroutine_traits<folly::Expected<Value, Error>, Args...> {
  1376. using promise_type = folly::expected_detail::Promise<Value, Error>;
  1377. };
  1378. } // namespace experimental
  1379. } // namespace std
  1380. #endif // FOLLY_HAS_COROUTINES