1 /* Example of Outcome try used with foreign object
2 (C) 2019-2020 Niall Douglas <http://www.nedproductions.biz/> (1 commit)
3
4
5 Boost Software License - Version 1.0 - August 17th, 2003
6
7 Permission is hereby granted, free of charge, to any person or organization
8 obtaining a copy of the software and accompanying documentation covered by
9 this license (the "Software") to use, reproduce, display, distribute,
10 execute, and transmit the Software, and to prepare derivative works of the
11 Software, and to permit third-parties to whom the Software is furnished to
12 do so, all subject to the following:
13
14 The copyright notices in the Software and this entire statement, including
15 the above license grant, this restriction and the following disclaimer,
16 must be included in all copies of the Software, in whole or in part, and
17 all derivative works of the Software, unless such copies or derivative
18 works are solely in the form of machine-executable object code generated by
19 a source language processor.
20
21 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
24 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
25 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
26 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27 DEALINGS IN THE SOFTWARE.
28 */
29
30 #include "../../../include/boost/outcome.hpp"
31
32 #include <iostream>
33
34 namespace outcome = BOOST_OUTCOME_V2_NAMESPACE;
35
36 //! [foreign_type]
37 enum Errc
38 {
39 kBadValue
40 };
41 template <class T, class E = Errc> struct ForeignExpected
42 {
43 T Value;
44 E Error;
45 int IsErrored;
46
ForeignExpectedForeignExpected47 ForeignExpected(T v)
48 : Value(v)
49 , Error()
50 , IsErrored(0)
51 {
52 }
ForeignExpectedForeignExpected53 ForeignExpected(E e)
54 : Value()
55 , Error(e)
56 , IsErrored(1)
57 {
58 }
59 };
60 //! [foreign_type]
61
62 //! [tell_outcome]
63 BOOST_OUTCOME_V2_NAMESPACE_BEGIN
64 template <class T, class E> //
try_operation_has_value(const ForeignExpected<T,E> & v)65 inline bool try_operation_has_value(const ForeignExpected<T, E> &v)
66 {
67 return 0 == v.IsErrored;
68 }
69 template <class T, class E> //
try_operation_return_as(const ForeignExpected<T,E> & v)70 inline auto try_operation_return_as(const ForeignExpected<T, E> &v)
71 {
72 switch(v.Error)
73 {
74 case kBadValue:
75 return failure(make_error_code(std::errc::argument_out_of_domain));
76 }
77 abort();
78 }
79 template <class T, class E> //
try_operation_extract_value(const ForeignExpected<T,E> & v)80 inline auto try_operation_extract_value(const ForeignExpected<T, E> &v)
81 {
82 return v.Value;
83 }
84 BOOST_OUTCOME_V2_NAMESPACE_END
85 //! [tell_outcome]
86
87 //! [functions]
old_code(int a)88 ForeignExpected<int> old_code(int a) // old code
89 {
90 if(0 == a)
91 return kBadValue;
92 return a;
93 }
94
new_code(int a)95 outcome::result<int> new_code(int a) // new code
96 {
97 BOOST_OUTCOME_TRY(x, old_code(a));
98 return x;
99 }
100 //! [functions]
101
main()102 int main()
103 {
104 //! [example]
105 auto printresult = [](const char *desc, auto x) {
106 if(x)
107 {
108 std::cout << desc << " returns successful " << x.value() << std::endl;
109 }
110 else
111 {
112 std::cout << desc << " returns failure " << x.error().message() << std::endl;
113 }
114 };
115 printresult("\nnew_code(5)", new_code(5));
116 printresult("\nnew_code(0)", new_code(0));
117 //! [example]
118 return 0;
119 }
120