HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
NET_Outcome.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: NET_Outcome.h
7  *
8  * COMMENTS:
9  *
10  *
11  */
12 
13 #ifndef __NET_OUTCOME_H__
14 #define __NET_OUTCOME_H__
15 
16 #include "NET_API.h"
17 
18 #include <UT/UT_BoostAsio.h>
19 #include <UT/UT_ErrorCode.h>
20 
21 #include <exception>
22 #include <variant>
23 
24 template <typename T, typename E>
26 {
27 using result_type = T&;
29 result_type operator()(const E& err) { throw std::system_error(err); }
30 result_type operator()(const std::exception_ptr& ptr) { std::rethrow_exception(ptr); }
31 };
32 
33 template <typename T, typename E>
35 {
36 using result_type = const E&;
37 result_type operator()(const T&) { throw std::logic_error("holds value not error."); }
38 result_type operator()(const E& e) { return e; }
39 result_type operator()(const std::exception_ptr& ptr) { std::rethrow_exception(ptr); }
40 };
41 
42 template <typename T, typename E>
44 {
45 using result_type = std::exception_ptr;
46 result_type operator()(const T&) { throw std::logic_error("holds exception not value."); }
47 result_type operator()(const E& e) { throw std::system_error(e); }
48 result_type operator()(const std::exception_ptr& ptr) { return ptr; }
49 };
50 
51 template <typename T, typename E = UT_ErrorCode>
53 {
54 public:
55  using value_type = T;
56  using error_type = E;
57  using exception_type = std::exception_ptr;
58 
59  NET_Outcome(T value) : myValue(std::move(value)) {}
60  NET_Outcome(const E& err) : myValue(err) {}
61  NET_Outcome(const std::exception_ptr& ptr) : myValue(ptr) {}
62 
63  bool hasValue() const { return std::holds_alternative<T>(myValue); }
64  bool hasError() const { return std::holds_alternative<E>(myValue); }
65  bool hasException() const { return std::holds_alternative<exception_type>(myValue); }
66 
67  T& value() &
68  {
69  return std::visit(net_OutcomeValueVisit<T,E>(), myValue);
70  }
71  const T& value() const &
72  {
73  return std::visit(net_OutcomeValueVisit<const T,E>(), myValue);
74  }
75  T&& value() &&
76  {
77  return std::move(this->value());
78  }
79  const error_type& error() const
80  {
81  return std::visit(net_OutcomeErrorVisit<T,E>(), myValue);
82  }
84  {
85  return std::visit(net_OutcomeExceptionVisit<T,E>(), myValue);
86  }
87 
88 private:
89  std::variant<T, E, std::exception_ptr> myValue;
90 };
91 
92 #endif // __NET_OUTCOME_H__
93 
NET_Outcome(const E &err)
Definition: NET_Outcome.h:60
result_type operator()(const T &)
Definition: NET_Outcome.h:37
exception_type exceptionPtr() const
Definition: NET_Outcome.h:83
result_type operator()(const E &e)
Definition: NET_Outcome.h:47
NET_Outcome(const std::exception_ptr &ptr)
Definition: NET_Outcome.h:61
GLsizei const GLfloat * value
Definition: glcorearb.h:824
auto system_error(int error_code, format_string< T...> fmt, T &&...args) -> std::system_error
Definition: format.h:2240
result_type operator()(const T &)
Definition: NET_Outcome.h:46
bool hasValue() const
Definition: NET_Outcome.h:63
result_type operator()(const std::exception_ptr &ptr)
Definition: NET_Outcome.h:48
result_type operator()(const std::exception_ptr &ptr)
Definition: NET_Outcome.h:30
std::exception_ptr exception_type
Definition: NET_Outcome.h:57
NET_Outcome(T value)
Definition: NET_Outcome.h:59
result_type operator()(const E &err)
Definition: NET_Outcome.h:29
result_type operator()(const std::exception_ptr &ptr)
Definition: NET_Outcome.h:39
T & value()&
Definition: NET_Outcome.h:67
bool hasException() const
Definition: NET_Outcome.h:65
const error_type & error() const
Definition: NET_Outcome.h:79
const T & value() const &
Definition: NET_Outcome.h:71
auto ptr(T p) -> const void *
Definition: format.h:2448
Definition: core.h:1131
result_type operator()(const E &e)
Definition: NET_Outcome.h:38
#define const
Definition: zconf.h:214
std::exception_ptr result_type
Definition: NET_Outcome.h:45
result_type operator()(T &value)
Definition: NET_Outcome.h:28
bool hasError() const
Definition: NET_Outcome.h:64
T && value()&&
Definition: NET_Outcome.h:75