AWS SDK for C++

AWS SDK for C++ Version 1.11.440

Loading...
Searching...
No Matches
Outcome.h
1
6#pragma once
7
8#include <aws/core/Core_EXPORTS.h>
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 retryCount = o.retryCount;
105 }
106
107 return *this;
108 }
109
110 Outcome(Outcome&& o) : // Required to force Move Constructor
111 result(std::move(o.result)),
112 error(std::move(o.error)),
113 success(o.success),
114 retryCount(std::move(o.retryCount))
115 {
116 }
117
119 {
120 if (this != &o)
121 {
122 result = std::move(o.result);
123 error = std::move(o.error);
124 success = o.success;
125 retryCount = std::move(o.retryCount);
126 }
127
128 return *this;
129 }
130
131 inline const R& GetResult() const
132 {
133 return result;
134 }
135
136 inline R& GetResult()
137 {
138 return result;
139 }
140
146 {
147 return std::move(result);
148 }
149
150 inline const E& GetError() const
151 {
152 return error;
153 }
154
155 template<typename T>
156 inline T GetError()
157 {
158 return error.template GetModeledError<T>();
159 }
160
161 inline bool IsSuccess() const
162 {
163 return this->success;
164 }
165
169 inline unsigned int GetRetryCount() const { return retryCount; }
173 inline void SetRetryCount(const unsigned int iRetryCount) { retryCount = iRetryCount; }
174
175 private:
176 R result;
177 E error;
178 bool success = false;
179 unsigned int retryCount = 0;
180 };
181
182 } // namespace Utils
183} // namespace Aws
Outcome(const E &e)
Definition Outcome.h:35
Outcome & operator=(Outcome &&o)
Definition Outcome.h:118
bool IsSuccess() const
Definition Outcome.h:161
Outcome(Outcome< RT, ET > &&o)
Definition Outcome.h:65
Outcome(Outcome &&o)
Definition Outcome.h:110
Outcome(const R &r)
Definition Outcome.h:32
const E & GetError() const
Definition Outcome.h:150
const R & GetResult() const
Definition Outcome.h:131
R && GetResultWithOwnership()
Definition Outcome.h:145
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
void SetRetryCount(const unsigned int iRetryCount)
Definition Outcome.h:173
unsigned int GetRetryCount() const
Definition Outcome.h:169