X-Git-Url: http://plrg.eecs.uci.edu/git/?a=blobdiff_plain;f=folly%2FOptional.h;h=16cf86a10090d75575749c34fbad902f4c47d83f;hb=49399f7cf36dfdda58e1bc01f2c9a1bc7a4ed400;hp=5350b2ee5e44d69c55868c0a96e2e24d286a16fb;hpb=c141f6ec365fcf87af33f7de9ad66d62520729c1;p=folly.git diff --git a/folly/Optional.h b/folly/Optional.h index 5350b2ee..16cf86a1 100644 --- a/folly/Optional.h +++ b/folly/Optional.h @@ -64,7 +64,14 @@ namespace folly { -namespace detail { struct NoneHelper {}; } +namespace detail { +struct NoneHelper {}; + +// Allow each translation unit to control its own -fexceptions setting. +// If exceptions are disabled, std::terminate() will be called instead of +// throwing OptionalEmptyException when the condition fails. +[[noreturn]] void throw_optional_empty_exception(); +} typedef int detail::NoneHelper::*None; @@ -252,7 +259,7 @@ class Optional { private: void require_value() const { if (!storage_.hasValue) { - throw OptionalEmptyException(); + detail::throw_optional_empty_exception(); } } @@ -352,57 +359,57 @@ Opt make_optional(T&& v) { /////////////////////////////////////////////////////////////////////////////// // Comparisons. -template -bool operator==(const Optional& a, const V& b) { +template +bool operator==(const Optional& a, const V& b) { return a.hasValue() && a.value() == b; } -template -bool operator!=(const Optional& a, const V& b) { +template +bool operator!=(const Optional& a, const V& b) { return !(a == b); } -template -bool operator==(const V& a, const Optional& b) { +template +bool operator==(const U& a, const Optional& b) { return b.hasValue() && b.value() == a; } -template -bool operator!=(const V& a, const Optional& b) { +template +bool operator!=(const U& a, const Optional& b) { return !(a == b); } -template -bool operator==(const Optional& a, const Optional& b) { +template +bool operator==(const Optional& a, const Optional& b) { if (a.hasValue() != b.hasValue()) { return false; } if (a.hasValue()) { return a.value() == b.value(); } return true; } -template -bool operator!=(const Optional& a, const Optional& b) { +template +bool operator!=(const Optional& a, const Optional& b) { return !(a == b); } -template -bool operator< (const Optional& a, const Optional& b) { +template +bool operator<(const Optional& a, const Optional& b) { if (a.hasValue() != b.hasValue()) { return a.hasValue() < b.hasValue(); } if (a.hasValue()) { return a.value() < b.value(); } return false; } -template -bool operator> (const Optional& a, const Optional& b) { +template +bool operator>(const Optional& a, const Optional& b) { return b < a; } -template -bool operator<=(const Optional& a, const Optional& b) { +template +bool operator<=(const Optional& a, const Optional& b) { return !(b < a); } -template -bool operator>=(const Optional& a, const Optional& b) { +template +bool operator>=(const Optional& a, const Optional& b) { return !(a < b); }