X-Git-Url: http://plrg.eecs.uci.edu/git/?p=folly.git;a=blobdiff_plain;f=folly%2FTry-inl.h;h=711c1a0905278f3d56a705563604a86fc6f51d50;hp=ed85e3dd1235de3a94fd904c38f730d27362e03a;hb=b2ae6ce3b6fc237e85cc988ad4a47a8453250b88;hpb=347311500aa10d0524ac6db3987a94216e6d76ef diff --git a/folly/Try-inl.h b/folly/Try-inl.h index ed85e3dd..711c1a09 100644 --- a/folly/Try-inl.h +++ b/folly/Try-inl.h @@ -1,5 +1,5 @@ /* - * Copyright 2016 Facebook, Inc. + * Copyright 2017 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,8 +16,10 @@ #pragma once -#include +#include + #include +#include namespace folly { @@ -26,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_)); } } @@ -40,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()); } } @@ -56,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; } @@ -70,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_); } } @@ -85,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; } @@ -96,7 +95,7 @@ Try::~Try() { if (LIKELY(contains_ == Contains::VALUE)) { value_.~T(); } else if (UNLIKELY(contains_ == Contains::EXCEPTION)) { - e_.~unique_ptr(); + e_.~exception_wrapper(); } } @@ -118,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, @@ -174,10 +171,31 @@ makeTryWith(F&& f) { } } -template -std::tuple unwrapTryTuple(std::tuple...>&& ts) { - return detail::TryTuple::unwrap( - std::forward...>>(ts)); +namespace try_detail { + +/** + * Trait that removes the layer of Try abstractions from the passed in type + */ +template +struct RemoveTry; +template