Outcome.h 1.7 KB

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