• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Example of Outcome used with error code enums
2 (C) 2017-2020 Niall Douglas <http://www.nedproductions.biz/> (3 commits)
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 //! [declaration]
35 struct udt
36 {
37   int a{0};
udtudt38   explicit udt(int _a)
39       : a(_a)
40   {
41   }
42   udt() = default;
operator *udt43   int operator*() const { return a; }
44 };
45 enum class err
46 {
47   success,
48   failure1,
49   failure2
50 };
51 
52 // Tell the standard library that enum err is an error code enum
53 // by specialising the is_error_code_enum trait. See
54 // http://en.cppreference.com/w/cpp/error/error_code/is_error_code_enum
55 namespace std
56 {
57   template <> struct is_error_code_enum<err> : std::true_type
58   {
59   };
60 }
61 
62 // We also must declare a free function make_error_code. This is
63 // discovered via ADL by the standard library. See
64 // http://en.cppreference.com/w/cpp/error/errc/make_error_code
make_error_code(err c)65 inline std::error_code make_error_code(err c)
66 {
67   // We need to inherit from std::error_category to define
68   // an error code domain with the standard library for
69   // our strongly typed enum. See
70   // http://en.cppreference.com/w/cpp/error/error_category
71   static struct err_category : std::error_category
72   {
73     virtual const char *name() const noexcept override final { return "err_category"; };
74     virtual std::string message(int c) const override final
75     {
76       switch(static_cast<err>(c))
77       {
78       case err::success:
79         return "err::success";
80       case err::failure1:
81         return "err::failure1";
82       case err::failure2:
83         return "err::failure2";
84       }
85       return "unknown";
86     }
87   } category;
88   return std::error_code(static_cast<int>(c), category);
89 }
90 //! [declaration]
91 
92 using namespace BOOST_OUTCOME_V2_NAMESPACE;
93 
main()94 int main()
95 {
96   //! [usage]
97   result<udt, err> res(err::failure1);
98 
99   // What happens here? What exception type is thrown?
100   try
101   {
102     std::cout << *res.value() << std::endl;
103   }
104   catch(const std::exception &e)
105   {
106     std::cerr << "Exception thrown was " << e.what() << std::endl;
107   }
108   //! [usage]
109   return 0;
110 }
111 
test()112 void test()
113 {
114   //! [usage2]
115   result<udt /*, std::error_code */> res(err::failure1);
116 
117   // What happens here? What exception type is thrown?
118   try
119   {
120     std::cout << *res.value() << std::endl;
121   }
122   catch(const std::exception &e)
123   {
124     // Prints "Exception thrown was failure1", exactly the same as before
125     std::cerr << "Exception thrown was " << e.what() << std::endl;
126   }
127   //! [usage2]
128 }
129 
130 //! [usage3]
boo()131 result<udt> boo()
132 {
133   return err::failure1;
134 }
foo()135 result<udt> foo()
136 {
137   BOOST_OUTCOME_TRY(v, (boo()));
138   return udt{5};  // emplace construct udt with 5
139 }
140 //! [usage3]
141