FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
cpp11/cpp11test/src/test-protect-nested.cpp at main · josh28x/cpp11 · GitHub
josh28x
cpp11
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
cpp11
/
cpp11test
/
src
/
test-protect-nested.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
77 lines (68 loc) · 2.49 KB
Breadcrumbs
cpp11
/
cpp11test
/
src
/
test-protect-nested.cpp
Copy path
File metadata and controls
77 lines (68 loc) · 2.49 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#
include
"
cpp11/function.hpp
"
#
include
"
cpp11/protect.hpp
"
#
include
"
testthat.h
"
/*
* See https://github.com/r-lib/cpp11/pull/327 for full details.
*
* - `cpp11::package("cpp11test")["test_destruction_outer"]` uses
* `unwind_protect()` to call R level `test_destruction_outer()` but no entry
* macros are set up. Instead we are going to catch exceptions that get here
* with `expect_error_as()`.
*
* - Call R level `test_destruction_outer()` to set up `BEGIN_CPP11` /
* `END_CPP11` entry macros.
*
* - C++ `test_destruction_outer()` goes through `unwind_protect()` to call
* the R level `test_destruction_inner()`.
*
* - R level `test_destruction_inner()` sets up its own `BEGIN_CPP11` /
* `END_CPP11` entry macros.
*
* - C++ `test_destruction_inner()` goes through `unwind_protect()` to call
* `Rf_error()` (i.e., we are nested within `unwind_protect()`s!).
*
* - `longjmp()` is caught from inner `unwind_protect()`, and an exception
* is thrown which is caught by the inner entry macros, allowing us to run
* the destructor of `x`, then we let R continue the unwind process.
*
* - This `longjmp()`s again and is caught by the outer `unwind_protect()`, an
* exception is thrown which is caught by the outer entry macros, and we let
* R continue the unwind process one more time.
*
* - This `longjmp()` is caught by `cpp11::package()`'s `unwind_protect()`,
* an exception is thrown, and that is caught by `expect_error_as()`.
*/
//
Global variable to detect if the destructor has been run or not
static
bool
destructed =
false
;
class
HasDestructor
{
public:
~HasDestructor
();
};
HasDestructor::~HasDestructor
() {
//
Destructor has run!
destructed =
true
;
}
[[cpp11::register]]
void
test_destruction_inner
() {
//
Expect that `x`'s destructor gets to run on the way out
HasDestructor x{};
cpp11::stop
(
"
oh no!
"
);
}
[[cpp11::register]]
void
test_destruction_outer
() {
const
auto
test_destruction_inner =
cpp11::package
(
"
cpp11test
"
)[
"
test_destruction_inner
"
];
test_destruction_inner
();
}
context
(
"
unwind_protect-nested-C++
"
) {
test_that
(
"
nested `unwind_protect()` (with entry macros set up) will run destructors
"
"
(#327)
"
) {
const
auto
fn = [&] {
const
auto
test_destruction_outer =
cpp11::package
(
"
cpp11test
"
)[
"
test_destruction_outer
"
];
test_destruction_outer
();
};
expect_error_as
(
fn
(), cpp11::unwind_exception);
expect_true
(destructed);
destructed =
false
;
}
}
Back
|
FazBrowse Home
|
New Git URL