1 /*
2  * Copyright 1999-2019 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 #ifndef CORE_INCLUDE_ALIBABACLOUD_CORE_OUTCOME_H_
18 #define CORE_INCLUDE_ALIBABACLOUD_CORE_OUTCOME_H_
19 
20 #include <utility>
21 
22 namespace AlibabaCloud {
23 template <typename E, typename R> class Outcome {
24 public:
Outcome()25   Outcome() : success_(true), e_(), r_() {}
Outcome(const E & e)26   explicit Outcome(const E &e) : e_(e), success_(false), r_() {}
Outcome(const R & r)27   explicit Outcome(const R &r) : r_(r), success_(true), e_() {}
Outcome(const Outcome & other)28   Outcome(const Outcome &other)
29       : success_(other.success_), e_(other.e_), r_(other.r_) {}
Outcome(Outcome && other)30   Outcome(Outcome &&other) { *this = std::move(other); }
31   Outcome &operator=(const Outcome &other) {
32     if (this != &other) {
33       success_ = other.success_;
34       e_ = other.e_;
35       r_ = other.r_;
36     }
37     return *this;
38   }
39   Outcome &operator=(Outcome &&other) {
40     if (this != &other) {
41       success_ = other.success_;
42       r_ = other.r_;
43       e_ = other.e_;
44     }
45     return *this;
46   }
47 
isSuccess()48   bool isSuccess() const { return success_; }
error()49   E error() const { return e_; }
result()50   R result() const { return r_; }
51 
52 private:
53   bool success_;
54   E e_;
55   R r_;
56 };
57 } // namespace AlibabaCloud
58 #endif // CORE_INCLUDE_ALIBABACLOUD_CORE_OUTCOME_H_
59