[190a833] | 1 | // Use of the exception system where we provide our own functions. |
---|
| 2 | |
---|
| 3 | #include <stdio.h> |
---|
| 4 | #include <string.h> |
---|
| 5 | // Using collections/string.hfa leads to a resolution error on snprintf. |
---|
| 6 | |
---|
| 7 | exception BadIndexException { |
---|
| 8 | int value; |
---|
| 9 | int size; |
---|
| 10 | char * message; |
---|
| 11 | }; |
---|
| 12 | |
---|
| 13 | const char * virtual_msg(BadIndexException * this) { |
---|
| 14 | return this->virtual_table->msg(this); |
---|
| 15 | } |
---|
| 16 | |
---|
| 17 | // Array length calculated to ususually be big enough. |
---|
| 18 | const size_t msg_size = 52; |
---|
| 19 | |
---|
| 20 | char * format_bad_index(int value, int size) { |
---|
| 21 | char * out = (char *)(void *)malloc(msg_size + 1); |
---|
| 22 | snprintf(out, msg_size, |
---|
| 23 | "Out of Range Index %d (expected less than %d)", value, size); |
---|
| 24 | return out; |
---|
| 25 | } |
---|
| 26 | |
---|
| 27 | const char * msg(BadIndexException * this) { |
---|
| 28 | if (this->message) { |
---|
| 29 | free(this->message); |
---|
| 30 | } |
---|
| 31 | this->message = format_bad_index(this->value, this->size); |
---|
| 32 | return this->message; |
---|
| 33 | } |
---|
| 34 | |
---|
| 35 | void copy(BadIndexException * dst, BadIndexException * src) { |
---|
| 36 | // This is an easy detail to miss, you have to copy the table over. |
---|
| 37 | dst->virtual_table = src->virtual_table; |
---|
| 38 | |
---|
| 39 | dst->value = src->value; |
---|
| 40 | dst->size = src->size; |
---|
| 41 | dst->message = (src->message) ? strndup(src->message, msg_size) : 0p; |
---|
| 42 | } |
---|
| 43 | |
---|
| 44 | void ^?{}(BadIndexException & this) { |
---|
| 45 | free(this.message); |
---|
| 46 | } |
---|
| 47 | |
---|
| 48 | vtable(BadIndexException) arrayIndex = { |
---|
| 49 | .msg = msg, |
---|
| 50 | .copy = copy, |
---|
| 51 | .^?{} = ^?{}, |
---|
| 52 | }; |
---|
| 53 | |
---|
| 54 | // This is not supposed to be a real range check, but that's the idea: |
---|
| 55 | void failRangeCheck(int index, int size) { |
---|
| 56 | throw (BadIndexException){ &arrayIndex, index, size }; |
---|
| 57 | } |
---|
| 58 | |
---|
| 59 | int atDefault(int fallback) { |
---|
| 60 | try { |
---|
| 61 | failRangeCheck(10, 8); |
---|
| 62 | } catch (BadIndexException * error) { |
---|
| 63 | printf("%s\n", virtual_msg(error)); |
---|
| 64 | } |
---|
| 65 | return fallback; |
---|
| 66 | } |
---|
| 67 | |
---|
| 68 | int main() { |
---|
| 69 | int value = atDefault(5); |
---|
| 70 | printf("%d\n", value); |
---|
| 71 | } |
---|