AWS SDK for C++
AWS SDK for C++
Loading...
Searching...
No Matches
Outcome.h
Go to the documentation of this file.
1
6#pragma once
7
9
10#include <cassert>
11#include <utility>
12
13namespace Aws
14{
15 namespace Utils
16 {
17
24 template<typename R, typename E> // Result, Error
25 class Outcome
26 {
27 public:
28
29 Outcome() : result(), error(), success(false)
30 {
31 }
32 Outcome(const R& r) : result(r), error(), success(true)
33 {
34 }
35 Outcome(const E& e) : result(), error(e), success(false)
36 {
37 }
38 Outcome(R&& r) : result(std::forward<R>(r)), error(), success(true)
39 {
40 }
41 Outcome(E&& e) : result(), error(std::forward<E>(e)), success(false)
42 {
43 }
44 Outcome(const Outcome& o) :
45 result(o.result),
46 error(o.error),
47 success(o.success)
48 {
49 }
50
51 template<typename RT, typename ET>
52 friend class Outcome;
53
54#if defined (__cplusplus) && __cplusplus > 201103L
55 template< bool B, class T = void >
56 using enable_if_t = std::enable_if_t<B, T>;
57#else
58 template< bool B, class T = void >
59 using enable_if_t = typename std::enable_if<B,T>::type;
60#endif
61
62 // Move both result and error from other type of outcome
63 template<typename RT, typename ET, enable_if_t<std::is_convertible<RT, R>::value &&
64 std::is_convertible<ET, E>::value, int> = 0>
66 result(std::move(o.result)),
67 error(std::move(o.error)),
68 success(o.success)
69 {
70 }
71
72 // Move result from other type of outcome
73 template<typename RT, typename ET, enable_if_t<std::is_convertible<RT, R>::value &&
74 !std::is_convertible<ET, E>::value, int> = 0>
76 result(std::move(o.result)),
77 success(o.success)
78 {
79 assert(o.success);
80 }
81
82 // Move error from other type of outcome
83 template<typename RT, typename ET, enable_if_t<!std::is_convertible<RT, R>::value &&
84 std::is_convertible<ET, E>::value, int> = 0>
86 error(std::move(o.error)),
87 success(o.success)
88 {
89 assert(!o.success);
90 }
91
92 template<typename ET, enable_if_t<std::is_convertible<ET, E>::value, int> = 0>
93 Outcome(ET&& e) : error(std::forward<ET>(e)), success(false)
94 {
95 }
96
98 {
99 if (this != &o)
100 {
101 result = o.result;
102 error = o.error;
103 success = o.success;
104 }
105
106 return *this;
107 }
108
109 Outcome(Outcome&& o) : // Required to force Move Constructor
110 result(std::move(o.result)),
111 error(std::move(o.error)),
112 success(o.success)
113 {
114 }
115
117 {
118 if (this != &o)
119 {
120 result = std::move(o.result);
121 error = std::move(o.error);
122 success = o.success;
123 }
124
125 return *this;
126 }
127
128 inline const R& GetResult() const
129 {
130 return result;
131 }
132
133 inline R& GetResult()
134 {
135 return result;
136 }
137
143 {
144 return std::move(result);
145 }
146
147 inline const E& GetError() const
148 {
149 return error;
150 }
151
152 template<typename T>
153 inline T GetError()
154 {
155 return error.template GetModeledError<T>();
156 }
157
158 inline bool IsSuccess() const
159 {
160 return this->success;
161 }
162
163 private:
164 R result;
165 E error;
166 bool success;
167 };
168
169 } // namespace Utils
170} // namespace Aws
Outcome(const E &e)
Definition: Outcome.h:35
Outcome & operator=(Outcome &&o)
Definition: Outcome.h:116
bool IsSuccess() const
Definition: Outcome.h:158
Outcome(Outcome< RT, ET > &&o)
Definition: Outcome.h:65
Outcome(Outcome &&o)
Definition: Outcome.h:109
Outcome(const R &r)
Definition: Outcome.h:32
const E & GetError() const
Definition: Outcome.h:147
const R & GetResult() const
Definition: Outcome.h:128
R && GetResultWithOwnership()
Definition: Outcome.h:142
Outcome(const Outcome &o)
Definition: Outcome.h:44
typename std::enable_if< B, T >::type enable_if_t
Definition: Outcome.h:59
Outcome & operator=(const Outcome &o)
Definition: Outcome.h:97
Outcome(ET &&e)
Definition: Outcome.h:93