[llvm] r176228 - [Support][ErrorOr] Add support for implicit conversion from error code/condition enums.
Michael J. Spencer
bigcheesegs at gmail.com
Wed Feb 27 17:44:26 PST 2013
Author: mspencer
Date: Wed Feb 27 19:44:26 2013
New Revision: 176228
URL: http://llvm.org/viewvc/llvm-project?rev=176228&view=rev
Log:
[Support][ErrorOr] Add support for implicit conversion from error code/condition enums.
Modified:
llvm/trunk/include/llvm/Support/ErrorOr.h
llvm/trunk/unittests/Support/ErrorOrTest.cpp
Modified: llvm/trunk/include/llvm/Support/ErrorOr.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/ErrorOr.h?rev=176228&r1=176227&r2=176228&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Support/ErrorOr.h (original)
+++ llvm/trunk/include/llvm/Support/ErrorOr.h Wed Feb 27 19:44:26 2013
@@ -180,6 +180,16 @@ private:
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;
@@ -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);
Modified: llvm/trunk/unittests/Support/ErrorOrTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/Support/ErrorOrTest.cpp?rev=176228&r1=176227&r2=176228&view=diff
==============================================================================
--- llvm/trunk/unittests/Support/ErrorOrTest.cpp (original)
+++ llvm/trunk/unittests/Support/ErrorOrTest.cpp Wed Feb 27 19:44:26 2013
@@ -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();
@@ -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.
More information about the llvm-commits
mailing list