Mir
unwind_helpers.h
Go to the documentation of this file.
1 /*
2  * Copyright © 2015 Canonical Ltd.
3  *
4  * This program is free software: you can redistribute it and/or modify it
5  * it under the terms of the GNU Lesser General Public License version 3 as
6  * published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU Lesser General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  *
16  * Authored by: Andreas Pokorny <andreas.pokorny@canonical.com>
17  */
18 
19 #ifndef MIR_UNWIND_HELPERS_H_
20 #define MIR_UNWIND_HELPERS_H_
21 
22 #include <stdexcept>
23 #include <iostream>
24 
25 namespace mir
26 {
27 template<typename Unwind>
29 {
30 public:
31  template<typename Apply>
32  RevertIfUnwinding(Apply && apply, Unwind&& unwind)
33  : unwind{std::move(unwind)}
34  {
35  apply();
36  }
37 
38  RevertIfUnwinding(Unwind&& unwind)
39  : unwind{std::move(unwind)}
40  {
41  }
42 
44  : unwind{std::move(rhs.unwind)}
45  {
46  }
47 
49  {
50  if (std::uncaught_exception())
51  unwind();
52  }
53 
54 private:
55  RevertIfUnwinding(RevertIfUnwinding const&) = delete;
56  RevertIfUnwinding& operator=(RevertIfUnwinding const&) = delete;
57 
58  Unwind unwind;
59 };
60 
61 template<typename Apply, typename Revert>
62 inline auto try_but_revert_if_unwinding(Apply && apply, Revert && reverse) -> RevertIfUnwinding<Revert>
63 {
64  return RevertIfUnwinding<Revert>{std::move(apply), std::move(reverse)};
65 }
66 
67 
68 template<typename Revert>
69 inline auto on_unwind(Revert && action) -> RevertIfUnwinding<Revert>
70 {
71  return RevertIfUnwinding<Revert>{std::move(action)};
72 }
73 
74 }
75 
76 #endif
All things Mir.
Definition: atomic_callback.h:25
auto on_unwind(Revert &&action) -> RevertIfUnwinding< Revert >
Definition: unwind_helpers.h:69
RevertIfUnwinding(Unwind &&unwind)
Definition: unwind_helpers.h:38
RevertIfUnwinding(RevertIfUnwinding< Unwind > &&rhs)
Definition: unwind_helpers.h:43
Definition: unwind_helpers.h:28
RevertIfUnwinding(Apply &&apply, Unwind &&unwind)
Definition: unwind_helpers.h:32
auto try_but_revert_if_unwinding(Apply &&apply, Revert &&reverse) -> RevertIfUnwinding< Revert >
Definition: unwind_helpers.h:62
~RevertIfUnwinding()
Definition: unwind_helpers.h:48

Copyright © 2012-2015 Canonical Ltd.
Generated on Wed Mar 30 00:29:56 UTC 2016