1 /* 2 * Copyright 2009-2017 Alibaba Cloud All rights reserved. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #pragma once 18 19 namespace AlibabaCloud 20 { 21 namespace OSS 22 { 23 24 template<typename E, typename R> 25 class Outcome 26 { 27 public: Outcome()28 Outcome():success_(false), e_(), r_() 29 { 30 } Outcome(const E & e)31 Outcome(const E& e) :success_(false), e_(e) 32 { 33 } Outcome(const R & r)34 Outcome(const R& r): success_(true), r_(r) 35 { 36 } Outcome(E && e)37 Outcome(E&& e) : success_(false), e_(std::forward<E>(e)) 38 { 39 } // Error move constructor Outcome(R && r)40 Outcome(R&& r) : success_(true), r_(std::forward<R>(r)) 41 { 42 } // Result move constructor Outcome(const Outcome & other)43 Outcome(const Outcome& other) : 44 success_(other.success_), 45 e_(other.e_), 46 r_(other.r_) 47 { 48 } Outcome(Outcome && other)49 Outcome(Outcome&& other): 50 success_(other.success_), 51 e_(std::move(other.e_)), 52 r_(std::move(other.r_)) 53 { 54 //*this = std::move(other); 55 } 56 Outcome& operator=(const Outcome& other) 57 { 58 if (this != &other) { 59 success_ = other.success_; 60 e_ = other.e_; 61 r_ = other.r_; 62 } 63 return *this; 64 } 65 Outcome& operator=(Outcome&& other) 66 { 67 if (this != &other) 68 { 69 success_ = other.success_; 70 r_ = std::move(other.r_); 71 e_ = std::move(other.e_); 72 } 73 return *this; 74 } 75 isSuccess()76 bool isSuccess()const { return success_; } error()77 const E& error()const { return e_; } result()78 const R& result()const { return r_; } error()79 E& error() { return e_; } result()80 R& result() { return r_; } 81 private: 82 bool success_; 83 E e_; 84 R r_; 85 }; 86 } 87 } 88