1 /* Copyright (C) 2019-2021 Free Software Foundation, Inc.
3 This file is part of GDB.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
18 #ifndef COMMON_FORWARD_SCOPE_EXIT_H
19 #define COMMON_FORWARD_SCOPE_EXIT_H
21 #include "gdbsupport/scope-exit.h"
24 /* A forward_scope_exit is like scope_exit, but instead of giving it a
25 callable, you instead specialize it for a given cleanup function,
26 and the generated class automatically has a constructor with the
27 same interface as the cleanup function. forward_scope_exit
28 captures the arguments passed to the ctor, and in turn passes those
29 as arguments to the wrapped cleanup function, when it is called at
30 scope exit time, from within the forward_scope_exit dtor. The
31 forward_scope_exit class can take any number of arguments, and is
34 This allows usage like this:
37 delete_longjmp_breakpoint (int arg)
39 // Blah, blah, blah...
42 using longjmp_breakpoint_cleanup
43 = FORWARD_SCOPE_EXIT (delete_longjmp_breakpoint);
45 This above created a new cleanup class `longjmp_breakpoint_cleanup`
46 than can then be used like this:
48 longjmp_breakpoint_cleanup obj (thread);
50 // Blah, blah, blah...
52 obj.release (); // Optional cancel if needed.
54 forward_scope_exit is also handy when you would need to wrap a
55 scope_exit in a gdb::optional:
57 gdb::optional<longjmp_breakpoint_cleanup> cleanup;
59 cleanup.emplace (thread);
64 since with scope exit, you would have to know the scope_exit's
65 callable template type when you create the gdb::optional:
67 gdb:optional<scope_exit<what goes here?>>
69 The "forward" naming fits both purposes shown above -- the class
70 "forwards" ctor arguments to the wrapped cleanup function at scope
71 exit time, and can also be used to "forward declare"
72 scope_exit-like objects. */
77 /* Function and Signature are passed in the same type, in order to
78 extract Function's arguments' types in the specialization below.
79 Those are used to generate the constructor. */
81 template<typename Function
, Function
*function
, typename Signature
>
82 struct forward_scope_exit
;
84 template<typename Function
, Function
*function
,
85 typename Res
, typename
... Args
>
86 class forward_scope_exit
<Function
, function
, Res (Args
...)>
87 : public scope_exit_base
<forward_scope_exit
<Function
,
91 /* For access to on_exit(). */
92 friend scope_exit_base
<forward_scope_exit
<Function
,
97 explicit forward_scope_exit (Args
...args
)
98 : m_bind_function (function
, args
...)
109 /* The function and the arguments passed to the ctor, all packed in
111 decltype (std::bind (function
, std::declval
<Args
> ()...))
115 } /* namespace detail */
117 /* This is the "public" entry point. It's a macro to avoid having to
118 name FUNC more than once. */
120 #define FORWARD_SCOPE_EXIT(FUNC) \
121 detail::forward_scope_exit<decltype (FUNC), FUNC, decltype (FUNC)>
123 #endif /* COMMON_FORWARD_SCOPE_EXIT_H */
This page took 0.032081 seconds and 4 git commands to generate.