The OpenD Programming Language

1 // Written in the D programming language.
2 
3 /**
4  * Interface to C++ <exception>
5  *
6  * Copyright: Copyright (c) 2016 D Language Foundation
7  * License:   $(HTTP boost.org/LICENSE_1_0.txt, Boost License 1.0).
8  * Authors:   $(HTTP digitalmars.com, Walter Bright)
9  *            Manu Evans
10  * Source:    $(DRUNTIMESRC core/stdcpp/_exception.d)
11  */
12 
13 module core.stdcpp.exception;
14 
15 // LDC: empty module for unsupported C++ runtimes
16 version (CppRuntime_Microsoft)  version = Supported;
17 else version (CppRuntime_Gcc)   version = Supported;
18 else version (CppRuntime_Clang) version = Supported;
19 version (Supported):
20 
21 import core.stdcpp.xutility : __cplusplus, CppStdRevision;
22 import core.attribute : weak;
23 
24 version (CppRuntime_Gcc)
25     version = GenericBaseException;
26 version (CppRuntime_Clang)
27     version = GenericBaseException;
28 version (CppRuntime_Sun)
29     version = GenericBaseException;
30 
31 extern (C++, "std"):
32 @nogc:
33 
34 ///
35 alias terminate_handler = void function() nothrow;
36 ///
37 terminate_handler set_terminate(terminate_handler f) nothrow;
38 ///
39 terminate_handler get_terminate() nothrow;
40 ///
41 void terminate() nothrow;
42 
43 static if (__cplusplus < CppStdRevision.cpp17)
44 {
45     ///
46     alias unexpected_handler = void function();
47     ///
48     deprecated unexpected_handler set_unexpected(unexpected_handler f) nothrow;
49     ///
50     deprecated unexpected_handler get_unexpected() nothrow;
51     ///
52     deprecated void unexpected();
53 }
54 
55 static if (__cplusplus < CppStdRevision.cpp17)
56 {
57     ///
58     bool uncaught_exception() nothrow;
59 }
60 else static if (__cplusplus == CppStdRevision.cpp17)
61 {
62     ///
63     deprecated bool uncaught_exception() nothrow;
64 }
65 static if (__cplusplus >= CppStdRevision.cpp17)
66 {
67     ///
68     int uncaught_exceptions() nothrow;
69 }
70 
71 version (GenericBaseException)
72 {
73     ///
74     class exception
75     {
76     @nogc:
77         ///
78         extern(D) this() nothrow {}
79         ///
80         @weak ~this() nothrow {} // HACK: this should extern, but then we have link errors!
81 
82         ///
83         @weak const(char)* what() const nothrow { return "unknown"; } // HACK: this should extern, but then we have link errors!
84 
85     protected:
86         extern(D) this(const(char)*, int = 1) nothrow { this(); } // compat with MS derived classes
87     }
88 }
89 else version (CppRuntime_DigitalMars)
90 {
91     ///
92     class exception
93     {
94     @nogc:
95         ///
96         extern(D) this() nothrow {}
97         //virtual ~this();
98         void dtor() { }     // reserve slot in vtbl[]
99 
100         ///
101         const(char)* what() const nothrow;
102 
103     protected:
104         this(const(char)*, int = 1) nothrow { this(); } // compat with MS derived classes
105     }
106 }
107 else version (CppRuntime_Microsoft)
108 {
109     ///
110     class exception
111     {
112     @nogc:
113         ///
114         extern(D) this(const(char)* message = "unknown", int = 1) nothrow { msg = message; }
115         ///
116         @weak ~this() nothrow {}
117 
118         ///
119         @weak const(char)* what() const nothrow { return msg != null ? msg : "unknown exception"; }
120 
121         // TODO: do we want this? exceptions are classes... ref types.
122 //        final ref exception opAssign(ref const(exception) e) nothrow { msg = e.msg; return this; }
123 
124     protected:
125         @weak void _Doraise() const { assert(0); }
126 
127     protected:
128         const(char)* msg;
129     }
130 
131 }
132 else
133     static assert(0, "Missing std::exception binding for this platform");
134 
135 ///
136 class bad_exception : exception
137 {
138 @nogc:
139     ///
140     extern(D) this(const(char)* message = "bad exception") nothrow { super(message); }
141 
142     version (GenericBaseException)
143     {
144         ///
145         @weak override const(char)* what() const nothrow { return "bad exception"; }
146     }
147 }