-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest_weak_pointer.cc
111 lines (107 loc) · 2.89 KB
/
test_weak_pointer.cc
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include "thread.h"
#include "extension.h"
#include "extension.cc"
struct t_symbol : t_object_with_extension
{
std::string v_name;
t_symbol(std::string_view a_name) : v_name(a_name)
{
f_epoch_point<t_type>();
}
};
t_root<t_slot_of<t_symbol>> v_resurrected;
int main(int argc, char* argv[])
{
t_engine<t_type>::t_options options;
if (argc > 1) std::sscanf(argv[1], "%zu", &options.v_collector__threshold);
options.v_verbose = true;
t_engine_with_finalizer engine(options, [](auto RECYCLONE__SPILL a_p)
{
f_epoch_point<t_type>();
if (a_p->f_type() != &t_type_of<t_symbol>::v_instance) return;
auto p = static_cast<t_symbol*>(a_p);
if (static_cast<t_symbol*>(p)->v_name == "resurrected"sv) return;
p->v_name = "resurrected"sv;
v_resurrected = p;
p->f_finalizee__(true);
});
return [&]() RECYCLONE__NOINLINE
{
auto pair = [](t_object<t_type>* x, t_object<t_type>* y)
{
return std::make_pair(x, y);
};
std::unique_ptr<t_weak_pointer> w;
f_with_scratch([&]
{
f_epoch_point<t_type>();
auto RECYCLONE__SPILL x = f_new<t_symbol>("foo"sv);
std::fprintf(stderr, "x: %p\n", x);
w = std::make_unique<t_weak_pointer>(x, false);
assert(w->f_get() == pair(x, nullptr));
});
engine.f_collect();
#ifndef NDEBUG
assert(w->f_get() == pair(nullptr, nullptr));
#endif
f_with_scratch([&]
{
f_epoch_point<t_type>();
auto RECYCLONE__SPILL x = f_new<t_symbol>("foo"sv);
std::fprintf(stderr, "x: %p\n", x);
auto RECYCLONE__SPILL y = f_new<t_symbol>("bar"sv);
std::fprintf(stderr, "y: %p\n", y);
w = std::make_unique<t_weak_pointer>(x, y);
assert(w->f_get() == pair(x, y));
auto RECYCLONE__SPILL z = f_new<t_symbol>("zot"sv);
std::fprintf(stderr, "z: %p\n", z);
w->f_dependent__(z);
assert(w->f_get() == pair(x, z));
});
engine.f_collect();
#ifndef NDEBUG
assert(w->f_get() == pair(nullptr, nullptr));
#endif
f_with_scratch([&]
{
f_epoch_point<t_type>();
auto RECYCLONE__SPILL x = f_new<t_symbol>("bar"sv);
std::fprintf(stderr, "x: %p\n", x);
x->f_finalizee__(true);
w = std::make_unique<t_weak_pointer>(x, true);
assert(w->f_get() == pair(x, nullptr));
});
engine.f_collect();
engine.f_finalize();
f_with_scratch([&]
{
f_epoch_point<t_type>();
assert(w->f_get().first != nullptr);
v_resurrected = nullptr;
});
engine.f_collect();
engine.f_finalize();
engine.f_collect();
#ifndef NDEBUG
assert(w->f_get() == pair(nullptr, nullptr));
#endif
f_with_scratch([&]
{
f_epoch_point<t_type>();
auto RECYCLONE__SPILL x = f_new<t_symbol>("foo"sv);
std::fprintf(stderr, "x: %p\n", x);
w = std::make_unique<t_weak_pointer>(x, true);
auto RECYCLONE__SPILL y = f_new<t_symbol>("bar"sv);
std::fprintf(stderr, "y: %p\n", y);
w->f_target__(y);
assert(w->f_get() == pair(y, nullptr));
});
engine.f_join_foregrounds();
#ifdef NDEBUG
std::exit(0);
#else
engine.f_quit_finalizer();
#endif
return 0;
}();
}