blob: 6e6e595bf8bc4d480ed79adbadc6f7c524b78a49 (
plain)
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
|
/////////////////////////////////////////////////////////////////////////////
// Copyright (c) Electronic Arts Inc. All rights reserved.
/////////////////////////////////////////////////////////////////////////////
#include "EASTLTest.h"
#include <EASTL/finally.h>
int TestFinally()
{
using namespace eastl;
int nErrorCount = 0;
{
#if defined(EA_COMPILER_CPP17_ENABLED)
{
// requires CTAD (class template argument deduction)
int a = 0;
{
VERIFY(a == 0);
eastl::finally _([&] { a = 42; });
VERIFY(a == 0);
}
VERIFY(a == 42);
}
#endif
{
int a = 0;
{
VERIFY(a == 0);
auto _ = eastl::make_finally([&] { a = 42; });
VERIFY(a == 0);
}
VERIFY(a == 42);
}
{
int a = 0;
{
VERIFY(a == 0);
auto f = eastl::make_finally([&] { a = 42; });
VERIFY(a == 0);
f.dismiss();
VERIFY(a == 0);
}
VERIFY(a == 0);
}
{
int a = 0;
{
VERIFY(a == 0);
auto f = eastl::make_finally([&] { a = 42; });
VERIFY(a == 0);
f.execute();
VERIFY(a == 42);
}
VERIFY(a == 42);
}
{
int a = 0;
{
VERIFY(a == 0);
auto f = eastl::make_finally([&] { a = 42; });
VERIFY(a == 0);
f.execute();
VERIFY(a == 42);
// verify the finally object doesn't re-run the callback on scope-exit.
a = -1;
}
VERIFY(a == -1);
}
{
struct local_flag { bool b = false; };
local_flag lf;
VERIFY(lf.b == false);
{ auto _ = eastl::make_finally([&] { lf.b = true; }); }
VERIFY(lf.b);
}
// This currently does not compile by design.
//
// {
// int a = 0;
// auto lbda = [&a] { a = 1234; };
// {
// VERIFY(a == 0);
// auto _ = eastl::make_finally(lbda); // compiler error
// VERIFY(a == 0);
// }
// VERIFY(a == 1234);
// }
}
return nErrorCount;
}
|