AsyncResult.h
Go to the documentation of this file.
1 /*--------------------------------------------------------------------------------------+
2 |
3 | Supplied under applicable software license agreement.
4 |
5 | Copyright (c) 2018 Bentley Systems, Incorporated. All rights reserved.
6 |
7 +---------------------------------------------------------------------------------------*/
8 #pragma once
9 
10 #include <Bentley/Tasks/Tasks.h>
11 #include <memory>
12 
14 
15 /*--------------------------------------------------------------------------------------+
16 * @bsiclass Bentley Systems
17 +---------------+---------------+---------------+---------------+---------------+------*/
18 template <typename ValueType, typename ErrorType>
21  {
22  template <typename AnyValue, typename AnyError> friend struct AsyncResult;
23 
24  private:
25  bool m_isSuccess;
26  mutable std::shared_ptr<ValueType> m_value;
27  mutable std::shared_ptr<ErrorType> m_error;
28 
29  protected:
30  AsyncResult (bool isSuccess, std::shared_ptr<ValueType> value, std::shared_ptr<ErrorType> error) :
31  m_isSuccess (isSuccess),
32  m_value (value),
33  m_error (error)
34  {
35  };
36 
37  public:
39  m_isSuccess (false)
40  {
41  };
42 
43  template<typename AnyValue>
45  {
46  return AsyncResult<ValueType, ErrorType> (false, nullptr, error.m_error);
47  }
48 
49  static AsyncResult<ValueType, ErrorType> Error (const ErrorType& error)
50  {
51  return AsyncResult<ValueType, ErrorType> (false, nullptr, std::make_shared<ErrorType> (error));
52  }
53 
54  static AsyncResult<ValueType, ErrorType> Error (const ErrorType& error, const ValueType& value)
55  {
56  return AsyncResult<ValueType, ErrorType> (false, std::make_shared<ValueType> (value), std::make_shared<ErrorType> (error));
57  }
58 
59  static AsyncResult<ValueType, ErrorType> Success (const ValueType& value)
60  {
61  return AsyncResult<ValueType, ErrorType> (true, std::make_shared<ValueType> (value), nullptr);
62  }
63 
65  {
66  m_isSuccess = true;
67  m_value = std::make_shared<ValueType> (value);
68  m_error = nullptr;
69  return *this;
70  };
71 
72  AsyncResult<ValueType, ErrorType>& SetError (const ErrorType& error)
73  {
74  m_isSuccess = false;
75  m_value = nullptr;
76  m_error = std::make_shared<ErrorType> (error);
77  return *this;
78  };
79 
80  AsyncResult<ValueType, ErrorType>& SetError (const ErrorType& error, const ValueType& value)
81  {
82  m_isSuccess = false;
83  m_value = std::make_shared<ValueType> (value);
84  m_error = std::make_shared<ErrorType> (error);
85  return *this;
86  };
87 
88  bool IsSuccess () const
89  {
90  return m_isSuccess;
91  };
92 
93  ValueType& GetValue ()
94  {
95  if (!m_value)
96  {
97  m_value = std::make_shared<ValueType> ();
98  }
99  return *m_value;
100  };
101 
102  const ValueType& GetValue () const
103  {
104  if (!m_value)
105  {
106  m_value = std::make_shared<ValueType> ();
107  }
108  return *m_value;
109  };
110 
111  ErrorType& GetError ()
112  {
113  if (!m_error)
114  {
115  m_error = std::make_shared<ErrorType> ();
116  }
117  return *m_error;
118  };
119 
120  const ErrorType& GetError () const
121  {
122  if (!m_error)
123  {
124  m_error = std::make_shared<ErrorType> ();
125  }
126  return *m_error;
127  };
128  };
129 
130 /*--------------------------------------------------------------------------------------+
131 * @bsiclass Bentley Systems
132 +---------------+---------------+---------------+---------------+---------------+------*/
133 template <typename ErrorType>
135 struct AsyncResult<void, ErrorType>
136  {
137  template <typename AnyValue, typename AnyError> friend struct AsyncResult;
138 
139  private:
140  bool m_isSuccess;
141  mutable std::shared_ptr<ErrorType> m_error;
142 
143  protected:
144  AsyncResult (bool isSuccess, std::shared_ptr<ErrorType> error) :
145  m_isSuccess (isSuccess),
146  m_error (error)
147  {
148  };
149 
150  public:
152  m_isSuccess (false)
153  {
154  };
155 
156  template<typename AnyValue>
158  {
159  return AsyncResult<void, ErrorType> (false, error.m_error);
160  }
161 
162  static AsyncResult<void, ErrorType> Error (const ErrorType& error)
163  {
164  return AsyncResult<void, ErrorType> (false, std::make_shared<ErrorType> (error));
165  }
166 
168  {
169  return AsyncResult<void, ErrorType> (true, nullptr);
170  }
171 
173  {
174  m_isSuccess = true;
175  m_error = nullptr;
176  return *this;
177  };
178 
179  AsyncResult<void, ErrorType>& SetError (const ErrorType& error)
180  {
181  m_isSuccess = false;
182  m_error = std::make_shared<ErrorType> (error);
183  return *this;
184  };
185 
186  bool IsSuccess () const
187  {
188  return m_isSuccess;
189  };
190 
191  ErrorType& GetError ()
192  {
193  if (!m_error)
194  {
195  m_error = std::make_shared<ErrorType> ();
196  }
197  return *m_error;
198  };
199 
200  const ErrorType& GetError () const
201  {
202  if (!m_error)
203  {
204  m_error = std::make_shared<ErrorType> ();
205  }
206  return *m_error;
207  };
208  };
209 
static AsyncResult< ValueType, ErrorType > Error(const ErrorType &error)
Definition: AsyncResult.h:49
const ErrorType & GetError() const
Definition: AsyncResult.h:120
AsyncResult()
Definition: AsyncResult.h:151
AsyncResult()
Definition: AsyncResult.h:38
static AsyncResult< ValueType, ErrorType > Error(const ErrorType &error, const ValueType &value)
Definition: AsyncResult.h:54
ErrorType & GetError()
Definition: AsyncResult.h:111
const ErrorType & GetError() const
Definition: AsyncResult.h:200
bool IsSuccess() const
Definition: AsyncResult.h:186
bool IsSuccess() const
Definition: AsyncResult.h:88
AsyncResult< void, ErrorType > & SetError(const ErrorType &error)
Definition: AsyncResult.h:179
#define BEGIN_BENTLEY_TASKS_NAMESPACE
Definition: Tasks.h:13
static AsyncResult< ValueType, ErrorType > Success(const ValueType &value)
Definition: AsyncResult.h:59
Class for returning success or Error from async operations.
Definition: AsyncResult.h:135
static AsyncResult< void, ErrorType > Success()
Definition: AsyncResult.h:167
AsyncResult(bool isSuccess, std::shared_ptr< ValueType > value, std::shared_ptr< ErrorType > error)
Definition: AsyncResult.h:30
static AsyncResult< void, ErrorType > Error(const ErrorType &error)
Definition: AsyncResult.h:162
const ValueType & GetValue() const
Definition: AsyncResult.h:102
AsyncResult< ValueType, ErrorType > & SetError(const ErrorType &error, const ValueType &value)
Definition: AsyncResult.h:80
AsyncResult< ValueType, ErrorType > & SetSuccess(const ValueType &value)
Definition: AsyncResult.h:64
AsyncResult(bool isSuccess, std::shared_ptr< ErrorType > error)
Definition: AsyncResult.h:144
static AsyncResult< void, ErrorType > Error(const AsyncResult< AnyValue, ErrorType > &error)
Definition: AsyncResult.h:157
ErrorType & GetError()
Definition: AsyncResult.h:191
AsyncResult< ValueType, ErrorType > & SetError(const ErrorType &error)
Definition: AsyncResult.h:72
ValueType & GetValue()
Definition: AsyncResult.h:93
#define END_BENTLEY_TASKS_NAMESPACE
Definition: Tasks.h:14
Class for returning success Value or Error from async operations.
Definition: AsyncResult.h:20
AsyncResult< void, ErrorType > & SetSuccess()
Definition: AsyncResult.h:172
static AsyncResult< ValueType, ErrorType > Error(const AsyncResult< AnyValue, ErrorType > &error)
Definition: AsyncResult.h:44

Copyright © 2017 Bentley Systems, Incorporated. All rights reserved.