This repository has been archived by the owner on Oct 17, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
state_saver_success_example.cpp
117 lines (93 loc) · 3.84 KB
/
state_saver_success_example.cpp
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
112
113
114
115
116
117
// Licensed under the MIT License <http://opensource.org/licenses/MIT>.
// SPDX-License-Identifier: MIT
// Copyright (c) 2018 - 2021 Daniil Goncharov <[email protected]>.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include <state_saver.hpp>
#include <iostream>
#include <stdexcept>
void foo1(int& a) {
SAVER_SUCCESS(a); // State saver on success.
a = 1;
std::cout << "foo1 a = " << a << std::endl;
// Original state will automatically restored, on scope leave when no exceptions have been thrown.
}
void foo2(int& a) {
SAVER_SUCCESS(a); // Custom state saver on success.
a = 2;
std::cout << "foo2 a = " << a << std::endl;
throw std::runtime_error{"error"};
// Original state will not automatically restored, on error.
}
void foo3(int& a) {
#if defined(__cpp_deduction_guides) && __cpp_deduction_guides >= 201611L
state_saver::saver_success state_saver{a}; // Custom state saver on success, without macros.
#else
state_saver::saver_success<decltype(a)> state_saver{a}; // Custom state saver on success, without macros.
#endif
a = 3;
std::cout << "foo3 a = " << a << std::endl;
// Original state will automatically restored, on scope leave when no exceptions have been thrown.
}
void foo4(int& a) {
MAKE_SAVER_EXIT(state_saver, a); // Custom state saver on success.
a = 4;
std::cout << "foo4 a = " << a << std::endl;
state_saver.dismiss(); // Dismiss, state will not automatically restored.
std::cout << "foo4 state_saver::dismiss" << std::endl;
// Original state will not automatically restored, on scope leave when no exceptions have been thrown.
}
void foo5(int& a) {
MAKE_SAVER_EXIT(state_saver, a); // Custom state saver on success.
a = 5;
std::cout << "foo5 a = " << a << std::endl;
state_saver.dismiss(); // Dismiss, state will not automatically restored.
std::cout << "foo5 state_saver::dismiss" << std::endl;
state_saver.restore(); // Restore state.
std::cout << "foo5 state_saver::restore" << std::endl;
std::cout << "foo5 a = " << a << std::endl;
// Original state will not automatically restored, on scope leave when no exceptions have been thrown.
}
void foo6(int& a) {
WITH_SAVER_SUCCESS(a) {
a = 1;
std::cout << "foo6 a = " << a << std::endl;
// Original state will automatically restored, on scope leave when no exceptions have been thrown.
}
std::cout << "foo6 a = " << a << std::endl;
}
int main() {
int a = 0;
std::cout << "main a = " << a << std::endl;
foo1(a);
std::cout << "main a = " << a << std::endl;
try {
foo2(a);
} catch (...) {}
std::cout << "main a = " << a << std::endl;
foo3(a);
std::cout << "main a = " << a << std::endl;
foo4(a);
std::cout << "main a = " << a << std::endl;
foo5(a);
std::cout << "main a = " << a << std::endl;
foo6(a);
std::cout << "main a = " << a << std::endl;
return 0;
}