AWS SDK for C++

AWS SDK for C++ Version 1.11.605

Loading...
Searching...
No Matches
Outcome.h
1
6#pragma once
7
8#include <aws/core/Core_EXPORTS.h>
9#include <aws/core/utils/logging/LogMacros.h>
10
11#include <cassert>
12#include <utility>
13
14namespace Aws
15{
16 namespace Utils
17 {
18 static const char OUTCOME_LOG_TAG[] = "Outcome";
19
26 template<typename R, typename E> // Result, Error
27 class Outcome
28 {
29 public:
30
31 Outcome() : result(), error(), success(false)
32 {
33 }
34 Outcome(const R& r) : result(r), error(), success(true)
35 {
36 }
37 Outcome(const E& e) : result(), error(e), success(false)
38 {
39 }
40 Outcome(R&& r) : result(std::forward<R>(r)), error(), success(true)
41 {
42 }
43 Outcome(E&& e) : result(), error(std::forward<E>(e)), success(false)
44 {
45 }
46 Outcome(const Outcome& o) :
47 result(o.result),
48 error(o.error),
49 success(o.success)
50 {
51 }
52
53 template<typename RT, typename ET>
54 friend class Outcome;
55
56#if defined (__cplusplus) && __cplusplus > 201103L
57 template< bool B, class T = void >
58 using enable_if_t = std::enable_if_t<B, T>;
59#else
60 template< bool B, class T = void >
61 using enable_if_t = typename std::enable_if<B,T>::type;
62#endif
63
64 // Move both result and error from other type of outcome
65 template<typename RT, typename ET, enable_if_t<std::is_convertible<RT, R>::value &&
66 std::is_convertible<ET, E>::value, int> = 0>
68 result(std::move(o.result)),
69 error(std::move(o.error)),
70 success(o.success)
71 {
72 }
73
74 // Move result from other type of outcome
75 template<typename RT, typename ET, enable_if_t<std::is_convertible<RT, R>::value &&
76 !std::is_convertible<ET, E>::value, int> = 0>
78 result(std::move(o.result)),
79 success(o.success)
80 {
81 assert(o.success);
82 }
83
84 // Move error from other type of outcome
85 template<typename RT, typename ET, enable_if_t<!std::is_convertible<RT, R>::value &&
86 std::is_convertible<ET, E>::value, int> = 0>
88 error(std::move(o.error)),
89 success(o.success)
90 {
91 assert(!o.success);
92 }
93
94 template<typename ET, enable_if_t<std::is_convertible<ET, E>::value, int> = 0>
95 Outcome(ET&& e) : error(std::forward<ET>(e)), success(false)
96 {
97 }
98
100 {
101 if (this != &o)
102 {
103 result = o.result;
104 error = o.error;
105 success = o.success;
106 retryCount = o.retryCount;
107 }
108
109 return *this;
110 }
111
112 Outcome(Outcome&& o) : // Required to force Move Constructor
113 result(std::move(o.result)),
114 error(std::move(o.error)),
115 success(o.success),
116 retryCount(std::move(o.retryCount))
117 {
118 }
119
121 {
122 if (this != &o)
123 {
124 result = std::move(o.result);
125 error = std::move(o.error);
126 success = o.success;
127 retryCount = std::move(o.retryCount);
128 }
129
130 return *this;
131 }
132
133 inline const R& GetResult() const
134 {
135 if (!success) {
136 AWS_LOGSTREAM_FATAL(OUTCOME_LOG_TAG, "GetResult called on a failed outcome! Result is not initialized!");
137 }
138 return result;
139 }
140
141 inline R& GetResult()
142 {
143 if (!success) {
144 AWS_LOGSTREAM_FATAL(OUTCOME_LOG_TAG, "GetResult called on a failed outcome! Result is not initialized!");
145 }
146 return result;
147 }
148
154 {
155 if (!success) {
156 AWS_LOGSTREAM_FATAL(OUTCOME_LOG_TAG, "GetResult called on a failed outcome! Result is not initialized!");
157 }
158 return std::move(result);
159 }
160
161 inline const E& GetError() const
162 {
163 if (success) {
164 AWS_LOGSTREAM_FATAL(OUTCOME_LOG_TAG, "GetError called on a success outcome! Error is not initialized!");
165 }
166 return error;
167 }
168
169 template<typename T>
170 inline T GetError()
171 {
172 if (success) {
173 AWS_LOGSTREAM_FATAL(OUTCOME_LOG_TAG, "GetError called on a success outcome! Error is not initialized!");
174 }
175 return error.template GetModeledError<T>();
176 }
177
178 inline bool IsSuccess() const
179 {
180 return this->success;
181 }
182
186 inline unsigned int GetRetryCount() const { return retryCount; }
190 inline void SetRetryCount(const unsigned int iRetryCount) { retryCount = iRetryCount; }
191
192 private:
193 R result;
194 E error;
195 bool success = false;
196 unsigned int retryCount = 0;
197 };
198
199 } // namespace Utils
200} // namespace Aws
Outcome(const E &e)
Definition Outcome.h:37
Outcome & operator=(Outcome &&o)
Definition Outcome.h:120
bool IsSuccess() const
Definition Outcome.h:178
Outcome(Outcome< RT, ET > &&o)
Definition Outcome.h:67
Outcome(Outcome &&o)
Definition Outcome.h:112
Outcome(const R &r)
Definition Outcome.h:34
const E & GetError() const
Definition Outcome.h:161
const R & GetResult() const
Definition Outcome.h:133
R && GetResultWithOwnership()
Definition Outcome.h:153
Outcome(const Outcome &o)
Definition Outcome.h:46
typename std::enable_if< B, T >::type enable_if_t
Definition Outcome.h:61
Outcome & operator=(const Outcome &o)
Definition Outcome.h:99
void SetRetryCount(const unsigned int iRetryCount)
Definition Outcome.h:190
unsigned int GetRetryCount() const
Definition Outcome.h:186
static const char OUTCOME_LOG_TAG[]
Definition Outcome.h:18