source: mainline/uspace/lib/cpp/include/internal/trycatch.hpp@ 0fe0f32

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

cpp: added missing forward declaration

  • Property mode set to 100644
File size: 4.4 KB
RevLine 
[e7970fe]1/*
2 * Copyright (c) 2018 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_INTERNAL_TRYCATCH
30#define LIBCPP_INTERNAL_TRYCATCH
31
32// TODO: This header should be included in every libcpp header.
33
34/**
35 * For the time that exception support is not present
36 * in HelenOS, we use mock macros in place of the keywords
37 * try, throw and catch that allow us to atleast partially
38 * mimic exception functionality (that is, without propagation
39 * and stack unwinding).
40 * The value of the macro below determines if the keyword
41 * hiding macros get defined.
42 */
43#define LIBCPP_EXCEPTIONS_SUPPORTED 0
44
45#if LIBCPP_EXCEPTIONS_SUPPORTED == 0
46
47namespace std
48{
49 /**
50 * In case the file where our macros get expanded
51 * does not include cstdlib.
52 */
53 extern "C" void abort(void) __attribute__((noreturn));
[46cf7bf]54 extern "C" int printf(const char*, ...);
[e7970fe]55
56 namespace aux
57 {
58 /**
59 * Monitors the state of the program with
60 * regards to exceptions.
61 */
62 extern bool exception_thrown;
63
64 inline constexpr bool try_blocks_allowed{true};
65 }
66}
67
68/**
69 * These macros allow us to choose how the program
70 * should behave when an exception is thrown
71 * (LIBCPP_EXCEPTION_HANDLE_THROW) or caught
72 * (LIBCPP_EXCEPTION_HANDLE_CATCH).
73 * We also provide three handlers that either
74 * hang the program (allowing us to read the
75 * message), exit the program (allowing us to
76 * redirect the message to some output file and
77 * end) or ignore the throw (in which case the
78 * state of program will be broken, but since
79 * we output messages on both and catch, this option
80 * might allow us to see which catch statement
81 * catches the "thrown exception" (supposing
82 * the program doesn't crash before reaching
83 * that statement.
84 */
85#define LIBCPP_EXCEPTION_HANG while (true);
86#define LIBCPP_EXCEPTION_ABORT ::std::abort();
87#define LIBCPP_EXCEPTION_IGNORE /* IGNORE */
88#define LIBCPP_EXCEPTION_HANDLE_THROW LIBCPP_EXCEPTION_IGNORE
[4529c4b]89#define LIBCPP_EXCEPTION_HANDLE_CATCH LIBCPP_EXCEPTION_HANG
[e7970fe]90
91#define try if constexpr (::std::aux::try_blocks_allowed)
92
93#define throw \
94 do {\
95 ::std::aux::exception_thrown = true; \
96 printf("[EXCEPTION] Thrown at %s:%d\n", __FILE__, __LINE__); \
97 LIBCPP_EXCEPTION_HANDLE_THROW \
[5df0491]98 } while (false);
[e7970fe]99
100#define catch(expr) \
101 if (::std::aux::exception_thrown) \
102 { \
103 printf("[EXCEPTION] Caught < "#expr" > at %s:%d\n", __FILE__, __LINE__); \
[4529c4b]104 ::std::aux::exception_thrown = false; \
[e7970fe]105 LIBCPP_EXCEPTION_HANDLE_CATCH \
106 } \
[4529c4b]107 if constexpr (false)
[e7970fe]108
[6fa83f0]109/**
110 * This macro can be used for testing the library. If
111 * exception handling is not available, it uses the
112 * internal bool variable and if it is, it uses a
113 * universal catch clause in which it sets the passed
114 * checking variable to true.
115 */
116#define LIBCPP_EXCEPTION_THROW_CHECK(variable) \
117 variable = ::std::aux::exception_thrown
118
119#else
120#define LIBCPP_EXCEPTION_THROW_CHECK(variable) \
121 catch (...) \
122 { \
123 variable = true; \
124 }
[e7970fe]125#endif
126
127#endif
Note: See TracBrowser for help on using the repository browser.