source:
doc/theses/andrew_beach_MMath/code/throw-detor.cpp
@
b5e3a80
Last change on this file since b5e3a80 was f79ee0d, checked in by , 3 years ago | |
---|---|
|
|
File size: 1.1 KB |
Rev | Line | |
---|---|---|
[ea593a3] | 1 | // Throw Across Destructor |
[ee23a8d] | 2 | #include <chrono> |
[ea593a3] | 3 | #include <cstdlib> |
[ee23a8d] | 4 | #include <exception> |
5 | #include <iostream> | |
[f79ee0d] | 6 | #include <iomanip> |
[ee23a8d] | 7 | |
[f79ee0d] | 8 | using namespace std; |
[ee23a8d] | 9 | using namespace std::chrono; |
[ea593a3] | 10 | |
11 | struct EmptyException : public std::exception {}; | |
12 | ||
13 | struct WithDestructor { | |
[54651005] | 14 | ~WithDestructor() { |
15 | asm volatile ("# destructor body"); | |
16 | } | |
[ea593a3] | 17 | }; |
18 | ||
19 | void unwind_destructor(unsigned int frames) { | |
20 | if (frames) { | |
21 | WithDestructor object; | |
22 | unwind_destructor(frames - 1); | |
23 | } else { | |
24 | throw (EmptyException){}; | |
25 | } | |
26 | } | |
27 | ||
28 | int main(int argc, char * argv[]) { | |
29 | unsigned int times = 1; | |
30 | unsigned int total_frames = 1; | |
[ee23a8d] | 31 | if (1 < argc) { |
[ea593a3] | 32 | times = strtol(argv[1], nullptr, 10); |
33 | } | |
[ee23a8d] | 34 | if (2 < argc) { |
[ea593a3] | 35 | total_frames = strtol(argv[2], nullptr, 10); |
36 | } | |
37 | ||
[ee23a8d] | 38 | time_point<steady_clock> start_time = steady_clock::now(); |
[ea593a3] | 39 | for (int count = 0 ; count < times ; ++count) { |
40 | try { | |
41 | unwind_destructor(total_frames); | |
42 | } catch (EmptyException &) { | |
[54651005] | 43 | asm volatile ("# catch block"); |
[ea593a3] | 44 | } |
45 | } | |
[ee23a8d] | 46 | time_point<steady_clock> end_time = steady_clock::now(); |
47 | nanoseconds duration = duration_cast<nanoseconds>(end_time - start_time); | |
[f79ee0d] | 48 | cout << "Run-Time (s): " << fixed << setprecision(1) << duration.count() / 1'000'000'000. << endl; |
[ea593a3] | 49 | } |
Note: See TracBrowser
for help on using the repository browser.