X-Git-Url: http://plrg.eecs.uci.edu/git/?a=blobdiff_plain;f=folly%2FTry-inl.h;h=801344d446553897736339df681763d180571669;hb=b50a3dd838801676f9f5a3d5f0b9a094a8175d72;hp=ba988e3dbc59a6b0615997fea025b3a99814974f;hpb=a56a988a8cd07c7e3250f27594b8f92c9274b30d;p=folly.git diff --git a/folly/Try-inl.h b/folly/Try-inl.h index ba988e3d..801344d4 100644 --- a/folly/Try-inl.h +++ b/folly/Try-inl.h @@ -1,5 +1,5 @@ /* - * Copyright 2016 Facebook, Inc. + * Copyright 2014-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. @@ -16,7 +16,10 @@ #pragma once +#include + #include +#include namespace folly { @@ -25,7 +28,7 @@ Try::Try(Try&& t) noexcept : contains_(t.contains_) { if (contains_ == Contains::VALUE) { new (&value_)T(std::move(t.value_)); } else if (contains_ == Contains::EXCEPTION) { - new (&e_)std::unique_ptr(std::move(t.e_)); + new (&e_) exception_wrapper(std::move(t.e_)); } } @@ -39,8 +42,7 @@ Try::Try(typename std::enable_if::value, new (&value_) T(); } else if (t.hasException()) { contains_ = Contains::EXCEPTION; - new (&e_) std::unique_ptr( - folly::make_unique(t.exception())); + new (&e_) exception_wrapper(t.exception()); } } @@ -55,7 +57,7 @@ Try& Try::operator=(Try&& t) noexcept { if (contains_ == Contains::VALUE) { new (&value_)T(std::move(t.value_)); } else if (contains_ == Contains::EXCEPTION) { - new (&e_)std::unique_ptr(std::move(t.e_)); + new (&e_) exception_wrapper(std::move(t.e_)); } return *this; } @@ -69,8 +71,7 @@ Try::Try(const Try& t) { if (contains_ == Contains::VALUE) { new (&value_)T(t.value_); } else if (contains_ == Contains::EXCEPTION) { - new (&e_)std::unique_ptr(); - e_ = folly::make_unique(*(t.e_)); + new (&e_) exception_wrapper(t.e_); } } @@ -84,8 +85,7 @@ Try& Try::operator=(const Try& t) { if (contains_ == Contains::VALUE) { new (&value_)T(t.value_); } else if (contains_ == Contains::EXCEPTION) { - new (&e_)std::unique_ptr(); - e_ = folly::make_unique(*(t.e_)); + new (&e_) exception_wrapper(t.e_); } return *this; } @@ -95,7 +95,7 @@ Try::~Try() { if (LIKELY(contains_ == Contains::VALUE)) { value_.~T(); } else if (UNLIKELY(contains_ == Contains::EXCEPTION)) { - e_.~unique_ptr(); + e_.~exception_wrapper(); } } @@ -117,32 +117,30 @@ const T& Try::value() const & { return value_; } +template +const T&& Try::value() const && { + throwIfFailed(); + return std::move(value_); +} + template void Try::throwIfFailed() const { - if (contains_ != Contains::VALUE) { - if (contains_ == Contains::EXCEPTION) { - e_->throwException(); - } else { - throw UsingUninitializedTry(); - } + switch (contains_) { + case Contains::VALUE: + return; + case Contains::EXCEPTION: + e_.throw_exception(); + default: + try_detail::throwUsingUninitializedTry(); } } void Try::throwIfFailed() const { if (!hasValue_) { - e_->throwException(); + e_.throw_exception(); } } -template -inline T moveFromTry(Try& t) { - return std::move(t.value()); -} - -inline void moveFromTry(Try& t) { - return t.value(); -} - template typename std::enable_if< !std::is_same::type, void>::value, @@ -173,4 +171,31 @@ makeTryWith(F&& f) { } } -} // folly +namespace try_detail { + +/** + * Trait that removes the layer of Try abstractions from the passed in type + */ +template +struct RemoveTry; +template