Skip to content

Commit

Permalink
[Support][ErrorOr] Add support for implicit conversion from error cod…
Browse files Browse the repository at this point in the history
…e/condition enums.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@176228 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
Bigcheese committed Feb 28, 2013
1 parent 2b9a946 commit 9b4886e
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
26 changes: 26 additions & 0 deletions include/llvm/Support/ErrorOr.h
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,16 @@ class ErrorOr {
public:
ErrorOr() : IsValid(false) {}

template <class E>
ErrorOr(E ErrorCode, typename enable_if_c<is_error_code_enum<E>::value ||
is_error_condition_enum<E>::value,
void *>::type = 0)
: HasError(true), IsValid(true) {
Error = new ErrorHolderBase;
Error->Error = make_error_code(ErrorCode);
Error->HasUserData = false;
}

ErrorOr(llvm::error_code EC) : HasError(true), IsValid(true) {
Error = new ErrorHolderBase;
Error->Error = EC;
Expand Down Expand Up @@ -387,6 +397,22 @@ class ErrorOr<void> {
public:
ErrorOr() : Error(0, 0) {}

template <class E>
ErrorOr(E ErrorCode, typename enable_if_c<is_error_code_enum<E>::value ||
is_error_condition_enum<E>::value,
void *> ::type = 0)
: Error(0, 0) {
error_code EC = make_error_code(ErrorCode);
if (EC == errc::success) {
Error.setInt(1);
return;
}
ErrorHolderBase *EHB = new ErrorHolderBase;
EHB->Error = EC;
EHB->HasUserData = false;
Error.setPointer(EHB);
}

ErrorOr(llvm::error_code EC) : Error(0, 0) {
if (EC == errc::success) {
Error.setInt(1);
Expand Down
6 changes: 3 additions & 3 deletions unittests/Support/ErrorOrTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ using namespace llvm;
namespace {

ErrorOr<int> t1() {return 1;}
ErrorOr<int> t2() {return make_error_code(errc::invalid_argument);}
ErrorOr<int> t2() { return errc::invalid_argument; }

TEST(ErrorOr, SimpleValue) {
ErrorOr<int> a = t1();
Expand All @@ -45,8 +45,8 @@ TEST(ErrorOr, Types) {
*a = 42;
EXPECT_EQ(42, x);

EXPECT_FALSE(ErrorOr<void>(make_error_code(errc::broken_pipe)));
EXPECT_TRUE(ErrorOr<void>(make_error_code(errc::success)));
EXPECT_FALSE(ErrorOr<void>(errc::broken_pipe));
EXPECT_TRUE(ErrorOr<void>(errc::success));

#if LLVM_HAS_CXX11_STDLIB
// Move only types.
Expand Down

0 comments on commit 9b4886e

Please sign in to comment.