About
Contact
QtCreator
KDevelop
Solarized
1
2
// Copyright (C) 2006-2009, 2012 Alexander Nasonov
3
// Copyright (C) 2012 Lorenzo Caminiti
4
// Distributed under the Boost Software License, Version 1.0
5
// (see accompanying file LICENSE_1_0.txt or a copy at
6
//
http://www.boost.org/LICENSE_1_0.txt
)
7
// Home at
http://www.boost.org/libs/scope_exit
8
9
#include
<boost/config.hpp>
10
#
ifdef
BOOST_NO_CXX11_VARIADIC_MACROS
11
# error "variadic macros required"
12
#
else
13
14
#include
<boost/scope_exit.hpp>
15
#include
<boost/typeof/typeof.hpp>
16
#include
<boost/typeof/std/vector.hpp>
17
#include
BOOST_TYPEOF_INCREMENT_REGISTRATION_GROUP
()
18
#include
<boost/detail/lightweight_test.hpp>
19
#include
<vector>
20
21
struct
person
{};
22
BOOST_TYPEOF_REGISTER_TYPE
(person)
23
24
struct
world
{
25
void
add_person
(
person
const
&
a_person
);
26
size_t
population
(
void
)
const
{
return
persons_
.
size
(); }
27
28
private
:
29
std::
vector
<
person
>
persons_
;
30
};
31
BOOST_TYPEOF_REGISTER_TYPE
(world)
32
33
//[world
34
void
world
::
add_person
(
person
const
&
a_person
) {
35
bool
commit
=
false
;
36
37
persons_
.
push_back
(
x:
a_person
);
// (1) direct action
38
// Following block is executed when the enclosing scope exits.
39
BOOST_SCOPE_EXIT
(&
commit
, &
persons_
) {
40
if
(!
commit
)
persons_
.
pop_back
();
// (2) rollback action
41
}
BOOST_SCOPE_EXIT_END
42
43
// ... // (3) other operations
44
45
commit
=
true
;
// (4) disable rollback actions
46
}
47
//]
48
49
int
main
(
void
) {
50
world
w
;
51
person
p
;
52
w
.
add_person
(
a_person:
p
);
53
BOOST_TEST
(
w
.
population
() ==
1
);
54
return
boost::
report_errors
();
55
}
56
57
#
endif
// variadic macros
58
59