AWS SDK for C++

AWS SDK for C++ Version 1.11.780

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