1 | // |
---|
2 | // Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo |
---|
3 | // |
---|
4 | // The contents of this file are covered under the licence agreement in the |
---|
5 | // file "LICENCE" distributed with Cforall. |
---|
6 | // |
---|
7 | // exception.hfa -- User facing tools for working with exceptions. |
---|
8 | // |
---|
9 | // Author : Andrew Beach |
---|
10 | // Created On : Thu Apr 7 10:25:00 2020 |
---|
11 | // Last Modified By : Andrew Beach |
---|
12 | // Last Modified On : Thu Apr 7 10:25:00 2020 |
---|
13 | // Update Count : 0 |
---|
14 | // |
---|
15 | |
---|
16 | // Everything below this line should be considered a patch while the exception |
---|
17 | // objects themselves are designed and created and should be removed in time. |
---|
18 | // ----------------------------------------------------------------------------------------------- |
---|
19 | |
---|
20 | // All internals helper macros begin with an underscore. |
---|
21 | #define _CLOSE(...) __VA_ARGS__ } |
---|
22 | #define _GLUE2(left, right) left##right |
---|
23 | #define _GLUE3(left, middle, right) left##middle##right |
---|
24 | #define _EXC_DISPATCH(to, ...) to(__VA_ARGS__,__cfaehm_base_exception_t,) |
---|
25 | |
---|
26 | // FWD_TRIVIAL_EXCEPTION(exception_name); |
---|
27 | // Declare a trivial exception, one that adds no fields or features. |
---|
28 | // This will make the exception visible and may go in a .hfa or .cfa file. |
---|
29 | #define FWD_TRIVIAL_EXCEPTION(...) _EXC_DISPATCH(_FWD_TRIVIAL_EXCEPTION, __VA_ARGS__) |
---|
30 | // INST_TRIVIAL_EXCEPTION(exception_name); |
---|
31 | // Create the trival exception. This must be used exactly once and should be used in a .cfa file, |
---|
32 | // as it creates the unique instance of the virtual table. |
---|
33 | #define INST_TRIVIAL_EXCEPTION(...) _EXC_DISPATCH(_INST_TRIVIAL_EXCEPTION, __VA_ARGS__) |
---|
34 | // TRIVIAL_EXCEPTION(exception_name[, parent_name]); |
---|
35 | // Does both of the above, a short hand if the exception is only used in one .cfa file. |
---|
36 | // For legacy reasons this is the only one that official supports having a parent other than the |
---|
37 | // base exception. This feature may be removed or changed. |
---|
38 | #define TRIVIAL_EXCEPTION(...) \ |
---|
39 | _EXC_DISPATCH(_FWD_TRIVIAL_EXCEPTION, __VA_ARGS__); \ |
---|
40 | _EXC_DISPATCH(_INST_TRIVIAL_EXCEPTION, __VA_ARGS__) |
---|
41 | #define _FWD_TRIVIAL_EXCEPTION(exception_name, parent_name, ...) \ |
---|
42 | _VTABLE_DECLARATION(exception_name, parent_name)(); \ |
---|
43 | struct exception_name { \ |
---|
44 | VTABLE_FIELD(exception_name); \ |
---|
45 | }; \ |
---|
46 | void ?{}(exception_name & this); \ |
---|
47 | const char * _GLUE2(exception_name,_msg)(exception_name * this) |
---|
48 | #define _INST_TRIVIAL_EXCEPTION(exception_name, parent_name, ...) \ |
---|
49 | void ?{}(exception_name & this) { \ |
---|
50 | VTABLE_INIT(this, exception_name); \ |
---|
51 | } \ |
---|
52 | const char * _GLUE2(exception_name,_msg)(exception_name * this) { \ |
---|
53 | return #exception_name; \ |
---|
54 | } \ |
---|
55 | _VTABLE_INSTANCE(exception_name, parent_name,)(_GLUE2(exception_name,_msg)) |
---|
56 | |
---|
57 | // DATA_EXCEPTION(exception_name)(fields...); |
---|
58 | // Forward declare an exception that adds fields but no features. The added fields go in the |
---|
59 | // second argument list. The virtual table instance must be provided later (see VTABLE_INSTANCE). |
---|
60 | #define DATA_EXCEPTION(...) _EXC_DISPATCH(_DATA_EXCEPTION, __VA_ARGS__) |
---|
61 | #define _DATA_EXCEPTION(exception_name, parent_name, ...) \ |
---|
62 | _VTABLE_DECLARATION(exception_name, parent_name)(); \ |
---|
63 | struct exception_name { VTABLE_FIELD(exception_name); _CLOSE |
---|
64 | |
---|
65 | // VTABLE_DECLARATION(exception_name)([new_features...]); |
---|
66 | // Declare a virtual table type for an exception with exception_name. You may also add features |
---|
67 | // (fields on the virtual table) by including them in the second list. |
---|
68 | #define VTABLE_DECLARATION(...) _EXC_DISPATCH(_VTABLE_DECLARATION, __VA_ARGS__) |
---|
69 | #define _VTABLE_DECLARATION(exception_name, parent_name, ...) \ |
---|
70 | struct exception_name; \ |
---|
71 | VTABLE_TYPE(exception_name); \ |
---|
72 | extern VTABLE_TYPE(exception_name) VTABLE_NAME(exception_name); \ |
---|
73 | VTABLE_TYPE(exception_name) { \ |
---|
74 | VTABLE_TYPE(parent_name) const * parent; \ |
---|
75 | size_t size; \ |
---|
76 | void (*copy)(exception_name * this, exception_name * other); \ |
---|
77 | void (*free)(exception_name & this); \ |
---|
78 | const char * (*msg)(exception_name * this); \ |
---|
79 | _CLOSE |
---|
80 | |
---|
81 | // VTABLE_INSTANCE(exception_name)(msg [, others...]); |
---|
82 | // Create the instance of the virtual table. There must be exactly one instance of a virtual table |
---|
83 | // for each exception type. This fills in most of the fields of the virtual table (uses ?=? and |
---|
84 | // ^?{}) but you must provide the message function and any other fields added in the declaration. |
---|
85 | #define VTABLE_INSTANCE(...) _EXC_DISPATCH(_VTABLE_INSTANCE, __VA_ARGS__) |
---|
86 | #define _VTABLE_INSTANCE(exception_name, parent_name, ...) \ |
---|
87 | void _GLUE2(exception_name,_copy)(exception_name * this, exception_name * other) { \ |
---|
88 | *this = *other; \ |
---|
89 | } \ |
---|
90 | VTABLE_TYPE(exception_name) VTABLE_NAME(exception_name) @= { \ |
---|
91 | &VTABLE_NAME(parent_name), sizeof(exception_name), \ |
---|
92 | _GLUE2(exception_name,_copy), ^?{}, \ |
---|
93 | _CLOSE |
---|
94 | |
---|
95 | // VTABLE_TYPE(exception_name) | VTABLE_NAME(exception_name) |
---|
96 | // Get the name of the vtable type or the name of the vtable instance for an exception type. |
---|
97 | #define VTABLE_TYPE(exception_name) struct _GLUE2(exception_name,_vtable) |
---|
98 | #define VTABLE_NAME(exception_name) _GLUE3(_,exception_name,_vtable_instance) |
---|
99 | |
---|
100 | // VTABLE_FIELD(exception_name); |
---|
101 | // The declaration of the virtual table field. Should be the first declaration in a virtual type. |
---|
102 | #define VTABLE_FIELD(exception_name) VTABLE_TYPE(exception_name) const * virtual_table |
---|
103 | |
---|
104 | // VTABLE_INIT(object_reference, exception_name); |
---|
105 | // Sets a virtual table field on an object to the virtual table instance for the type. |
---|
106 | #define VTABLE_INIT(this, exception_name) (this).virtual_table = &VTABLE_NAME(exception_name) |
---|