source:
doc/theses/andrew_beach_MMath/code/throw-detor.cfa
@
433e2c3
Last change on this file since 433e2c3 was f79ee0d, checked in by , 3 years ago | |
---|---|
|
|
File size: 994 bytes |
Rev | Line | |
---|---|---|
[ea593a3] | 1 | // Throw Across Destructor |
[ee23a8d] | 2 | #include <clock.hfa> |
[ea593a3] | 3 | #include <exception.hfa> |
[ee23a8d] | 4 | #include <fstream.hfa> |
[f79ee0d] | 5 | #include <stdlib.hfa> // strto |
[ea593a3] | 6 | |
[f79ee0d] | 7 | exception empty_exception; |
8 | vtable(empty_exception) empty_vt; | |
[ea593a3] | 9 | |
10 | struct WithDestructor {}; | |
11 | ||
12 | void ^?{}(WithDestructor & this) { | |
[54651005] | 13 | asm volatile ("# destructor body"); |
[ea593a3] | 14 | } |
15 | ||
16 | void unwind_destructor(unsigned int frames) { | |
17 | if (frames) { | |
18 | WithDestructor object; | |
19 | unwind_destructor(frames - 1); | |
20 | } else { | |
21 | throw (empty_exception){&empty_vt}; | |
22 | } | |
23 | } | |
24 | ||
25 | int main(int argc, char * argv[]) { | |
26 | unsigned int times = 1; | |
27 | unsigned int total_frames = 1; | |
[ee23a8d] | 28 | if (1 < argc) { |
[f79ee0d] | 29 | times = strto(argv[1], 0p, 10); |
[ea593a3] | 30 | } |
[ee23a8d] | 31 | if (2 < argc) { |
[f79ee0d] | 32 | total_frames = strto(argv[2], 0p, 10); |
[ea593a3] | 33 | } |
34 | ||
[ee23a8d] | 35 | Time start_time = timeHiRes(); |
[ea593a3] | 36 | for (int count = 0 ; count < times ; ++count) { |
37 | try { | |
38 | unwind_destructor(total_frames); | |
39 | } catch (empty_exception *) { | |
[54651005] | 40 | asm volatile ("# catch block"); |
[ea593a3] | 41 | } |
42 | } | |
[ee23a8d] | 43 | Time end_time = timeHiRes(); |
[f79ee0d] | 44 | sout | "Run-Time (s): " | wd(0,1, (end_time - start_time)`ns / 1_000_000_000.); |
[ea593a3] | 45 | } |
Note: See TracBrowser
for help on using the repository browser.