source: mainline/uspace/lib/cpp/include/impl/exception.hpp@ 1610aa35

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 1610aa35 was 1610aa35, checked in by Dzejrou <dzejrou@…>, 7 years ago

cpp: added <exception>

  • Property mode set to 100644
File size: 4.0 KB
Line 
1/*
2 * Copyright (c) 2017 Jaroslav Jindrak
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#ifndef LIBCPP_EXCEPTION
30#define LIBCPP_EXCEPTION
31
32#include <internal/trycatch.hpp>
33
34namespace std
35{
36 /**
37 * 18.8.1, class exception:
38 */
39
40 class exception
41 {
42 public:
43 exception() noexcept = default;
44 exception(const exception&) noexcept = default;
45 exception& operator=(const exception&) noexcept = default;
46 virtual ~exception() = default;
47 virtual const char* what() const noexcept;
48 };
49
50 /**
51 * 18.8.2, class bad_exception:
52 */
53
54 class bad_exception: public exception
55 {
56 public:
57 bad_exception() noexcept = default;
58 bad_exception(const bad_exception&) noexcept = default;
59 bad_exception& operator=(const bad_exception&) noexcept = default;
60
61 virtual const char* what() const noexcept;
62 };
63
64 /**
65 * 18.8.3, abnormal termination:
66 */
67
68 using terminate_handler = void (*)();
69
70 terminate_handler get_terminate() noexcept;
71 terminate_handler set_terminate(terminate_handler) noexcept;
72 [[noreturn]] void terminate() noexcept;
73
74 /**
75 * 18.8.4, uncaght_exceptions:
76 */
77
78 bool uncaught_exception() noexcept;
79 int uncaught_exceptions() noexcept;
80
81 using unexpected_handler = void (*)();
82
83 unexpected_handler get_unexpected() noexcept;
84 unexpected_handler set_unexpected(unexpected_handler) noexcept;
85 [[noreturn]] void unexpected();
86
87 /**
88 * 18.8.5, exception propagation:
89 */
90
91 namespace aux
92 {
93 class exception_ptr_t
94 { /* DUMMY BODY */ };
95 }
96
97 using exception_ptr = aux::exception_ptr_t;
98
99 exception_ptr current_exception() noexcept;
100 [[noreturn]] void rethrow_exception(exception_ptr);
101
102 template<class E>
103 exception_ptr make_exception_ptr(E e) noexcept
104 {
105 return exception_ptr{};
106 }
107
108 class nested_exception
109 {
110 public:
111 nested_exception() noexcept = default;
112 nested_exception(const nested_exception&) noexcept = default;
113 nested_exception& operator=(const nested_exception&) noexcept = default;
114 virtual ~nested_exception() = default;
115
116 [[noreturn]] void throw_nested() const;
117 exception_ptr nested_ptr() const noexcept;
118
119 private:
120 exception_ptr ptr_;
121 };
122
123 template<class E>
124 [[noreturn]] void throw_with_nested(E&& e)
125 {
126 terminate();
127 }
128
129 template<class E>
130 void rethrow_if_nested(const E& e)
131 {
132 auto casted = dynamic_cast<const nested_exception*>(&e);
133 if (casted)
134 casted->throw_nested();
135 }
136}
137
138#endif
139
Note: See TracBrowser for help on using the repository browser.