Commit 7b7397fe authored by Yedidya Feldblum's avatar Yedidya Feldblum Committed by Facebook Github Bot

Use throw_exception in Expected

Summary: [Folly] Use `throw_exception` in `Expected`, replacing the one-off throwing functions and outlining `throw` statements.

Reviewed By: Orvid

Differential Revision: D7985812

fbshipit-source-id: 396b92d1ec40c909c9c37dd7f7d9ed94b2bcf709
parent 29fd73db
/*
* Copyright 2017-present Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <folly/Expected.h>
namespace folly {
namespace expected_detail {
// clang-format off
[[noreturn]] void throwBadExpectedAccess() {
throw BadExpectedAccess();
}
// clang-format on
} // namespace expected_detail
} // namespace folly
...@@ -38,6 +38,7 @@ ...@@ -38,6 +38,7 @@
#include <folly/Unit.h> #include <folly/Unit.h>
#include <folly/Utility.h> #include <folly/Utility.h>
#include <folly/lang/ColdClass.h> #include <folly/lang/ColdClass.h>
#include <folly/lang/Exception.h>
#define FOLLY_EXPECTED_ID(X) FB_CONCATENATE(FB_CONCATENATE(Folly, X), __LINE__) #define FOLLY_EXPECTED_ID(X) FB_CONCATENATE(FB_CONCATENATE(Folly, X), __LINE__)
...@@ -620,7 +621,7 @@ struct ExpectedHelper { ...@@ -620,7 +621,7 @@ struct ExpectedHelper {
if (LIKELY(ex.which_ == expected_detail::Which::eValue)) { if (LIKELY(ex.which_ == expected_detail::Which::eValue)) {
return Ret(static_cast<Yes&&>(yes)(static_cast<This&&>(ex).value())); return Ret(static_cast<Yes&&>(yes)(static_cast<This&&>(ex).value()));
} }
throw static_cast<No&&>(no)(static_cast<This&&>(ex).error()); throw_exception(static_cast<No&&>(no)(static_cast<This&&>(ex).error()));
} }
template < template <
...@@ -635,8 +636,8 @@ struct ExpectedHelper { ...@@ -635,8 +636,8 @@ struct ExpectedHelper {
return Ret(static_cast<Yes&&>(yes)(static_cast<This&&>(ex).value())); return Ret(static_cast<Yes&&>(yes)(static_cast<This&&>(ex).value()));
} }
static_cast<No&&>(no)(ex.error()); static_cast<No&&>(no)(ex.error());
throw typename Unexpected<ExpectedErrorType<This>>::MakeBadExpectedAccess()( typename Unexpected<ExpectedErrorType<This>>::MakeBadExpectedAccess bad;
static_cast<This&&>(ex).error()); throw_exception(bad(static_cast<This&&>(ex).error()));
} }
FOLLY_POP_WARNING FOLLY_POP_WARNING
}; };
...@@ -665,8 +666,6 @@ class FOLLY_EXPORT BadExpectedAccess : public std::logic_error { ...@@ -665,8 +666,6 @@ class FOLLY_EXPORT BadExpectedAccess : public std::logic_error {
namespace expected_detail { namespace expected_detail {
[[noreturn]] void throwBadExpectedAccess();
} // namespace expected_detail } // namespace expected_detail
/** /**
...@@ -1065,7 +1064,7 @@ class Expected final : expected_detail::ExpectedStorage<Value, Error> { ...@@ -1065,7 +1064,7 @@ class Expected final : expected_detail::ExpectedStorage<Value, Error> {
void swap(Expected& that) noexcept( void swap(Expected& that) noexcept(
expected_detail::StrictAllOf<IsNothrowSwappable, Value, Error>::value) { expected_detail::StrictAllOf<IsNothrowSwappable, Value, Error>::value) {
if (this->uninitializedByException() || that.uninitializedByException()) { if (this->uninitializedByException() || that.uninitializedByException()) {
expected_detail::throwBadExpectedAccess(); throw_exception<BadExpectedAccess>();
} }
using std::swap; using std::swap;
if (*this) { if (*this) {
...@@ -1205,7 +1204,7 @@ class Expected final : expected_detail::ExpectedStorage<Value, Error> { ...@@ -1205,7 +1204,7 @@ class Expected final : expected_detail::ExpectedStorage<Value, Error> {
std::declval<const Base&>(), std::declval<const Base&>(),
std::declval<Fns>()...)) { std::declval<Fns>()...)) {
if (this->uninitializedByException()) { if (this->uninitializedByException()) {
expected_detail::throwBadExpectedAccess(); throw_exception<BadExpectedAccess>();
} }
return expected_detail::ExpectedHelper::then_( return expected_detail::ExpectedHelper::then_(
base(), static_cast<Fns&&>(fns)...); base(), static_cast<Fns&&>(fns)...);
...@@ -1216,7 +1215,7 @@ class Expected final : expected_detail::ExpectedStorage<Value, Error> { ...@@ -1216,7 +1215,7 @@ class Expected final : expected_detail::ExpectedStorage<Value, Error> {
std::declval<Base&>(), std::declval<Base&>(),
std::declval<Fns>()...)) { std::declval<Fns>()...)) {
if (this->uninitializedByException()) { if (this->uninitializedByException()) {
expected_detail::throwBadExpectedAccess(); throw_exception<BadExpectedAccess>();
} }
return expected_detail::ExpectedHelper::then_( return expected_detail::ExpectedHelper::then_(
base(), static_cast<Fns&&>(fns)...); base(), static_cast<Fns&&>(fns)...);
...@@ -1227,7 +1226,7 @@ class Expected final : expected_detail::ExpectedStorage<Value, Error> { ...@@ -1227,7 +1226,7 @@ class Expected final : expected_detail::ExpectedStorage<Value, Error> {
std::declval<Base&&>(), std::declval<Base&&>(),
std::declval<Fns>()...)) { std::declval<Fns>()...)) {
if (this->uninitializedByException()) { if (this->uninitializedByException()) {
expected_detail::throwBadExpectedAccess(); throw_exception<BadExpectedAccess>();
} }
return expected_detail::ExpectedHelper::then_( return expected_detail::ExpectedHelper::then_(
std::move(base()), static_cast<Fns&&>(fns)...); std::move(base()), static_cast<Fns&&>(fns)...);
...@@ -1241,7 +1240,7 @@ class Expected final : expected_detail::ExpectedStorage<Value, Error> { ...@@ -1241,7 +1240,7 @@ class Expected final : expected_detail::ExpectedStorage<Value, Error> {
std::declval<Yes>()(std::declval<const Value&>())) { std::declval<Yes>()(std::declval<const Value&>())) {
using Ret = decltype(std::declval<Yes>()(std::declval<const Value&>())); using Ret = decltype(std::declval<Yes>()(std::declval<const Value&>()));
if (this->uninitializedByException()) { if (this->uninitializedByException()) {
expected_detail::throwBadExpectedAccess(); throw_exception<BadExpectedAccess>();
} }
return Ret(expected_detail::ExpectedHelper::thenOrThrow_( return Ret(expected_detail::ExpectedHelper::thenOrThrow_(
base(), static_cast<Yes&&>(yes), static_cast<No&&>(no))); base(), static_cast<Yes&&>(yes), static_cast<No&&>(no)));
...@@ -1252,7 +1251,7 @@ class Expected final : expected_detail::ExpectedStorage<Value, Error> { ...@@ -1252,7 +1251,7 @@ class Expected final : expected_detail::ExpectedStorage<Value, Error> {
std::declval<Yes>()(std::declval<Value&>())) { std::declval<Yes>()(std::declval<Value&>())) {
using Ret = decltype(std::declval<Yes>()(std::declval<Value&>())); using Ret = decltype(std::declval<Yes>()(std::declval<Value&>()));
if (this->uninitializedByException()) { if (this->uninitializedByException()) {
expected_detail::throwBadExpectedAccess(); throw_exception<BadExpectedAccess>();
} }
return Ret(expected_detail::ExpectedHelper::thenOrThrow_( return Ret(expected_detail::ExpectedHelper::thenOrThrow_(
base(), static_cast<Yes&&>(yes), static_cast<No&&>(no))); base(), static_cast<Yes&&>(yes), static_cast<No&&>(no)));
...@@ -1263,7 +1262,7 @@ class Expected final : expected_detail::ExpectedStorage<Value, Error> { ...@@ -1263,7 +1262,7 @@ class Expected final : expected_detail::ExpectedStorage<Value, Error> {
std::declval<Yes>()(std::declval<Value&&>())) { std::declval<Yes>()(std::declval<Value&&>())) {
using Ret = decltype(std::declval<Yes>()(std::declval<Value&&>())); using Ret = decltype(std::declval<Yes>()(std::declval<Value&&>()));
if (this->uninitializedByException()) { if (this->uninitializedByException()) {
expected_detail::throwBadExpectedAccess(); throw_exception<BadExpectedAccess>();
} }
return Ret(expected_detail::ExpectedHelper::thenOrThrow_( return Ret(expected_detail::ExpectedHelper::thenOrThrow_(
std::move(base()), static_cast<Yes&&>(yes), static_cast<No&&>(no))); std::move(base()), static_cast<Yes&&>(yes), static_cast<No&&>(no)));
...@@ -1273,15 +1272,16 @@ class Expected final : expected_detail::ExpectedStorage<Value, Error> { ...@@ -1273,15 +1272,16 @@ class Expected final : expected_detail::ExpectedStorage<Value, Error> {
void requireValue() const { void requireValue() const {
if (UNLIKELY(!hasValue())) { if (UNLIKELY(!hasValue())) {
if (LIKELY(hasError())) { if (LIKELY(hasError())) {
throw typename Unexpected<Error>::BadExpectedAccess(this->error_); using Err = typename Unexpected<Error>::BadExpectedAccess;
throw_exception<Err>(this->error_);
} }
expected_detail::throwBadExpectedAccess(); throw_exception<BadExpectedAccess>();
} }
} }
void requireError() const { void requireError() const {
if (UNLIKELY(!hasError())) { if (UNLIKELY(!hasError())) {
expected_detail::throwBadExpectedAccess(); throw_exception<BadExpectedAccess>();
} }
} }
...@@ -1296,7 +1296,7 @@ operator==( ...@@ -1296,7 +1296,7 @@ operator==(
const Expected<Value, Error>& lhs, const Expected<Value, Error>& lhs,
const Expected<Value, Error>& rhs) { const Expected<Value, Error>& rhs) {
if (UNLIKELY(lhs.uninitializedByException())) { if (UNLIKELY(lhs.uninitializedByException())) {
expected_detail::throwBadExpectedAccess(); throw_exception<BadExpectedAccess>();
} }
if (UNLIKELY(lhs.which_ != rhs.which_)) { if (UNLIKELY(lhs.which_ != rhs.which_)) {
return false; return false;
...@@ -1323,7 +1323,7 @@ operator<( ...@@ -1323,7 +1323,7 @@ operator<(
const Expected<Value, Error>& rhs) { const Expected<Value, Error>& rhs) {
if (UNLIKELY( if (UNLIKELY(
lhs.uninitializedByException() || rhs.uninitializedByException())) { lhs.uninitializedByException() || rhs.uninitializedByException())) {
expected_detail::throwBadExpectedAccess(); throw_exception<BadExpectedAccess>();
} }
if (UNLIKELY(lhs.hasError())) { if (UNLIKELY(lhs.hasError())) {
return !rhs.hasError(); return !rhs.hasError();
......
...@@ -545,7 +545,6 @@ libfolly_la_SOURCES = \ ...@@ -545,7 +545,6 @@ libfolly_la_SOURCES = \
dynamic.cpp \ dynamic.cpp \
ExceptionWrapper.cpp \ ExceptionWrapper.cpp \
Executor.cpp \ Executor.cpp \
Expected.cpp \
File.cpp \ File.cpp \
FileUtil.cpp \ FileUtil.cpp \
FingerprintTables.cpp \ FingerprintTables.cpp \
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment